ClickHouse/dbms/src/Databases/DatabaseLazy.h

138 lines
3.8 KiB
C++
Raw Normal View History

2019-09-25 09:35:56 +00:00
#pragma once
#include <Databases/DatabaseOnDisk.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
*/
class DatabaseLazy : public DatabaseOnDisk
2019-09-25 09:35:56 +00:00
{
//TODO rewrite it all
2019-09-25 09:35:56 +00:00
public:
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;
void removeTable(
const Context & context,
const String & table_name) override;
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;
time_t getObjectMetadataModificationTime(const String & table_name) const override;
2019-09-25 09:35:56 +00:00
bool isTableExist(
const Context & context,
const String & table_name) const override;
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;
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;
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-11-07 14:52:12 +00:00
CachedTable() = delete;
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 07:16:31 +00:00
const time_t expiration_time;
2019-09-25 12:08:41 +00:00
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-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
};
}