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
17namespace duckdb {
18
19struct AlterInfo;
20
21class BufferedSerializer;
22class Catalog;
23class DuckDB;
24class SchemaCatalogEntry;
25class SequenceCatalogEntry;
26class ViewCatalogEntry;
27class TableCatalogEntry;
28class Transaction;
29class 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.
35class WriteAheadLog {
36public:
37 WriteAheadLog(DuckDB &database);
38
39 //! Whether or not the WAL has been initialized
40 bool initialized;
41
42public:
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
77private:
78 DuckDB &database;
79 unique_ptr<BufferedFileWriter> writer;
80};
81
82} // namespace duckdb
83