ClickHouse/dbms/src/Databases/DatabaseLazy.h

191 lines
5.3 KiB
C++
Raw Normal View History

2019-09-25 09:35:56 +00:00
#pragma once
#include <Databases/DatabasesCommon.h>
2019-10-03 09:31:59 +00:00
#include <Interpreters/Context.h>
2019-10-10 17:33:01 +00:00
#include <Parsers/ASTCreateQuery.h>
2019-09-25 09:35:56 +00:00
namespace DB
{
2019-10-10 17:33:01 +00:00
2019-09-26 09:20:46 +00:00
class DatabaseLazyIterator;
2019-09-25 09:35:56 +00:00
/** Lazy engine of databases.
2019-09-25 12:08:41 +00:00
* Works like DatabaseOrdinary, but stores in memory only cache.
* Can be used only with *Log engines.
2019-09-25 09:35:56 +00:00
*/
2019-09-26 07:16:31 +00:00
class DatabaseLazy : public IDatabase
2019-09-25 09:35:56 +00:00
{
public:
2019-09-26 07:16:31 +00:00
DatabaseLazy(const String & name_, const String & metadata_path_, time_t expiration_time_, const Context & context);
2019-09-25 09:35:56 +00:00
String getEngineName() const override { return "Lazy"; }
2019-10-10 17:33:01 +00:00
void loadStoredObjects(
2019-09-25 09:35:56 +00:00
Context & context,
bool has_force_restore_data_flag) override;
void createTable(
const Context & context,
const String & table_name,
const StoragePtr & table,
const ASTPtr & query) override;
2019-10-10 17:33:01 +00:00
void createDictionary(
const Context & context,
const String & dictionary_name,
const ASTPtr & query) override;
2019-09-25 09:35:56 +00:00
void removeTable(
const Context & context,
const String & table_name) override;
2019-10-10 17:33:01 +00:00
void removeDictionary(
const Context & context,
const String & table_name) override;
2019-09-25 09:35:56 +00:00
void renameTable(
const Context & context,
const String & table_name,
IDatabase & to_database,
const String & to_table_name,
TableStructureWriteLockHolder &) override;
void alterTable(
const Context & context,
const String & name,
const ColumnsDescription & columns,
const IndicesDescription & indices,
const ConstraintsDescription & constraints,
const ASTModifier & engine_modifier) override;
2019-10-10 17:33:01 +00:00
time_t getObjectMetadataModificationTime(
2019-09-25 09:35:56 +00:00
const Context & context,
const String & table_name) override;
ASTPtr getCreateTableQuery(
const Context & context,
const String & table_name) const override;
ASTPtr tryGetCreateTableQuery(
const Context & context,
const String & table_name) const override;
2019-10-10 17:33:01 +00:00
ASTPtr getCreateDictionaryQuery(
const Context & context,
2019-10-11 13:51:31 +00:00
const String & dictionary_name) const override;
2019-10-10 17:33:01 +00:00
ASTPtr tryGetCreateDictionaryQuery(
const Context & context,
2019-10-11 13:51:31 +00:00
const String & dictionary_name) const override;
2019-10-10 17:33:01 +00:00
2019-09-25 09:35:56 +00:00
ASTPtr getCreateDatabaseQuery(const Context & context) const override;
String getDataPath() const override;
String getDatabaseName() const override;
String getMetadataPath() const override;
2019-10-10 17:33:01 +00:00
String getObjectMetadataPath(const String & table_name) const override;
2019-09-25 09:35:56 +00:00
void drop() override;
bool isTableExist(
const Context & context,
const String & table_name) const override;
2019-10-10 17:33:01 +00:00
bool isDictionaryExist(
const Context & context,
const String & table_name) const override;
2019-09-25 09:35:56 +00:00
StoragePtr tryGetTable(
const Context & context,
const String & table_name) const override;
bool empty(const Context & context) const override;
2019-10-10 17:33:01 +00:00
DatabaseTablesIteratorPtr getTablesIterator(const Context & context, const FilterByNameFunction & filter_by_table_name = {}) override;
DatabaseDictionariesIteratorPtr getDictionariesIterator(const Context & context, const FilterByNameFunction & filter_by_dictionary_name = {}) override;
2019-09-25 09:35:56 +00:00
2019-09-26 07:16:31 +00:00
void attachTable(const String & table_name, const StoragePtr & table) override;
StoragePtr detachTable(const String & table_name) override;
2019-10-16 14:59:52 +00:00
void attachDictionary(const String & dictionary_name) override;
2019-10-10 17:33:01 +00:00
2019-10-16 14:59:52 +00:00
void detachDictionary(const String & dictionary_name) override;
2019-10-10 17:33:01 +00:00
2019-09-26 07:16:31 +00:00
void shutdown() override;
~DatabaseLazy() override;
2019-09-25 09:35:56 +00:00
private:
2019-10-03 09:20:21 +00:00
struct CacheExpirationQueueElement
{
time_t last_touched;
String table_name;
2019-10-08 08:10:55 +00:00
CacheExpirationQueueElement(time_t last_touched_, const String & table_name_)
2019-10-03 09:20:21 +00:00
: last_touched(last_touched_), table_name(table_name_) {}
};
using CacheExpirationQueue = std::list<CacheExpirationQueueElement>;
2019-09-26 09:23:04 +00:00
struct CachedTable
{
StoragePtr table;
time_t last_touched;
time_t metadata_modification_time;
2019-10-03 09:20:21 +00:00
CacheExpirationQueue::iterator expiration_iterator;
2019-09-26 09:23:04 +00:00
2019-09-26 12:34:58 +00:00
CachedTable() {}
2019-09-26 09:23:04 +00:00
CachedTable(const StoragePtr & table_, time_t last_touched_, time_t metadata_modification_time_)
: table(table_), last_touched(last_touched_), metadata_modification_time(metadata_modification_time_) {}
};
using TablesCache = std::unordered_map<String, CachedTable>;
2019-09-26 12:34:58 +00:00
2019-09-25 12:08:41 +00:00
String name;
2019-09-25 09:35:56 +00:00
const String metadata_path;
const String data_path;
2019-09-26 09:20:46 +00:00
2019-09-26 07:16:31 +00:00
const time_t expiration_time;
2019-09-25 12:08:41 +00:00
2019-09-26 07:16:31 +00:00
mutable std::mutex tables_mutex;
2019-09-26 09:20:46 +00:00
mutable TablesCache tables_cache;
2019-09-26 12:34:58 +00:00
mutable CacheExpirationQueue cache_expiration_queue;
2019-09-25 09:35:56 +00:00
Poco::Logger * log;
2019-10-05 10:21:04 +00:00
2019-09-26 09:20:46 +00:00
StoragePtr loadTable(const Context & context, const String & table_name) const;
2019-09-25 09:35:56 +00:00
2019-09-26 07:16:31 +00:00
void clearExpiredTables() const;
2019-09-25 09:35:56 +00:00
friend class DatabaseLazyIterator;
};
2019-10-10 17:33:01 +00:00
class DatabaseLazyIterator final : public IDatabaseTablesIterator
2019-09-25 09:35:56 +00:00
{
public:
2019-10-10 20:47:47 +00:00
DatabaseLazyIterator(
DatabaseLazy & database_,
const Context & context_,
Strings && table_names_);
2019-09-25 09:35:56 +00:00
void next() override;
bool isValid() const override;
const String & name() const override;
2019-09-26 07:16:31 +00:00
const StoragePtr & table() const override;
2019-09-25 09:35:56 +00:00
private:
2019-09-25 12:08:41 +00:00
const DatabaseLazy & database;
const Strings table_names;
const Context context;
Strings::const_iterator iterator;
2019-09-26 07:16:31 +00:00
mutable StoragePtr current_storage;
2019-09-25 09:35:56 +00:00
};
}