2015-11-16 17:27:12 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/Dictionaries/IDictionary.h>
|
|
|
|
|
#include <DB/Dictionaries/IDictionarySource.h>
|
|
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
2015-11-17 16:08:31 +00:00
|
|
|
|
#include <DB/Common/Arena.h>
|
|
|
|
|
#include <DB/Common/ArenaWithFreeLists.h>
|
2015-11-18 11:53:01 +00:00
|
|
|
|
#include <DB/Common/SmallObjectPool.h>
|
2015-11-16 17:27:12 +00:00
|
|
|
|
#include <DB/Common/HashTable/HashMap.h>
|
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
|
|
|
|
#include <DB/Core/StringRef.h>
|
|
|
|
|
#include <ext/scope_guard.hpp>
|
|
|
|
|
#include <ext/bit_cast.hpp>
|
|
|
|
|
#include <ext/map.hpp>
|
|
|
|
|
#include <Poco/RWLock.h>
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <tuple>
|
2016-06-15 12:43:36 +00:00
|
|
|
|
#include <random>
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2017-01-25 18:40:44 +00:00
|
|
|
|
namespace ProfileEvents
|
|
|
|
|
{
|
|
|
|
|
extern const Event DictCacheKeysRequested;
|
|
|
|
|
extern const Event DictCacheKeysRequestedMiss;
|
|
|
|
|
extern const Event DictCacheKeysRequestedFound;
|
|
|
|
|
extern const Event DictCacheKeysExpired;
|
|
|
|
|
extern const Event DictCacheKeysNotFound;
|
|
|
|
|
extern const Event DictCacheKeysHit;
|
|
|
|
|
extern const Event DictCacheRequestTimeNs;
|
|
|
|
|
extern const Event DictCacheLockWriteNs;
|
|
|
|
|
extern const Event DictCacheLockReadNs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
|
{
|
|
|
|
|
extern const Metric DictCacheRequests;
|
|
|
|
|
}
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
|
|
2015-11-16 17:27:12 +00:00
|
|
|
|
class ComplexKeyCacheDictionary final : public IDictionaryBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ComplexKeyCacheDictionary(const std::string & name, const DictionaryStructure & dict_struct,
|
|
|
|
|
DictionarySourcePtr source_ptr, const DictionaryLifetime dict_lifetime,
|
2016-06-07 21:07:44 +00:00
|
|
|
|
const std::size_t size);
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
|
ComplexKeyCacheDictionary(const ComplexKeyCacheDictionary & other);
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
|
|
|
|
std::string getKeyDescription() const { return key_description; };
|
|
|
|
|
|
|
|
|
|
std::exception_ptr getCreationException() const override { return {}; }
|
|
|
|
|
|
|
|
|
|
std::string getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
std::string getTypeName() const override { return "ComplexKeyCache"; }
|
|
|
|
|
|
2015-11-18 11:53:01 +00:00
|
|
|
|
std::size_t getBytesAllocated() const override
|
|
|
|
|
{
|
2015-11-24 12:47:51 +00:00
|
|
|
|
return bytes_allocated + (key_size_is_fixed ? fixed_size_keys_pool->size() : keys_pool->size()) +
|
|
|
|
|
(string_arena ? string_arena->size() : 0);
|
2015-11-18 11:53:01 +00:00
|
|
|
|
}
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
|
|
|
|
std::size_t getQueryCount() const override { return query_count.load(std::memory_order_relaxed); }
|
|
|
|
|
|
|
|
|
|
double getHitRate() const override
|
|
|
|
|
{
|
|
|
|
|
return static_cast<double>(hit_count.load(std::memory_order_acquire)) /
|
|
|
|
|
query_count.load(std::memory_order_relaxed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::size_t getElementCount() const override { return element_count.load(std::memory_order_relaxed); }
|
|
|
|
|
|
|
|
|
|
double getLoadFactor() const override
|
|
|
|
|
{
|
|
|
|
|
return static_cast<double>(element_count.load(std::memory_order_relaxed)) / size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isCached() const override { return true; }
|
|
|
|
|
|
|
|
|
|
DictionaryPtr clone() const override { return std::make_unique<ComplexKeyCacheDictionary>(*this); }
|
|
|
|
|
|
|
|
|
|
const IDictionarySource * getSource() const override { return source_ptr.get(); }
|
|
|
|
|
|
|
|
|
|
const DictionaryLifetime & getLifetime() const override { return dict_lifetime; }
|
|
|
|
|
|
|
|
|
|
const DictionaryStructure & getStructure() const override { return dict_struct; }
|
|
|
|
|
|
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> getCreationTime() const override
|
|
|
|
|
{
|
|
|
|
|
return creation_time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isInjective(const std::string & attribute_name) const override
|
|
|
|
|
{
|
|
|
|
|
return dict_struct.attributes[&getAttribute(attribute_name) - attributes.data()].injective;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 01:54:58 +00:00
|
|
|
|
/// Во всех функциях ниже, key_columns должны быть полноценными (не константными) столбцами.
|
|
|
|
|
/// См. требование в IDataType.h для функций текстовой сериализации.
|
2015-11-20 15:53:23 +00:00
|
|
|
|
#define DECLARE(TYPE)\
|
2015-11-16 17:27:12 +00:00
|
|
|
|
void get##TYPE(\
|
|
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,\
|
2016-06-07 21:07:44 +00:00
|
|
|
|
PaddedPODArray<TYPE> & out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
DECLARE(UInt8)
|
|
|
|
|
DECLARE(UInt16)
|
|
|
|
|
DECLARE(UInt32)
|
|
|
|
|
DECLARE(UInt64)
|
|
|
|
|
DECLARE(Int8)
|
|
|
|
|
DECLARE(Int16)
|
|
|
|
|
DECLARE(Int32)
|
|
|
|
|
DECLARE(Int64)
|
|
|
|
|
DECLARE(Float32)
|
|
|
|
|
DECLARE(Float64)
|
|
|
|
|
#undef DECLARE
|
2016-06-07 21:07:44 +00:00
|
|
|
|
|
2015-11-16 17:27:12 +00:00
|
|
|
|
void getString(
|
|
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,
|
2016-06-07 21:07:44 +00:00
|
|
|
|
ColumnString * out) const;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
|
#define DECLARE(TYPE)\
|
2015-11-16 17:27:12 +00:00
|
|
|
|
void get##TYPE(\
|
|
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,\
|
2016-06-07 21:07:44 +00:00
|
|
|
|
const PaddedPODArray<TYPE> & def, PaddedPODArray<TYPE> & out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
DECLARE(UInt8)
|
|
|
|
|
DECLARE(UInt16)
|
|
|
|
|
DECLARE(UInt32)
|
|
|
|
|
DECLARE(UInt64)
|
|
|
|
|
DECLARE(Int8)
|
|
|
|
|
DECLARE(Int16)
|
|
|
|
|
DECLARE(Int32)
|
|
|
|
|
DECLARE(Int64)
|
|
|
|
|
DECLARE(Float32)
|
|
|
|
|
DECLARE(Float64)
|
|
|
|
|
#undef DECLARE
|
2016-06-07 21:07:44 +00:00
|
|
|
|
|
2015-11-16 17:27:12 +00:00
|
|
|
|
void getString(
|
|
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,
|
2016-06-07 21:07:44 +00:00
|
|
|
|
const ColumnString * const def, ColumnString * const out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
|
|
|
|
|
#define DECLARE(TYPE)\
|
|
|
|
|
void get##TYPE(\
|
|
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,\
|
2016-06-07 21:07:44 +00:00
|
|
|
|
const TYPE def, PaddedPODArray<TYPE> & out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
DECLARE(UInt8)
|
|
|
|
|
DECLARE(UInt16)
|
|
|
|
|
DECLARE(UInt32)
|
|
|
|
|
DECLARE(UInt64)
|
|
|
|
|
DECLARE(Int8)
|
|
|
|
|
DECLARE(Int16)
|
|
|
|
|
DECLARE(Int32)
|
|
|
|
|
DECLARE(Int64)
|
|
|
|
|
DECLARE(Float32)
|
|
|
|
|
DECLARE(Float64)
|
|
|
|
|
#undef DECLARE
|
2016-06-07 21:07:44 +00:00
|
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
|
void getString(
|
|
|
|
|
const std::string & attribute_name, const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types,
|
2016-06-07 21:07:44 +00:00
|
|
|
|
const String & def, ColumnString * const out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
|
void has(const ConstColumnPlainPtrs & key_columns, const DataTypes & key_types, PaddedPODArray<UInt8> & out) const;
|
2015-11-19 13:15:02 +00:00
|
|
|
|
|
2015-11-16 17:27:12 +00:00
|
|
|
|
private:
|
|
|
|
|
template <typename Value> using MapType = HashMapWithSavedHash<StringRef, Value, StringRefHash>;
|
|
|
|
|
template <typename Value> using ContainerType = Value[];
|
|
|
|
|
template <typename Value> using ContainerPtrType = std::unique_ptr<ContainerType<Value>>;
|
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
|
struct CellMetadata final
|
2015-11-16 17:27:12 +00:00
|
|
|
|
{
|
|
|
|
|
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>;
|
|
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
|
static constexpr UInt64 EXPIRES_AT_MASK = std::numeric_limits<time_point_rep_t>::max();
|
|
|
|
|
static constexpr UInt64 IS_DEFAULT_MASK = ~EXPIRES_AT_MASK;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
|
|
|
|
StringRef key;
|
|
|
|
|
decltype(StringRefHash{}(key)) hash;
|
|
|
|
|
/// 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; }
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
|
struct Attribute final
|
2015-11-16 17:27:12 +00:00
|
|
|
|
{
|
|
|
|
|
AttributeUnderlyingType type;
|
|
|
|
|
std::tuple<
|
|
|
|
|
UInt8, UInt16, UInt32, UInt64,
|
|
|
|
|
Int8, Int16, Int32, Int64,
|
|
|
|
|
Float32, Float64,
|
|
|
|
|
String> null_values;
|
|
|
|
|
std::tuple<
|
|
|
|
|
ContainerPtrType<UInt8>, ContainerPtrType<UInt16>, ContainerPtrType<UInt32>, ContainerPtrType<UInt64>,
|
|
|
|
|
ContainerPtrType<Int8>, ContainerPtrType<Int16>, ContainerPtrType<Int32>, ContainerPtrType<Int64>,
|
|
|
|
|
ContainerPtrType<Float32>, ContainerPtrType<Float64>,
|
|
|
|
|
ContainerPtrType<StringRef>> arrays;
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
|
void createAttributes();
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
|
Attribute createAttributeWithType(const AttributeUnderlyingType type, const Field & null_value);
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2016-06-07 19:11:04 +00:00
|
|
|
|
template <typename OutputType, typename DefaultGetter>
|
|
|
|
|
void getItemsNumber(
|
2016-08-07 09:09:18 +00:00
|
|
|
|
Attribute & attribute,
|
2016-06-07 19:11:04 +00:00
|
|
|
|
const ConstColumnPlainPtrs & key_columns,
|
|
|
|
|
PaddedPODArray<OutputType> & out,
|
2016-06-07 21:07:44 +00:00
|
|
|
|
DefaultGetter && get_default) const;
|
2016-06-07 19:11:04 +00:00
|
|
|
|
|
|
|
|
|
template <typename AttributeType, typename OutputType, typename DefaultGetter>
|
|
|
|
|
void getItemsNumberImpl(
|
2016-08-07 09:09:18 +00:00
|
|
|
|
Attribute & attribute,
|
2016-06-07 19:11:04 +00:00
|
|
|
|
const ConstColumnPlainPtrs & key_columns,
|
|
|
|
|
PaddedPODArray<OutputType> & out,
|
2016-06-07 21:07:44 +00:00
|
|
|
|
DefaultGetter && get_default) const;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
|
template <typename DefaultGetter>
|
2016-06-07 19:11:04 +00:00
|
|
|
|
void getItemsString(
|
2016-08-07 09:09:18 +00:00
|
|
|
|
Attribute & attribute, const ConstColumnPlainPtrs & key_columns, ColumnString * out,
|
2016-06-07 21:07:44 +00:00
|
|
|
|
DefaultGetter && get_default) const;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
|
|
|
|
template <typename PresentKeyHandler, typename AbsentKeyHandler>
|
|
|
|
|
void update(
|
|
|
|
|
const ConstColumnPlainPtrs & in_key_columns, const PODArray<StringRef> & in_keys,
|
|
|
|
|
const std::vector<std::size_t> & in_requested_rows, PresentKeyHandler && on_cell_updated,
|
2016-06-07 21:07:44 +00:00
|
|
|
|
AbsentKeyHandler && on_key_not_found) const;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
|
UInt64 getCellIdx(const StringRef key) const;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
|
void setDefaultAttributeValue(Attribute & attribute, const std::size_t idx) const;
|
2015-11-18 11:53:01 +00:00
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
|
void setAttributeValue(Attribute & attribute, const std::size_t idx, const Field & value) const;
|
2015-11-18 11:53:01 +00:00
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
|
Attribute & getAttribute(const std::string & attribute_name) const;
|
2015-11-18 11:53:01 +00:00
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
|
StringRef allocKey(const std::size_t row, const ConstColumnPlainPtrs & key_columns, StringRefs & keys) const;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
|
void freeKey(const StringRef key) const;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2015-11-17 16:08:31 +00:00
|
|
|
|
template <typename Arena>
|
2015-11-16 17:27:12 +00:00
|
|
|
|
static StringRef placeKeysInPool(
|
2016-06-07 21:07:44 +00:00
|
|
|
|
const std::size_t row, const ConstColumnPlainPtrs & key_columns, StringRefs & keys, Arena & pool);
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2015-11-18 11:53:01 +00:00
|
|
|
|
StringRef placeKeysInFixedSizePool(
|
2016-06-07 21:07:44 +00:00
|
|
|
|
const std::size_t row, const ConstColumnPlainPtrs & key_columns) const;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2016-08-07 08:12:58 +00:00
|
|
|
|
static StringRef copyIntoArena(StringRef src, Arena & arena);
|
2016-06-07 21:07:44 +00:00
|
|
|
|
StringRef copyKey(const StringRef key) const;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2017-02-17 20:37:03 +00:00
|
|
|
|
struct FindResult
|
|
|
|
|
{
|
|
|
|
|
const size_t cell_idx;
|
|
|
|
|
const bool valid;
|
|
|
|
|
const bool outdated;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FindResult findCellIdx(const StringRef & key, const CellMetadata::time_point_t now, const size_t hash) const;
|
|
|
|
|
FindResult findCellIdx(const StringRef & key, const CellMetadata::time_point_t now) const
|
|
|
|
|
{
|
|
|
|
|
const auto hash = StringRefHash{}(key);
|
|
|
|
|
return findCellIdx(key, now, hash);
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-16 17:27:12 +00:00
|
|
|
|
const std::string name;
|
|
|
|
|
const DictionaryStructure dict_struct;
|
|
|
|
|
const DictionarySourcePtr source_ptr;
|
|
|
|
|
const DictionaryLifetime dict_lifetime;
|
2015-11-20 16:20:54 +00:00
|
|
|
|
const std::string key_description{dict_struct.getKeyDescription()};
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
|
|
|
|
mutable Poco::RWLock rw_lock;
|
2017-02-17 20:37:03 +00:00
|
|
|
|
|
|
|
|
|
/// Actual size will be increased to match power of 2
|
2015-11-16 17:27:12 +00:00
|
|
|
|
const std::size_t size;
|
2017-02-17 20:37:03 +00:00
|
|
|
|
|
|
|
|
|
/// all bits to 1 mask (size - 1) (0b1000 - 1 = 0b111)
|
|
|
|
|
const std::size_t size_overlap_mask;
|
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
static constexpr std::size_t max_collision_length = 10;
|
|
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
|
const UInt64 zero_cell_idx{getCellIdx(StringRef{})};
|
2015-11-16 17:27:12 +00:00
|
|
|
|
std::map<std::string, std::size_t> attribute_index_by_name;
|
2016-08-07 09:09:18 +00:00
|
|
|
|
mutable std::vector<Attribute> attributes;
|
|
|
|
|
mutable std::vector<CellMetadata> cells{size};
|
2015-11-18 11:53:01 +00:00
|
|
|
|
const bool key_size_is_fixed{dict_struct.isKeySizeFixed()};
|
|
|
|
|
std::size_t key_size{key_size_is_fixed ? dict_struct.getKeySize() : 0};
|
|
|
|
|
std::unique_ptr<ArenaWithFreeLists> keys_pool = key_size_is_fixed ? nullptr :
|
|
|
|
|
std::make_unique<ArenaWithFreeLists>();
|
|
|
|
|
std::unique_ptr<SmallObjectPool> fixed_size_keys_pool = key_size_is_fixed ?
|
|
|
|
|
std::make_unique<SmallObjectPool>(key_size) : nullptr;
|
2015-11-24 12:47:51 +00:00
|
|
|
|
std::unique_ptr<ArenaWithFreeLists> string_arena;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
|
mutable std::mt19937_64 rnd_engine;
|
2015-11-16 17:27:12 +00:00
|
|
|
|
|
|
|
|
|
mutable std::size_t bytes_allocated = 0;
|
|
|
|
|
mutable std::atomic<std::size_t> element_count{0};
|
|
|
|
|
mutable std::atomic<std::size_t> hit_count{0};
|
|
|
|
|
mutable std::atomic<std::size_t> query_count{0};
|
|
|
|
|
|
|
|
|
|
const std::chrono::time_point<std::chrono::system_clock> creation_time = std::chrono::system_clock::now();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|