| 1 | #pragma once |
| 2 | |
| 3 | #include <ext/shared_ptr_helper.h> |
| 4 | |
| 5 | #include <Storages/IStorage.h> |
| 6 | |
| 7 | |
| 8 | namespace DB |
| 9 | { |
| 10 | |
| 11 | class Set; |
| 12 | using SetPtr = std::shared_ptr<Set>; |
| 13 | |
| 14 | |
| 15 | /** Common part of StorageSet and StorageJoin. |
| 16 | */ |
| 17 | class StorageSetOrJoinBase : public IStorage |
| 18 | { |
| 19 | friend class SetOrJoinBlockOutputStream; |
| 20 | |
| 21 | public: |
| 22 | String getTableName() const override { return table_name; } |
| 23 | String getDatabaseName() const override { return database_name; } |
| 24 | |
| 25 | void rename(const String & new_path_to_table_data, const String & new_database_name, const String & new_table_name, TableStructureWriteLockHolder &) override; |
| 26 | |
| 27 | BlockOutputStreamPtr write(const ASTPtr & query, const Context & context) override; |
| 28 | |
| 29 | Strings getDataPaths() const override { return {path}; } |
| 30 | |
| 31 | protected: |
| 32 | StorageSetOrJoinBase( |
| 33 | const String & relative_path_, |
| 34 | const String & database_name_, |
| 35 | const String & table_name_, |
| 36 | const ColumnsDescription & columns_, |
| 37 | const ConstraintsDescription & constraints_, |
| 38 | const Context & context_); |
| 39 | |
| 40 | String base_path; |
| 41 | String path; |
| 42 | String table_name; |
| 43 | String database_name; |
| 44 | |
| 45 | std::atomic<UInt64> increment = 0; /// For the backup file names. |
| 46 | |
| 47 | /// Restore from backup. |
| 48 | void restore(); |
| 49 | |
| 50 | private: |
| 51 | void restoreFromFile(const String & file_path); |
| 52 | |
| 53 | /// Insert the block into the state. |
| 54 | virtual void insertBlock(const Block & block) = 0; |
| 55 | /// Call after all blocks were inserted. |
| 56 | virtual void finishInsert() = 0; |
| 57 | virtual size_t getSize() const = 0; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | /** Lets you save the set for later use on the right side of the IN statement. |
| 62 | * When inserted into a table, the data will be inserted into the set, |
| 63 | * and also written to a file-backup, for recovery after a restart. |
| 64 | * Reading from the table is not possible directly - it is possible to specify only the right part of the IN statement. |
| 65 | */ |
| 66 | class StorageSet : public ext::shared_ptr_helper<StorageSet>, public StorageSetOrJoinBase |
| 67 | { |
| 68 | friend struct ext::shared_ptr_helper<StorageSet>; |
| 69 | |
| 70 | public: |
| 71 | String getName() const override { return "Set" ; } |
| 72 | |
| 73 | /// Access the insides. |
| 74 | SetPtr & getSet() { return set; } |
| 75 | |
| 76 | void truncate(const ASTPtr &, const Context &, TableStructureWriteLockHolder &) override; |
| 77 | |
| 78 | private: |
| 79 | SetPtr set; |
| 80 | |
| 81 | void insertBlock(const Block & block) override; |
| 82 | void finishInsert() override; |
| 83 | size_t getSize() const override; |
| 84 | |
| 85 | protected: |
| 86 | StorageSet( |
| 87 | const String & relative_path_, |
| 88 | const String & database_name_, |
| 89 | const String & table_name_, |
| 90 | const ColumnsDescription & columns_, |
| 91 | const ConstraintsDescription & constraints_, |
| 92 | const Context & context_); |
| 93 | }; |
| 94 | |
| 95 | } |
| 96 | |