| 1 | //===----------------------------------------------------------------------===// |
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/storage/storage_manager.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/common/helper.hpp" |
| 12 | #include "duckdb/storage/buffer_manager.hpp" |
| 13 | #include "duckdb/storage/data_table.hpp" |
| 14 | #include "duckdb/storage/table_io_manager.hpp" |
| 15 | #include "duckdb/storage/write_ahead_log.hpp" |
| 16 | #include "duckdb/storage/database_size.hpp" |
| 17 | |
| 18 | namespace duckdb { |
| 19 | class BlockManager; |
| 20 | class Catalog; |
| 21 | class CheckpointWriter; |
| 22 | class DatabaseInstance; |
| 23 | class TransactionManager; |
| 24 | class TableCatalogEntry; |
| 25 | |
| 26 | class StorageCommitState { |
| 27 | public: |
| 28 | // Destruction of this object, without prior call to FlushCommit, |
| 29 | // will roll back the committed changes. |
| 30 | virtual ~StorageCommitState() { |
| 31 | } |
| 32 | |
| 33 | // Make the commit persistent |
| 34 | virtual void FlushCommit() = 0; |
| 35 | }; |
| 36 | |
| 37 | //! StorageManager is responsible for managing the physical storage of the |
| 38 | //! database on disk |
| 39 | class StorageManager { |
| 40 | public: |
| 41 | StorageManager(AttachedDatabase &db, string path, bool read_only); |
| 42 | virtual ~StorageManager(); |
| 43 | |
| 44 | public: |
| 45 | static StorageManager &Get(AttachedDatabase &db); |
| 46 | static StorageManager &Get(Catalog &catalog); |
| 47 | |
| 48 | //! Initialize a database or load an existing database from the given path |
| 49 | void Initialize(); |
| 50 | |
| 51 | DatabaseInstance &GetDatabase(); |
| 52 | AttachedDatabase &GetAttached() { |
| 53 | return db; |
| 54 | } |
| 55 | |
| 56 | //! Get the WAL of the StorageManager, returns nullptr if in-memory |
| 57 | optional_ptr<WriteAheadLog> GetWriteAheadLog() { |
| 58 | return wal.get(); |
| 59 | } |
| 60 | |
| 61 | string GetDBPath() { |
| 62 | return path; |
| 63 | } |
| 64 | bool InMemory(); |
| 65 | |
| 66 | virtual bool AutomaticCheckpoint(idx_t estimated_wal_bytes) = 0; |
| 67 | virtual unique_ptr<StorageCommitState> GenStorageCommitState(Transaction &transaction, bool checkpoint) = 0; |
| 68 | virtual bool IsCheckpointClean(block_id_t checkpoint_id) = 0; |
| 69 | virtual void CreateCheckpoint(bool delete_wal = false, bool force_checkpoint = false) = 0; |
| 70 | virtual DatabaseSize GetDatabaseSize() = 0; |
| 71 | virtual shared_ptr<TableIOManager> GetTableIOManager(BoundCreateTableInfo *info) = 0; |
| 72 | |
| 73 | protected: |
| 74 | virtual void LoadDatabase() = 0; |
| 75 | |
| 76 | protected: |
| 77 | //! The database this storagemanager belongs to |
| 78 | AttachedDatabase &db; |
| 79 | //! The path of the database |
| 80 | string path; |
| 81 | //! The WriteAheadLog of the storage manager |
| 82 | unique_ptr<WriteAheadLog> wal; |
| 83 | //! Whether or not the database is opened in read-only mode |
| 84 | bool read_only; |
| 85 | |
| 86 | public: |
| 87 | template <class TARGET> |
| 88 | TARGET &Cast() { |
| 89 | D_ASSERT(dynamic_cast<TARGET *>(this)); |
| 90 | return reinterpret_cast<TARGET &>(*this); |
| 91 | } |
| 92 | template <class TARGET> |
| 93 | const TARGET &Cast() const { |
| 94 | D_ASSERT(dynamic_cast<const TARGET *>(this)); |
| 95 | return reinterpret_cast<const TARGET &>(*this); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | //! Stores database in a single file. |
| 100 | class SingleFileStorageManager : public StorageManager { |
| 101 | public: |
| 102 | SingleFileStorageManager(AttachedDatabase &db, string path, bool read_only); |
| 103 | |
| 104 | //! The BlockManager to read/store meta information and data in blocks |
| 105 | unique_ptr<BlockManager> block_manager; |
| 106 | //! TableIoManager |
| 107 | unique_ptr<TableIOManager> table_io_manager; |
| 108 | |
| 109 | public: |
| 110 | bool AutomaticCheckpoint(idx_t estimated_wal_bytes) override; |
| 111 | unique_ptr<StorageCommitState> GenStorageCommitState(Transaction &transaction, bool checkpoint) override; |
| 112 | bool IsCheckpointClean(block_id_t checkpoint_id) override; |
| 113 | void CreateCheckpoint(bool delete_wal, bool force_checkpoint) override; |
| 114 | DatabaseSize GetDatabaseSize() override; |
| 115 | shared_ptr<TableIOManager> GetTableIOManager(BoundCreateTableInfo *info) override; |
| 116 | |
| 117 | protected: |
| 118 | void LoadDatabase() override; |
| 119 | }; |
| 120 | } // namespace duckdb |
| 121 | |