| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/storage/write_ahead_log.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/common/helper.hpp" |
| 12 | #include "duckdb/common/types/data_chunk.hpp" |
| 13 | #include "duckdb/common/enums/wal_type.hpp" |
| 14 | #include "duckdb/common/serializer/buffered_file_writer.hpp" |
| 15 | #include "duckdb/catalog/catalog_entry/sequence_catalog_entry.hpp" |
| 16 | |
| 17 | namespace duckdb { |
| 18 | |
| 19 | struct AlterInfo; |
| 20 | |
| 21 | class BufferedSerializer; |
| 22 | class Catalog; |
| 23 | class DuckDB; |
| 24 | class SchemaCatalogEntry; |
| 25 | class SequenceCatalogEntry; |
| 26 | class ViewCatalogEntry; |
| 27 | class TableCatalogEntry; |
| 28 | class Transaction; |
| 29 | class TransactionManager; |
| 30 | |
| 31 | //! The WriteAheadLog (WAL) is a log that is used to provide durability. Prior |
| 32 | //! to committing a transaction it writes the changes the transaction made to |
| 33 | //! the database to the log, which can then be replayed upon startup in case the |
| 34 | //! server crashes or is shut down. |
| 35 | class WriteAheadLog { |
| 36 | public: |
| 37 | WriteAheadLog(DuckDB &database); |
| 38 | |
| 39 | //! Whether or not the WAL has been initialized |
| 40 | bool initialized; |
| 41 | |
| 42 | public: |
| 43 | //! Replay the WAL |
| 44 | static void Replay(DuckDB &database, string &path); |
| 45 | |
| 46 | //! Initialize the WAL in the specified directory |
| 47 | void Initialize(string &path); |
| 48 | //! Returns the current size of the WAL in bytes |
| 49 | int64_t GetWALSize(); |
| 50 | |
| 51 | void WriteCreateTable(TableCatalogEntry *entry); |
| 52 | void WriteDropTable(TableCatalogEntry *entry); |
| 53 | |
| 54 | void WriteCreateSchema(SchemaCatalogEntry *entry); |
| 55 | void WriteDropSchema(SchemaCatalogEntry *entry); |
| 56 | |
| 57 | void WriteCreateView(ViewCatalogEntry *entry); |
| 58 | void WriteDropView(ViewCatalogEntry *entry); |
| 59 | |
| 60 | void WriteCreateSequence(SequenceCatalogEntry *entry); |
| 61 | void WriteDropSequence(SequenceCatalogEntry *entry); |
| 62 | void WriteSequenceValue(SequenceCatalogEntry *entry, SequenceValue val); |
| 63 | |
| 64 | //! Sets the table used for subsequent insert/delete/update commands |
| 65 | void WriteSetTable(string &schema, string &table); |
| 66 | |
| 67 | void WriteAlter(AlterInfo &info); |
| 68 | |
| 69 | void WriteInsert(DataChunk &chunk); |
| 70 | void WriteDelete(DataChunk &chunk); |
| 71 | void WriteUpdate(DataChunk &chunk, column_t col_idx); |
| 72 | |
| 73 | //! Truncate the WAL to a previous size, and clear anything currently set in the writer |
| 74 | void Truncate(int64_t size); |
| 75 | void Flush(); |
| 76 | |
| 77 | private: |
| 78 | DuckDB &database; |
| 79 | unique_ptr<BufferedFileWriter> writer; |
| 80 | }; |
| 81 | |
| 82 | } // namespace duckdb |
| 83 |