1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/storage/checkpoint_manager.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/storage/partial_block_manager.hpp" |
12 | #include "duckdb/catalog/catalog_entry/index_catalog_entry.hpp" |
13 | #include "duckdb/catalog/catalog.hpp" |
14 | |
15 | namespace duckdb { |
16 | class DatabaseInstance; |
17 | class ClientContext; |
18 | class ColumnSegment; |
19 | class MetaBlockReader; |
20 | class SchemaCatalogEntry; |
21 | class SequenceCatalogEntry; |
22 | class TableCatalogEntry; |
23 | class ViewCatalogEntry; |
24 | class TypeCatalogEntry; |
25 | |
26 | class CheckpointWriter { |
27 | public: |
28 | explicit CheckpointWriter(AttachedDatabase &db) : db(db) { |
29 | } |
30 | virtual ~CheckpointWriter() { |
31 | } |
32 | |
33 | //! The database |
34 | AttachedDatabase &db; |
35 | |
36 | virtual MetaBlockWriter &GetMetaBlockWriter() = 0; |
37 | virtual unique_ptr<TableDataWriter> GetTableDataWriter(TableCatalogEntry &table) = 0; |
38 | |
39 | protected: |
40 | virtual void WriteSchema(SchemaCatalogEntry &schema); |
41 | virtual void WriteTable(TableCatalogEntry &table); |
42 | virtual void WriteView(ViewCatalogEntry &table); |
43 | virtual void WriteSequence(SequenceCatalogEntry &table); |
44 | virtual void WriteMacro(ScalarMacroCatalogEntry &table); |
45 | virtual void WriteTableMacro(TableMacroCatalogEntry &table); |
46 | virtual void WriteIndex(IndexCatalogEntry &index_catalog); |
47 | virtual void WriteType(TypeCatalogEntry &type); |
48 | }; |
49 | |
50 | class CheckpointReader { |
51 | public: |
52 | CheckpointReader(Catalog &catalog) : catalog(catalog) { |
53 | } |
54 | virtual ~CheckpointReader() { |
55 | } |
56 | |
57 | protected: |
58 | Catalog &catalog; |
59 | |
60 | protected: |
61 | virtual void LoadCheckpoint(ClientContext &context, MetaBlockReader &reader); |
62 | virtual void ReadSchema(ClientContext &context, MetaBlockReader &reader); |
63 | virtual void ReadTable(ClientContext &context, MetaBlockReader &reader); |
64 | virtual void ReadView(ClientContext &context, MetaBlockReader &reader); |
65 | virtual void ReadSequence(ClientContext &context, MetaBlockReader &reader); |
66 | virtual void ReadMacro(ClientContext &context, MetaBlockReader &reader); |
67 | virtual void ReadTableMacro(ClientContext &context, MetaBlockReader &reader); |
68 | virtual void ReadIndex(ClientContext &context, MetaBlockReader &reader); |
69 | virtual void ReadType(ClientContext &context, MetaBlockReader &reader); |
70 | |
71 | virtual void ReadTableData(ClientContext &context, MetaBlockReader &reader, BoundCreateTableInfo &bound_info); |
72 | }; |
73 | |
74 | class SingleFileCheckpointReader final : public CheckpointReader { |
75 | public: |
76 | explicit SingleFileCheckpointReader(SingleFileStorageManager &storage) |
77 | : CheckpointReader(Catalog::GetCatalog(db&: storage.GetAttached())), storage(storage) { |
78 | } |
79 | |
80 | void LoadFromStorage(); |
81 | |
82 | //! The database |
83 | SingleFileStorageManager &storage; |
84 | }; |
85 | |
86 | //! CheckpointWriter is responsible for checkpointing the database |
87 | class SingleFileRowGroupWriter; |
88 | class SingleFileTableDataWriter; |
89 | |
90 | class SingleFileCheckpointWriter final : public CheckpointWriter { |
91 | friend class SingleFileRowGroupWriter; |
92 | friend class SingleFileTableDataWriter; |
93 | |
94 | public: |
95 | SingleFileCheckpointWriter(AttachedDatabase &db, BlockManager &block_manager); |
96 | |
97 | //! Checkpoint the current state of the WAL and flush it to the main storage. This should be called BEFORE any |
98 | //! connection is available because right now the checkpointing cannot be done online. (TODO) |
99 | void CreateCheckpoint(); |
100 | |
101 | virtual MetaBlockWriter &GetMetaBlockWriter() override; |
102 | virtual unique_ptr<TableDataWriter> GetTableDataWriter(TableCatalogEntry &table) override; |
103 | |
104 | BlockManager &GetBlockManager(); |
105 | |
106 | private: |
107 | //! The metadata writer is responsible for writing schema information |
108 | unique_ptr<MetaBlockWriter> metadata_writer; |
109 | //! The table data writer is responsible for writing the DataPointers used by the table chunks |
110 | unique_ptr<MetaBlockWriter> table_metadata_writer; |
111 | //! Because this is single-file storage, we can share partial blocks across |
112 | //! an entire checkpoint. |
113 | PartialBlockManager partial_block_manager; |
114 | }; |
115 | |
116 | } // namespace duckdb |
117 | |