2015-02-10 14:50:43 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Dictionaries/IDictionary.h>
|
|
|
|
#include <Dictionaries/IDictionarySource.h>
|
|
|
|
#include <Dictionaries/DictionaryStructure.h>
|
|
|
|
#include <Common/ArenaWithFreeLists.h>
|
2017-04-08 01:32:05 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/bit_cast.h>
|
2015-02-19 14:51:39 +00:00
|
|
|
#include <cmath>
|
2015-03-24 17:02:56 +00:00
|
|
|
#include <atomic>
|
2015-02-19 14:51:39 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2015-02-26 14:53:33 +00:00
|
|
|
#include <tuple>
|
2017-09-09 23:17:38 +00:00
|
|
|
#include <pcg_random.hpp>
|
2017-07-28 17:34:02 +00:00
|
|
|
#include <shared_mutex>
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class CacheDictionary final : public IDictionary
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
CacheDictionary(const std::string & name, const DictionaryStructure & dict_struct,
|
|
|
|
DictionarySourcePtr source_ptr, const DictionaryLifetime dict_lifetime,
|
2017-07-21 06:35:58 +00:00
|
|
|
const size_t size);
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
CacheDictionary(const CacheDictionary & other);
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::exception_ptr getCreationException() const override { return {}; }
|
2015-06-09 16:12:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string getName() const override { return name; }
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string getTypeName() const override { return "Cache"; }
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t getBytesAllocated() const override { return bytes_allocated + (string_arena ? string_arena->size() : 0); }
|
2015-03-24 11:30:16 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t getQueryCount() const override { return query_count.load(std::memory_order_relaxed); }
|
2015-05-08 12:31:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
double getHitRate() const override
|
|
|
|
{
|
|
|
|
return static_cast<double>(hit_count.load(std::memory_order_acquire)) /
|
|
|
|
query_count.load(std::memory_order_relaxed);
|
|
|
|
}
|
2015-03-24 17:02:56 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t getElementCount() const override { return element_count.load(std::memory_order_relaxed); }
|
2015-03-24 17:02:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
double getLoadFactor() const override
|
|
|
|
{
|
|
|
|
return static_cast<double>(element_count.load(std::memory_order_relaxed)) / size;
|
|
|
|
}
|
2015-03-24 17:02:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool isCached() const override { return true; }
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2017-10-26 13:19:58 +00:00
|
|
|
std::unique_ptr<IExternalLoadable> clone() const override { return std::make_unique<CacheDictionary>(*this); }
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const IDictionarySource * getSource() const override { return source_ptr.get(); }
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const DictionaryLifetime & getLifetime() const override { return dict_lifetime; }
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const DictionaryStructure & getStructure() const override { return dict_struct; }
|
2015-03-24 13:59:19 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> getCreationTime() const override
|
|
|
|
{
|
|
|
|
return creation_time;
|
|
|
|
}
|
2015-03-24 17:02:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool isInjective(const std::string & attribute_name) const override
|
|
|
|
{
|
|
|
|
return dict_struct.attributes[&getAttribute(attribute_name) - attributes.data()].injective;
|
|
|
|
}
|
2015-05-13 16:11:07 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool hasHierarchy() const override { return hierarchical_attribute; }
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void toParent(const PaddedPODArray<Key> & ids, PaddedPODArray<Key> & out) const override;
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void isInVectorVector(const PaddedPODArray<Key> & child_ids, const PaddedPODArray<Key> & ancestor_ids, PaddedPODArray<UInt8> & out) const override;
|
|
|
|
void isInVectorConstant(const PaddedPODArray<Key> & child_ids, const Key ancestor_id, PaddedPODArray<UInt8> & out) const override;
|
|
|
|
void isInConstantVector(const Key child_id, const PaddedPODArray<Key> & ancestor_ids, PaddedPODArray<UInt8> & out) const override;
|
2016-12-12 21:37:57 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
2017-04-01 07:20:54 +00:00
|
|
|
void get##TYPE(const std::string & attribute_name, const PaddedPODArray<Key> & ids, PaddedPODArray<TYPE> & out) const;
|
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
2017-11-14 00:08:54 +00:00
|
|
|
DECLARE(UInt128)
|
2017-04-01 07:20:54 +00:00
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
2015-11-20 15:53:23 +00:00
|
|
|
#undef DECLARE
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void getString(const std::string & attribute_name, const PaddedPODArray<Key> & ids, ColumnString * out) const;
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
2017-04-01 07:20:54 +00:00
|
|
|
void get##TYPE(\
|
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const PaddedPODArray<TYPE> & def,\
|
|
|
|
PaddedPODArray<TYPE> & out) const;
|
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
2017-11-14 00:08:54 +00:00
|
|
|
DECLARE(UInt128)
|
2017-04-01 07:20:54 +00:00
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
2015-11-20 15:53:23 +00:00
|
|
|
#undef DECLARE
|
2016-06-07 21:07:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void getString(
|
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const ColumnString * const def,
|
|
|
|
ColumnString * const out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
|
|
|
#define DECLARE(TYPE)\
|
2017-04-01 07:20:54 +00:00
|
|
|
void get##TYPE(\
|
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const TYPE def, PaddedPODArray<TYPE> & out) const;
|
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
2017-11-14 00:08:54 +00:00
|
|
|
DECLARE(UInt128)
|
2017-04-01 07:20:54 +00:00
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
2015-11-20 15:53:23 +00:00
|
|
|
#undef DECLARE
|
2016-06-07 21:07:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void getString(
|
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const String & def,
|
|
|
|
ColumnString * const out) const;
|
2015-11-06 14:49:54 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void has(const PaddedPODArray<Key> & ids, PaddedPODArray<UInt8> & out) const override;
|
2015-11-19 13:15:02 +00:00
|
|
|
|
2017-05-29 17:26:45 +00:00
|
|
|
BlockInputStreamPtr getBlockInputStream(const Names & column_names, size_t max_block_size) const override;
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename Value> using ContainerType = Value[];
|
|
|
|
template <typename Value> using ContainerPtrType = std::unique_ptr<ContainerType<Value>>;
|
|
|
|
|
|
|
|
struct CellMetadata final
|
|
|
|
{
|
|
|
|
using time_point_t = std::chrono::system_clock::time_point;
|
|
|
|
using time_point_rep_t = time_point_t::rep;
|
|
|
|
using time_point_urep_t = std::make_unsigned_t<time_point_rep_t>;
|
|
|
|
|
|
|
|
static constexpr UInt64 EXPIRES_AT_MASK = std::numeric_limits<time_point_rep_t>::max();
|
|
|
|
static constexpr UInt64 IS_DEFAULT_MASK = ~EXPIRES_AT_MASK;
|
|
|
|
|
|
|
|
UInt64 id;
|
|
|
|
/// Stores both expiration time and `is_default` flag in the most significant bit
|
|
|
|
time_point_urep_t data;
|
|
|
|
|
|
|
|
/// Sets expiration time, resets `is_default` flag to false
|
|
|
|
time_point_t expiresAt() const { return ext::safe_bit_cast<time_point_t>(data & EXPIRES_AT_MASK); }
|
|
|
|
void setExpiresAt(const time_point_t & t) { data = ext::safe_bit_cast<time_point_urep_t>(t); }
|
|
|
|
|
|
|
|
bool isDefault() const { return (data & IS_DEFAULT_MASK) == IS_DEFAULT_MASK; }
|
|
|
|
void setDefault() { data |= IS_DEFAULT_MASK; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Attribute final
|
|
|
|
{
|
|
|
|
AttributeUnderlyingType type;
|
|
|
|
std::tuple<
|
|
|
|
UInt8, UInt16, UInt32, UInt64,
|
2017-11-14 00:08:54 +00:00
|
|
|
UInt128,
|
2017-04-01 07:20:54 +00:00
|
|
|
Int8, Int16, Int32, Int64,
|
|
|
|
Float32, Float64,
|
|
|
|
String> null_values;
|
|
|
|
std::tuple<
|
|
|
|
ContainerPtrType<UInt8>, ContainerPtrType<UInt16>, ContainerPtrType<UInt32>, ContainerPtrType<UInt64>,
|
2017-11-14 00:08:54 +00:00
|
|
|
ContainerPtrType<UInt128>,
|
2017-04-01 07:20:54 +00:00
|
|
|
ContainerPtrType<Int8>, ContainerPtrType<Int16>, ContainerPtrType<Int32>, ContainerPtrType<Int64>,
|
|
|
|
ContainerPtrType<Float32>, ContainerPtrType<Float64>,
|
|
|
|
ContainerPtrType<StringRef>> arrays;
|
|
|
|
};
|
|
|
|
|
|
|
|
void createAttributes();
|
|
|
|
|
|
|
|
Attribute createAttributeWithType(const AttributeUnderlyingType type, const Field & null_value);
|
|
|
|
|
|
|
|
|
|
|
|
template <typename OutputType, typename DefaultGetter>
|
|
|
|
void getItemsNumber(
|
|
|
|
Attribute & attribute,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
|
|
|
PaddedPODArray<OutputType> & out,
|
|
|
|
DefaultGetter && get_default) const;
|
|
|
|
|
|
|
|
template <typename AttributeType, typename OutputType, typename DefaultGetter>
|
|
|
|
void getItemsNumberImpl(
|
|
|
|
Attribute & attribute,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
|
|
|
PaddedPODArray<OutputType> & out,
|
|
|
|
DefaultGetter && get_default) const;
|
|
|
|
|
|
|
|
template <typename DefaultGetter>
|
|
|
|
void getItemsString(
|
|
|
|
Attribute & attribute,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
|
|
|
ColumnString * out,
|
|
|
|
DefaultGetter && get_default) const;
|
|
|
|
|
|
|
|
template <typename PresentIdHandler, typename AbsentIdHandler>
|
|
|
|
void update(
|
|
|
|
const std::vector<Key> & requested_ids, PresentIdHandler && on_cell_updated,
|
|
|
|
AbsentIdHandler && on_id_not_found) const;
|
|
|
|
|
2017-04-27 17:16:24 +00:00
|
|
|
PaddedPODArray<Key> getCachedIds() const;
|
|
|
|
|
|
|
|
bool isEmptyCell(const UInt64 idx) const;
|
|
|
|
|
2017-09-01 17:21:03 +00:00
|
|
|
size_t getCellIdx(const Key id) const;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
void setDefaultAttributeValue(Attribute & attribute, const Key idx) const;
|
|
|
|
|
|
|
|
void setAttributeValue(Attribute & attribute, const Key idx, const Field & value) const;
|
|
|
|
|
|
|
|
Attribute & getAttribute(const std::string & attribute_name) const;
|
|
|
|
|
|
|
|
struct FindResult
|
|
|
|
{
|
|
|
|
const size_t cell_idx;
|
|
|
|
const bool valid;
|
|
|
|
const bool outdated;
|
|
|
|
};
|
|
|
|
|
|
|
|
FindResult findCellIdx(const Key & id, const CellMetadata::time_point_t now) const;
|
|
|
|
|
|
|
|
template <typename AncestorType>
|
|
|
|
void isInImpl(
|
|
|
|
const PaddedPODArray<Key> & child_ids,
|
|
|
|
const AncestorType & ancestor_ids,
|
|
|
|
PaddedPODArray<UInt8> & out) const;
|
|
|
|
|
|
|
|
const std::string name;
|
|
|
|
const DictionaryStructure dict_struct;
|
|
|
|
const DictionarySourcePtr source_ptr;
|
|
|
|
const DictionaryLifetime dict_lifetime;
|
|
|
|
|
2017-07-28 17:34:02 +00:00
|
|
|
mutable std::shared_mutex rw_lock;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Actual size will be increased to match power of 2
|
2017-07-21 06:35:58 +00:00
|
|
|
const size_t size;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// all bits to 1 mask (size - 1) (0b1000 - 1 = 0b111)
|
2017-07-21 06:35:58 +00:00
|
|
|
const size_t size_overlap_mask;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Max tries to find cell, overlaped with mask: if size = 16 and start_cell=10: will try cells: 10,11,12,13,14,15,0,1,2,3
|
2017-07-21 06:35:58 +00:00
|
|
|
static constexpr size_t max_collision_length = 10;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-09-01 17:21:03 +00:00
|
|
|
const size_t zero_cell_idx{getCellIdx(0)};
|
2017-07-21 06:35:58 +00:00
|
|
|
std::map<std::string, size_t> attribute_index_by_name;
|
2017-04-01 07:20:54 +00:00
|
|
|
mutable std::vector<Attribute> attributes;
|
|
|
|
mutable std::vector<CellMetadata> cells;
|
|
|
|
Attribute * hierarchical_attribute = nullptr;
|
|
|
|
std::unique_ptr<ArenaWithFreeLists> string_arena;
|
|
|
|
|
2017-09-09 23:17:38 +00:00
|
|
|
mutable pcg64 rnd_engine;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
mutable size_t bytes_allocated = 0;
|
|
|
|
mutable std::atomic<size_t> element_count{0};
|
|
|
|
mutable std::atomic<size_t> hit_count{0};
|
|
|
|
mutable std::atomic<size_t> query_count{0};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const std::chrono::time_point<std::chrono::system_clock> creation_time = std::chrono::system_clock::now();
|
2015-02-26 14:53:33 +00:00
|
|
|
};
|
2015-02-19 14:51:39 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
}
|