2015-02-10 14:50:43 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Dictionaries/IDictionary.h>
|
|
|
|
#include <DB/Dictionaries/IDictionarySource.h>
|
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
2015-11-24 12:47:51 +00:00
|
|
|
#include <DB/Common/ArenaWithFreeLists.h>
|
2015-02-19 14:51:39 +00:00
|
|
|
#include <DB/Columns/ColumnString.h>
|
2015-11-06 14:49:54 +00:00
|
|
|
#include <ext/bit_cast.hpp>
|
2015-02-26 14:53:33 +00:00
|
|
|
#include <Poco/RWLock.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>
|
2016-05-23 00:40:28 +00:00
|
|
|
#include <random>
|
2015-02-10 14:50:43 +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 DictCacheRequests;
|
|
|
|
extern const Event DictCacheLockWriteNs;
|
|
|
|
extern const Event DictCacheLockReadNs;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric DictCacheRequests;
|
|
|
|
}
|
2015-11-20 15:53:23 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class CacheDictionary final : public IDictionary
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CacheDictionary(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-02-10 14:50:43 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
CacheDictionary(const CacheDictionary & other);
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2015-06-09 16:12:51 +00:00
|
|
|
std::exception_ptr getCreationException() const override { return {}; }
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
std::string getName() const override { return name; }
|
|
|
|
|
2015-03-24 12:26:19 +00:00
|
|
|
std::string getTypeName() const override { return "Cache"; }
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2015-11-24 12:47:51 +00:00
|
|
|
std::size_t getBytesAllocated() const override { return bytes_allocated + (string_arena ? string_arena->size() : 0); }
|
2015-03-24 11:30:16 +00:00
|
|
|
|
2015-05-08 12:31:00 +00:00
|
|
|
std::size_t getQueryCount() const override { return query_count.load(std::memory_order_relaxed); }
|
|
|
|
|
2015-03-24 17:02:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
bool isCached() const override { return true; }
|
|
|
|
|
2015-02-10 21:14:11 +00:00
|
|
|
DictionaryPtr clone() const override { return std::make_unique<CacheDictionary>(*this); }
|
2015-02-10 14:50:43 +00:00
|
|
|
|
|
|
|
const IDictionarySource * getSource() const override { return source_ptr.get(); }
|
|
|
|
|
|
|
|
const DictionaryLifetime & getLifetime() const override { return dict_lifetime; }
|
|
|
|
|
2015-03-24 13:59:19 +00:00
|
|
|
const DictionaryStructure & getStructure() const override { return dict_struct; }
|
|
|
|
|
2015-03-24 17:02:56 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> getCreationTime() const override
|
|
|
|
{
|
|
|
|
return creation_time;
|
|
|
|
}
|
|
|
|
|
2015-05-13 16:11:07 +00:00
|
|
|
bool isInjective(const std::string & attribute_name) const override
|
|
|
|
{
|
|
|
|
return dict_struct.attributes[&getAttribute(attribute_name) - attributes.data()].injective;
|
|
|
|
}
|
|
|
|
|
2015-02-27 11:57:14 +00:00
|
|
|
bool hasHierarchy() const override { return hierarchical_attribute; }
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void toParent(const PaddedPODArray<Key> & ids, PaddedPODArray<Key> & out) const override;
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2015-11-20 15:53:23 +00:00
|
|
|
#define DECLARE(TYPE)\
|
2016-08-07 09:09:18 +00:00
|
|
|
void get##TYPE(const std::string & attribute_name, const PaddedPODArray<Key> & ids, 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-08-07 09:09:18 +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)\
|
2015-11-06 14:49:54 +00:00
|
|
|
void get##TYPE(\
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const PaddedPODArray<TYPE> & def,\
|
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-06 14:49:54 +00:00
|
|
|
void getString(
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const ColumnString * const def,
|
2016-06-07 21:07:44 +00:00
|
|
|
ColumnString * const out) const;
|
2015-11-20 15:53:23 +00:00
|
|
|
|
|
|
|
#define DECLARE(TYPE)\
|
|
|
|
void get##TYPE(\
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, 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(
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::string & attribute_name, const PaddedPODArray<Key> & ids, const String & def,
|
2016-06-07 21:07:44 +00:00
|
|
|
ColumnString * const out) const;
|
2015-11-06 14:49:54 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void has(const PaddedPODArray<Key> & ids, PaddedPODArray<UInt8> & out) const override;
|
2015-11-19 13:15:02 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
private:
|
2015-11-19 13:15:02 +00:00
|
|
|
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-02-26 14:53:33 +00:00
|
|
|
{
|
2015-11-06 14:49:54 +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-06 14:49:54 +00:00
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
UInt64 id;
|
2015-11-06 14:49:54 +00:00
|
|
|
/// 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; }
|
2015-02-26 14:53:33 +00:00
|
|
|
};
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
struct Attribute final
|
2015-02-19 14:51:39 +00:00
|
|
|
{
|
2015-03-20 15:21:29 +00:00
|
|
|
AttributeUnderlyingType type;
|
2015-11-19 13:15:02 +00:00
|
|
|
std::tuple<
|
|
|
|
UInt8, UInt16, UInt32, UInt64,
|
2015-02-26 14:53:33 +00:00
|
|
|
Int8, Int16, Int32, Int64,
|
|
|
|
Float32, Float64,
|
|
|
|
String> null_values;
|
2015-11-19 13:15:02 +00:00
|
|
|
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;
|
2015-02-19 14:51:39 +00:00
|
|
|
};
|
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
void createAttributes();
|
2015-02-19 14:51:39 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
Attribute createAttributeWithType(const AttributeUnderlyingType type, const Field & null_value);
|
2015-02-10 14:50:43 +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,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
2016-06-07 19:11:04 +00:00
|
|
|
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,
|
|
|
|
const PaddedPODArray<Key> & ids,
|
2016-06-07 19:11:04 +00:00
|
|
|
PaddedPODArray<OutputType> & out,
|
2016-06-07 21:07:44 +00:00
|
|
|
DefaultGetter && get_default) const;
|
2015-02-26 14:53:33 +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 PaddedPODArray<Key> & ids,
|
2016-06-07 19:11:04 +00:00
|
|
|
ColumnString * out,
|
2016-06-07 21:07:44 +00:00
|
|
|
DefaultGetter && get_default) const;
|
2015-02-19 14:51:39 +00:00
|
|
|
|
2015-11-06 14:49:54 +00:00
|
|
|
template <typename PresentIdHandler, typename AbsentIdHandler>
|
|
|
|
void update(
|
2016-08-07 09:09:18 +00:00
|
|
|
const std::vector<Key> & requested_ids, PresentIdHandler && on_cell_updated,
|
2016-06-07 21:07:44 +00:00
|
|
|
AbsentIdHandler && on_id_not_found) const;
|
2015-02-19 14:51:39 +00:00
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
UInt64 getCellIdx(const Key id) const;
|
2015-03-06 16:13:59 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void setDefaultAttributeValue(Attribute & attribute, const Key idx) const;
|
2015-02-19 14:51:39 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
void setAttributeValue(Attribute & attribute, const Key idx, const Field & value) const;
|
2015-02-19 14:51:39 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
Attribute & getAttribute(const std::string & attribute_name) const;
|
2015-02-19 14:51:39 +00:00
|
|
|
|
|
|
|
const std::string name;
|
|
|
|
const DictionaryStructure dict_struct;
|
|
|
|
const DictionarySourcePtr source_ptr;
|
|
|
|
const DictionaryLifetime dict_lifetime;
|
|
|
|
|
2015-02-26 14:53:33 +00:00
|
|
|
mutable Poco::RWLock rw_lock;
|
2015-02-19 14:51:39 +00:00
|
|
|
const std::size_t size;
|
2016-10-26 22:27:38 +00:00
|
|
|
const UInt64 zero_cell_idx{getCellIdx(0)};
|
2015-02-19 14:51:39 +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;
|
|
|
|
Attribute * hierarchical_attribute = nullptr;
|
2015-11-24 12:47:51 +00:00
|
|
|
std::unique_ptr<ArenaWithFreeLists> string_arena;
|
2015-02-10 14:50:43 +00:00
|
|
|
|
2016-06-07 21:07:44 +00:00
|
|
|
mutable std::mt19937_64 rnd_engine;
|
2015-03-24 12:12:48 +00:00
|
|
|
|
2015-03-24 17:02:56 +00:00
|
|
|
mutable std::size_t bytes_allocated = 0;
|
2015-08-12 04:21:10 +00:00
|
|
|
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};
|
2015-03-24 17:02:56 +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
|
|
|
}
|