2022-11-09 13:08:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class NamedCollection;
|
|
|
|
using NamedCollectionPtr = std::shared_ptr<const NamedCollection>;
|
|
|
|
struct NamedCollectionValueInfo;
|
2022-11-10 15:32:15 +00:00
|
|
|
using NamedCollectionInfo = std::map<std::string, NamedCollectionValueInfo>;
|
2022-11-09 13:08:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A factory of immutable named collections.
|
|
|
|
* Named collections are defined in server config as arbitrary
|
|
|
|
* structure configurations:
|
|
|
|
* <named_collections>
|
|
|
|
* <collection1>
|
|
|
|
* ...
|
|
|
|
* </collection1>
|
|
|
|
* ...
|
|
|
|
* </named_collections>
|
|
|
|
* In order to get a named collection, you need to know it's name
|
|
|
|
* and expected structure of the collection defined as NamedCollectionInfo.
|
|
|
|
*/
|
|
|
|
class NamedCollectionFactory : boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
2022-11-10 15:32:15 +00:00
|
|
|
static NamedCollectionFactory & instance();
|
|
|
|
|
2022-11-09 13:08:16 +00:00
|
|
|
void initialize(const Poco::Util::AbstractConfiguration & server_config);
|
|
|
|
|
|
|
|
bool exists(const std::string & collection_name) const;
|
|
|
|
|
|
|
|
NamedCollectionPtr get(
|
|
|
|
const std::string & collection_name,
|
|
|
|
const NamedCollectionInfo & collection_info) const;
|
|
|
|
|
|
|
|
NamedCollectionPtr tryGet(
|
|
|
|
const std::string & collection_name,
|
|
|
|
const NamedCollectionInfo & collection_info) const;
|
|
|
|
|
2022-11-10 15:32:15 +00:00
|
|
|
void add(
|
|
|
|
const std::string & collection_name,
|
|
|
|
NamedCollectionPtr collection);
|
|
|
|
|
|
|
|
void remove(const std::string & collection_name);
|
|
|
|
|
|
|
|
using NamedCollections = std::unordered_map<std::string, NamedCollectionPtr>;
|
|
|
|
NamedCollections getAll() const;
|
2022-11-09 13:08:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
NamedCollectionPtr getImpl(
|
|
|
|
const std::string & collection_name,
|
|
|
|
const NamedCollectionInfo & collection_info,
|
|
|
|
std::lock_guard<std::mutex> & lock) const;
|
|
|
|
|
2022-11-10 15:32:15 +00:00
|
|
|
bool existsUnlocked(
|
|
|
|
const std::string & collection_name,
|
|
|
|
std::lock_guard<std::mutex> & lock) const;
|
|
|
|
|
2022-11-09 13:08:16 +00:00
|
|
|
mutable NamedCollections named_collections;
|
|
|
|
|
|
|
|
private:
|
2022-11-10 15:32:15 +00:00
|
|
|
/// FIXME: this will be invalid when config is reloaded
|
2022-11-09 13:08:16 +00:00
|
|
|
const Poco::Util::AbstractConfiguration * config;
|
|
|
|
|
|
|
|
void assertInitialized(std::lock_guard<std::mutex> & lock) const;
|
|
|
|
|
|
|
|
bool is_initialized = false;
|
|
|
|
mutable std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class NamedCollection
|
|
|
|
{
|
|
|
|
friend class NamedCollectionFactory;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
|
|
using ImplPtr = std::unique_ptr<Impl>;
|
|
|
|
|
|
|
|
ImplPtr pimpl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using Key = std::string;
|
|
|
|
using Value = Field;
|
2022-11-10 15:32:15 +00:00
|
|
|
using ValueInfo = NamedCollectionValueInfo;
|
|
|
|
using CollectionInfo = NamedCollectionInfo;
|
2022-11-09 13:08:16 +00:00
|
|
|
|
2022-11-10 15:32:15 +00:00
|
|
|
NamedCollection(
|
2022-11-09 13:08:16 +00:00
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
2022-11-10 15:32:15 +00:00
|
|
|
const std::string & collection_path,
|
|
|
|
const CollectionInfo & collection_info);
|
2022-11-09 13:08:16 +00:00
|
|
|
|
2022-11-10 15:32:15 +00:00
|
|
|
Value get(const Key & key) const;
|
|
|
|
|
2022-11-10 16:07:17 +00:00
|
|
|
std::map<Key, Value> dumpStructure() const;
|
|
|
|
|
|
|
|
/// Get a string representation of the collection structure.
|
|
|
|
/// Used for debugging and tests.
|
2022-11-10 15:32:15 +00:00
|
|
|
std::string toString() const;
|
2022-11-09 13:08:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Named collection info which allows to parse config.
|
|
|
|
* Contains a mapping key_path -> value_info.
|
|
|
|
*/
|
|
|
|
struct NamedCollectionValueInfo
|
|
|
|
{
|
|
|
|
/// Type of the value. One of: String, UInt64, Int64, Double.
|
2022-11-10 15:32:15 +00:00
|
|
|
using Type = Field::Types::Which;
|
|
|
|
Type type = Type::String;
|
2022-11-09 13:08:16 +00:00
|
|
|
/// Optional default value for the case if there is no such key in config.
|
|
|
|
std::optional<NamedCollection::Value> default_value;
|
|
|
|
/// Is this value required or optional? Throw exception if the value is
|
|
|
|
/// required, but is not specified in config.
|
|
|
|
bool is_required = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|