1#pragma once
2
3#include <Databases/DatabaseOnDisk.h>
4#include <Interpreters/Context.h>
5#include <Parsers/ASTCreateQuery.h>
6
7
8namespace DB
9{
10
11
12class DatabaseLazyIterator;
13
14/** Lazy engine of databases.
15 * Works like DatabaseOrdinary, but stores in memory only cache.
16 * Can be used only with *Log engines.
17 */
18class DatabaseLazy : public DatabaseOnDisk
19{
20public:
21 DatabaseLazy(const String & name_, const String & metadata_path_, time_t expiration_time_, const Context & context_);
22
23 String getEngineName() const override { return "Lazy"; }
24
25 void loadStoredObjects(
26 Context & context,
27 bool has_force_restore_data_flag) override;
28
29 void createTable(
30 const Context & context,
31 const String & table_name,
32 const StoragePtr & table,
33 const ASTPtr & query) override;
34
35 void removeTable(
36 const Context & context,
37 const String & table_name) override;
38
39 void renameTable(
40 const Context & context,
41 const String & table_name,
42 IDatabase & to_database,
43 const String & to_table_name,
44 TableStructureWriteLockHolder &) override;
45
46 void alterTable(
47 const Context & context,
48 const String & name,
49 const StorageInMemoryMetadata & metadata) override;
50
51 time_t getObjectMetadataModificationTime(const String & table_name) const override;
52
53 bool isTableExist(
54 const Context & context,
55 const String & table_name) const override;
56
57 StoragePtr tryGetTable(
58 const Context & context,
59 const String & table_name) const override;
60
61 bool empty(const Context & context) const override;
62
63 DatabaseTablesIteratorPtr getTablesIterator(const Context & context, const FilterByNameFunction & filter_by_table_name = {}) override;
64
65 void attachTable(const String & table_name, const StoragePtr & table) override;
66
67 StoragePtr detachTable(const String & table_name) override;
68
69 void shutdown() override;
70
71 ~DatabaseLazy() override;
72
73private:
74 struct CacheExpirationQueueElement
75 {
76 time_t last_touched;
77 String table_name;
78
79 CacheExpirationQueueElement(time_t last_touched_, const String & table_name_)
80 : last_touched(last_touched_), table_name(table_name_) {}
81 };
82
83 using CacheExpirationQueue = std::list<CacheExpirationQueueElement>;
84
85
86 struct CachedTable
87 {
88 StoragePtr table;
89 time_t last_touched;
90 time_t metadata_modification_time;
91 CacheExpirationQueue::iterator expiration_iterator;
92
93 CachedTable() {}
94 CachedTable(const StoragePtr & table_, time_t last_touched_, time_t metadata_modification_time_)
95 : table(table_), last_touched(last_touched_), metadata_modification_time(metadata_modification_time_) {}
96 };
97
98 using TablesCache = std::unordered_map<String, CachedTable>;
99
100 const time_t expiration_time;
101
102 /// TODO use DatabaseWithOwnTablesBase::tables
103 mutable TablesCache tables_cache;
104 mutable CacheExpirationQueue cache_expiration_queue;
105
106 StoragePtr loadTable(const Context & context, const String & table_name) const;
107
108 void clearExpiredTables() const;
109
110 friend class DatabaseLazyIterator;
111};
112
113
114class DatabaseLazyIterator final : public IDatabaseTablesIterator
115{
116public:
117 DatabaseLazyIterator(
118 DatabaseLazy & database_,
119 const Context & context_,
120 Strings && table_names_);
121
122 void next() override;
123 bool isValid() const override;
124 const String & name() const override;
125 const StoragePtr & table() const override;
126
127private:
128 const DatabaseLazy & database;
129 const Strings table_names;
130 const Context context;
131 Strings::const_iterator iterator;
132 mutable StoragePtr current_storage;
133};
134}
135