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:
|
|
|
|
struct Impl;
|
|
|
|
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;
|
|
|
|
|
|
|
|
template <typename T> void set(const Key & key, const T & value);
|
|
|
|
|
|
|
|
NamedCollectionPtr duplicate() const;
|
|
|
|
|
|
|
|
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-09 13:08:16 +00:00
|
|
|
void initialize(const Poco::Util::AbstractConfiguration & server_config);
|
|
|
|
|
|
|
|
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-09 13:08:16 +00:00
|
|
|
mutable NamedCollections named_collections;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
bool is_initialized = false;
|
|
|
|
mutable std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|