-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
undo_buffer.hpp
73 lines (58 loc) · 1.99 KB
/
undo_buffer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//===----------------------------------------------------------------------===//
// DuckDB
//
// transaction/undo_buffer.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "duckdb/common/common.hpp"
#include "duckdb/common/enums/undo_flags.hpp"
#include <memory>
#include <vector>
namespace duckdb {
class WriteAheadLog;
struct UndoChunk {
UndoChunk(index_t size);
~UndoChunk();
data_ptr_t WriteEntry(UndoFlags type, uint32_t len);
unique_ptr<data_t[]> data;
index_t current_position;
index_t maximum_size;
unique_ptr<UndoChunk> next;
UndoChunk *prev;
};
//! The undo buffer of a transaction is used to hold previous versions of tuples
//! that might be required in the future (because of rollbacks or previous
//! transactions accessing them)
class UndoBuffer {
public:
struct IteratorState {
UndoChunk *current;
data_ptr_t start;
data_ptr_t end;
};
public:
UndoBuffer();
//! Reserve space for an entry of the specified type and length in the undo
//! buffer
data_ptr_t CreateEntry(UndoFlags type, index_t len);
bool ChangesMade();
//! Cleanup the undo buffer
void Cleanup();
//! Commit the changes made in the UndoBuffer: should be called on commit
void Commit(UndoBuffer::IteratorState iterator_state, WriteAheadLog *log, transaction_t commit_id);
//! Revert committed changes made in the UndoBuffer up until the currently committed state
void RevertCommit(UndoBuffer::IteratorState iterator_state, transaction_t transaction_id);
//! Rollback the changes made in this UndoBuffer: should be called on
//! rollback
void Rollback() noexcept;
private:
unique_ptr<UndoChunk> head;
UndoChunk *tail;
private:
template <class T> void IterateEntries(UndoBuffer::IteratorState &state, T &&callback);
template <class T> void IterateEntries(UndoBuffer::IteratorState &state, UndoBuffer::IteratorState &end_state, T &&callback);
template <class T> void ReverseIterateEntries(T &&callback);
};
} // namespace duckdb