Kùzu C++ API
Loading...
Searching...
No Matches
undo_buffer.h
Go to the documentation of this file.
1#pragma once
2
3#include <mutex>
4
5#include "constants.h"
6#include "types.h"
7
8namespace kuzu {
9namespace catalog {
10class CatalogEntry;
11class CatalogSet;
12class SequenceCatalogEntry;
13struct SequenceRollbackData;
14} // namespace catalog
15namespace transaction {
16class Transaction;
17}
18
19namespace main {
20class ClientContext;
21}
22namespace storage {
23
24// TODO(Guodong): This should be reworked to use MemoryManager for memory allocaiton.
25// For now, we use malloc to get around the limitation of 256KB from MM.
27public:
29
30 explicit UndoMemoryBuffer(uint64_t size) : size{size} {
31 data = std::make_unique<uint8_t[]>(size);
32 currentPosition = 0;
33 }
34
35 uint8_t* getDataUnsafe() const { return data.get(); }
36 uint8_t const* getData() const { return data.get(); }
37 uint64_t getSize() const { return size; }
38 uint64_t getCurrentPosition() const { return currentPosition; }
39 void moveCurrentPosition(uint64_t offset) {
40 KU_ASSERT(currentPosition + offset <= size);
41 currentPosition += offset;
42 }
43 bool canFit(uint64_t size_) const { return currentPosition + size_ <= this->size; }
44
45private:
46 std::unique_ptr<uint8_t[]> data;
47 uint64_t size;
48 uint64_t currentPosition;
49};
50
51class UndoBuffer;
53public:
54 explicit UndoBufferIterator(const UndoBuffer& undoBuffer) : undoBuffer{undoBuffer} {}
55
56 template<typename F>
57 void iterate(F&& callback);
58 template<typename F>
59 void reverseIterate(F&& callback);
60
61private:
62 const UndoBuffer& undoBuffer;
63};
64
65class UpdateInfo;
66class VersionInfo;
67struct VectorUpdateInfo;
69class WAL;
70// This class is not thread safe, as it is supposed to be accessed by a single thread.
72 friend class UndoBufferIterator;
73
74public:
75 enum class UndoRecordType : uint16_t {
76 CATALOG_ENTRY = 0,
78 UPDATE_INFO = 6,
79 INSERT_INFO = 7,
80 DELETE_INFO = 8,
81 };
82
83 explicit UndoBuffer(transaction::Transaction* transaction);
84
86 void createSequenceChange(catalog::SequenceCatalogEntry& sequenceEntry,
87 const catalog::SequenceRollbackData& data);
88 void createVectorInsertInfo(VersionInfo* versionInfo, common::idx_t vectorIdx,
89 common::row_idx_t startRowInVector, common::row_idx_t numRows);
90 void createVectorDeleteInfo(VersionInfo* versionInfo, common::idx_t vectorIdx,
91 common::row_idx_t startRowInVector, common::row_idx_t numRows);
92 void createVectorUpdateInfo(UpdateInfo* updateInfo, common::idx_t vectorIdx,
93 VectorUpdateInfo* vectorUpdateInfo);
94
95 void commit(common::transaction_t commitTS) const;
96 void rollback();
97
98 uint64_t getMemUsage() const;
99
100private:
101 uint8_t* createUndoRecord(uint64_t size);
102
103 void createVectorVersionInfo(UndoRecordType recordType, VersionInfo* versionInfo,
104 common::idx_t vectorIdx, common::row_idx_t startRowInVector, common::row_idx_t numRows);
105
106 void commitRecord(UndoRecordType recordType, const uint8_t* record,
107 common::transaction_t commitTS) const;
108 void rollbackRecord(UndoRecordType recordType, const uint8_t* record);
109
110 void commitCatalogEntryRecord(const uint8_t* record, common::transaction_t commitTS) const;
111 void rollbackCatalogEntryRecord(const uint8_t* record);
112
113 void commitSequenceEntry(uint8_t const* entry, common::transaction_t commitTS) const;
114 void rollbackSequenceEntry(uint8_t const* entry);
115
116 void commitVectorVersionInfo(UndoRecordType recordType, const uint8_t* record,
117 common::transaction_t commitTS) const;
118 void rollbackVectorVersionInfo(UndoRecordType recordType, const uint8_t* record);
119
120 void commitVectorUpdateInfo(const uint8_t* record, common::transaction_t commitTS) const;
121 void rollbackVectorUpdateInfo(const uint8_t* record) const;
122
123private:
124 std::mutex mtx;
125 transaction::Transaction* transaction;
126 std::vector<UndoMemoryBuffer> memoryBuffers;
127};
128
129} // namespace storage
130} // namespace kuzu
#define KU_ASSERT(condition)
Definition assert.h:19
Definition catalog_entry.h:18
Definition catalog_set.h:24
Definition undo_buffer.h:71
void createVectorInsertInfo(VersionInfo *versionInfo, common::idx_t vectorIdx, common::row_idx_t startRowInVector, common::row_idx_t numRows)
void createSequenceChange(catalog::SequenceCatalogEntry &sequenceEntry, const catalog::SequenceRollbackData &data)
void createVectorUpdateInfo(UpdateInfo *updateInfo, common::idx_t vectorIdx, VectorUpdateInfo *vectorUpdateInfo)
void createCatalogEntry(catalog::CatalogSet &catalogSet, catalog::CatalogEntry &catalogEntry)
void commit(common::transaction_t commitTS) const
void createVectorDeleteInfo(VersionInfo *versionInfo, common::idx_t vectorIdx, common::row_idx_t startRowInVector, common::row_idx_t numRows)
UndoRecordType
Definition undo_buffer.h:75
uint64_t getMemUsage() const
UndoBuffer(transaction::Transaction *transaction)
Definition undo_buffer.h:52
UndoBufferIterator(const UndoBuffer &undoBuffer)
Definition undo_buffer.h:54
void reverseIterate(F &&callback)
Definition undo_buffer.h:26
uint64_t getSize() const
Definition undo_buffer.h:37
static constexpr uint64_t UNDO_MEMORY_BUFFER_SIZE
Definition undo_buffer.h:28
uint8_t * getDataUnsafe() const
Definition undo_buffer.h:35
bool canFit(uint64_t size_) const
Definition undo_buffer.h:43
uint64_t getCurrentPosition() const
Definition undo_buffer.h:38
UndoMemoryBuffer(uint64_t size)
Definition undo_buffer.h:30
void moveCurrentPosition(uint64_t offset)
Definition undo_buffer.h:39
uint8_t const * getData() const
Definition undo_buffer.h:36
Definition update_info.h:46
Definition version_info.h:75
Definition transaction.h:28
uint32_t idx_t
Definition types.h:39
uint64_t transaction_t
Definition types.h:57
uint64_t row_idx_t
Definition types.h:46
Definition alter_type.h:5
static constexpr uint64_t PAGE_4KB_SIZE
Definition constants.h:62
Definition update_info.h:21
Definition version_info.h:17