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>;
|
|
|
|
|
|
|
|
/**
|
2022-11-10 18:58:07 +00:00
|
|
|
* Class to represent arbitrary-structured named collection object.
|
|
|
|
* It can be defined via config or via SQL command.
|
2022-11-09 13:08:16 +00:00
|
|
|
* <named_collections>
|
|
|
|
* <collection1>
|
|
|
|
* ...
|
|
|
|
* </collection1>
|
|
|
|
* ...
|
|
|
|
* </named_collections>
|
2022-11-10 18:58:07 +00:00
|
|
|
*/
|
|
|
|
class NamedCollection
|
|
|
|
{
|
|
|
|
private:
|
2022-11-10 21:44:26 +00:00
|
|
|
class Impl;
|
2022-11-10 18:58:07 +00:00
|
|
|
using ImplPtr = std::unique_ptr<Impl>;
|
|
|
|
|
|
|
|
ImplPtr pimpl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using Key = std::string;
|
|
|
|
using Keys = std::set<Key>;
|
|
|
|
|
|
|
|
static NamedCollectionPtr create(
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const std::string & collection_name);
|
|
|
|
|
|
|
|
NamedCollection(
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const std::string & collection_path,
|
|
|
|
const Keys & keys);
|
|
|
|
|
|
|
|
explicit NamedCollection(ImplPtr pimpl_);
|
|
|
|
|
|
|
|
template <typename T> T get(const Key & key) const;
|
|
|
|
|
|
|
|
template <typename T> T getOrDefault(const Key & key, const T & default_value) const;
|
|
|
|
|
2022-11-10 21:44:26 +00:00
|
|
|
template <typename T> void set(const Key & key, const T & value, bool update_if_exists = false);
|
2022-11-10 18:58:07 +00:00
|
|
|
|
2022-11-10 21:44:26 +00:00
|
|
|
void remove(const Key & key);
|
|
|
|
|
|
|
|
std::shared_ptr<NamedCollection> duplicate() const;
|
2022-11-10 18:58:07 +00:00
|
|
|
|
|
|
|
Keys getKeys() const;
|
|
|
|
|
|
|
|
std::string dumpStructure() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A factory of immutable named collections.
|
2022-11-09 13:08:16 +00:00
|
|
|
*/
|
|
|
|
class NamedCollectionFactory : boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
2022-11-10 15:32:15 +00:00
|
|
|
static NamedCollectionFactory & instance();
|
|
|
|
|
2022-11-10 21:44:26 +00:00
|
|
|
void initialize(const Poco::Util::AbstractConfiguration & config_);
|
|
|
|
|
|
|
|
void reload(const Poco::Util::AbstractConfiguration & config_);
|
2022-11-09 13:08:16 +00:00
|
|
|
|
|
|
|
bool exists(const std::string & collection_name) const;
|
|
|
|
|
2022-11-10 18:58:07 +00:00
|
|
|
NamedCollectionPtr get(const std::string & collection_name) const;
|
2022-11-09 13:08:16 +00:00
|
|
|
|
2022-11-10 18:58:07 +00:00
|
|
|
NamedCollectionPtr tryGet(const std::string & collection_name) const;
|
2022-11-09 13:08:16 +00:00
|
|
|
|
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:
|
2022-11-10 18:58:07 +00:00
|
|
|
void assertInitialized(std::lock_guard<std::mutex> & lock) const;
|
|
|
|
|
2022-11-09 13:08:16 +00:00
|
|
|
NamedCollectionPtr getImpl(
|
|
|
|
const std::string & collection_name,
|
|
|
|
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-10 21:44:26 +00:00
|
|
|
mutable NamedCollections loaded_named_collections;
|
2022-11-09 13:08:16 +00:00
|
|
|
|
|
|
|
const Poco::Util::AbstractConfiguration * config;
|
|
|
|
|
|
|
|
bool is_initialized = false;
|
|
|
|
mutable std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|