ClickHouse/src/Storages/NamedCollections.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

108 lines
2.6 KiB
C++
Raw Normal View History

#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.
* <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.
*/
class NamedCollectionFactory : boost::noncopyable
{
public:
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_);
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-10 18:58:07 +00:00
NamedCollectionPtr tryGet(const std::string & collection_name) const;
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;
private:
2022-11-10 18:58:07 +00:00
void assertInitialized(std::lock_guard<std::mutex> & lock) const;
NamedCollectionPtr getImpl(
const std::string & collection_name,
std::lock_guard<std::mutex> & lock) const;
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;
const Poco::Util::AbstractConfiguration * config;
bool is_initialized = false;
mutable std::mutex mutex;
};
}