1 | #include "duckdb/storage/write_ahead_log.hpp" |
---|---|
2 | #include "duckdb/main/database.hpp" |
3 | #include "duckdb/catalog/catalog_entry/schema_catalog_entry.hpp" |
4 | #include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp" |
5 | #include "duckdb/catalog/catalog_entry/view_catalog_entry.hpp" |
6 | #include "duckdb/parser/parsed_data/alter_table_info.hpp" |
7 | #include <cstring> |
8 | |
9 | using namespace duckdb; |
10 | using namespace std; |
11 | |
12 | WriteAheadLog::WriteAheadLog(DuckDB &database) : initialized(false), database(database) { |
13 | } |
14 | |
15 | void WriteAheadLog::Initialize(string &path) { |
16 | writer = make_unique<BufferedFileWriter>(*database.file_system, path.c_str(), true); |
17 | initialized = true; |
18 | } |
19 | |
20 | int64_t WriteAheadLog::GetWALSize() { |
21 | return writer->GetFileSize(); |
22 | } |
23 | |
24 | void WriteAheadLog::Truncate(int64_t size) { |
25 | writer->Truncate(size); |
26 | } |
27 | //===--------------------------------------------------------------------===// |
28 | // Write Entries |
29 | //===--------------------------------------------------------------------===// |
30 | //===--------------------------------------------------------------------===// |
31 | // CREATE TABLE |
32 | //===--------------------------------------------------------------------===// |
33 | void WriteAheadLog::WriteCreateTable(TableCatalogEntry *entry) { |
34 | writer->Write<WALType>(WALType::CREATE_TABLE); |
35 | entry->Serialize(*writer); |
36 | } |
37 | |
38 | //===--------------------------------------------------------------------===// |
39 | // DROP TABLE |
40 | //===--------------------------------------------------------------------===// |
41 | void WriteAheadLog::WriteDropTable(TableCatalogEntry *entry) { |
42 | writer->Write<WALType>(WALType::DROP_TABLE); |
43 | writer->WriteString(entry->schema->name); |
44 | writer->WriteString(entry->name); |
45 | } |
46 | |
47 | //===--------------------------------------------------------------------===// |
48 | // CREATE SCHEMA |
49 | //===--------------------------------------------------------------------===// |
50 | void WriteAheadLog::WriteCreateSchema(SchemaCatalogEntry *entry) { |
51 | writer->Write<WALType>(WALType::CREATE_SCHEMA); |
52 | writer->WriteString(entry->name); |
53 | } |
54 | |
55 | //===--------------------------------------------------------------------===// |
56 | // SEQUENCES |
57 | //===--------------------------------------------------------------------===// |
58 | void WriteAheadLog::WriteCreateSequence(SequenceCatalogEntry *entry) { |
59 | writer->Write<WALType>(WALType::CREATE_SEQUENCE); |
60 | entry->Serialize(*writer); |
61 | } |
62 | |
63 | void WriteAheadLog::WriteDropSequence(SequenceCatalogEntry *entry) { |
64 | writer->Write<WALType>(WALType::DROP_SEQUENCE); |
65 | writer->WriteString(entry->schema->name); |
66 | writer->WriteString(entry->name); |
67 | } |
68 | |
69 | void WriteAheadLog::WriteSequenceValue(SequenceCatalogEntry *entry, SequenceValue val) { |
70 | writer->Write<WALType>(WALType::SEQUENCE_VALUE); |
71 | writer->WriteString(entry->schema->name); |
72 | writer->WriteString(entry->name); |
73 | writer->Write<uint64_t>(val.usage_count); |
74 | writer->Write<int64_t>(val.counter); |
75 | } |
76 | |
77 | //===--------------------------------------------------------------------===// |
78 | // VIEWS |
79 | //===--------------------------------------------------------------------===// |
80 | void WriteAheadLog::WriteCreateView(ViewCatalogEntry *entry) { |
81 | writer->Write<WALType>(WALType::CREATE_VIEW); |
82 | entry->Serialize(*writer); |
83 | } |
84 | |
85 | void WriteAheadLog::WriteDropView(ViewCatalogEntry *entry) { |
86 | writer->Write<WALType>(WALType::DROP_VIEW); |
87 | writer->WriteString(entry->schema->name); |
88 | writer->WriteString(entry->name); |
89 | } |
90 | |
91 | //===--------------------------------------------------------------------===// |
92 | // DROP SCHEMA |
93 | //===--------------------------------------------------------------------===// |
94 | void WriteAheadLog::WriteDropSchema(SchemaCatalogEntry *entry) { |
95 | writer->Write<WALType>(WALType::DROP_SCHEMA); |
96 | writer->WriteString(entry->name); |
97 | } |
98 | |
99 | //===--------------------------------------------------------------------===// |
100 | // DATA |
101 | //===--------------------------------------------------------------------===// |
102 | void WriteAheadLog::WriteSetTable(string &schema, string &table) { |
103 | writer->Write<WALType>(WALType::USE_TABLE); |
104 | writer->WriteString(schema); |
105 | writer->WriteString(table); |
106 | } |
107 | |
108 | void WriteAheadLog::WriteInsert(DataChunk &chunk) { |
109 | assert(chunk.size() > 0); |
110 | chunk.Verify(); |
111 | |
112 | writer->Write<WALType>(WALType::INSERT_TUPLE); |
113 | chunk.Serialize(*writer); |
114 | } |
115 | |
116 | void WriteAheadLog::WriteDelete(DataChunk &chunk) { |
117 | assert(chunk.size() > 0); |
118 | assert(chunk.column_count() == 1 && chunk.data[0].type == ROW_TYPE); |
119 | chunk.Verify(); |
120 | |
121 | writer->Write<WALType>(WALType::DELETE_TUPLE); |
122 | chunk.Serialize(*writer); |
123 | } |
124 | |
125 | void WriteAheadLog::WriteUpdate(DataChunk &chunk, column_t col_idx) { |
126 | assert(chunk.size() > 0); |
127 | chunk.Verify(); |
128 | |
129 | writer->Write<WALType>(WALType::UPDATE_TUPLE); |
130 | writer->Write<column_t>(col_idx); |
131 | chunk.Serialize(*writer); |
132 | } |
133 | |
134 | //===--------------------------------------------------------------------===// |
135 | // Write ALTER Statement |
136 | //===--------------------------------------------------------------------===// |
137 | void WriteAheadLog::WriteAlter(AlterInfo &info) { |
138 | writer->Write<WALType>(WALType::ALTER_INFO); |
139 | info.Serialize(*writer); |
140 | } |
141 | |
142 | //===--------------------------------------------------------------------===// |
143 | // FLUSH |
144 | //===--------------------------------------------------------------------===// |
145 | void WriteAheadLog::Flush() { |
146 | // write an empty entry |
147 | writer->Write<WALType>(WALType::WAL_FLUSH); |
148 | // flushes all changes made to the WAL to disk |
149 | writer->Sync(); |
150 | } |
151 |