Kuzu C++ API
Loading...
Searching...
No Matches
transaction.h
Go to the documentation of this file.
1#pragma once
2
3#include <mutex>
4
5#include "statement_type.h"
6#include "types.h"
7
8namespace kuzu {
9namespace binder {
10struct BoundAlterInfo;
11}
12namespace catalog {
13class CatalogEntry;
14class CatalogSet;
15class SequenceCatalogEntry;
16struct SequenceRollbackData;
17} // namespace catalog
18namespace main {
19class ClientContext;
20} // namespace main
21namespace storage {
22class LocalStorage;
23class UndoBuffer;
24class WAL;
25class VersionInfo;
26class UpdateInfo;
27struct VectorUpdateInfo;
28class ChunkedNodeGroup;
29class VersionRecordHandler;
30} // namespace storage
31namespace transaction {
32class TransactionManager;
33
35
36class LocalCacheManager;
38public:
39 explicit LocalCacheObject(std::string key) : key{std::move(key)} {}
40
41 virtual ~LocalCacheObject() = default;
42
43 std::string getKey() const { return key; }
44
45 template<typename T>
46 T* cast() {
47 return common::ku_dynamic_cast<T*>(this);
48 }
49
50private:
51 std::string key;
52};
53
55public:
56 bool contains(const std::string& key) {
57 std::unique_lock lck{mtx};
58 return cachedObjects.contains(key);
59 }
60 LocalCacheObject& at(const std::string& key) {
61 std::unique_lock lck{mtx};
62 return *cachedObjects.at(key);
63 }
64 bool put(std::unique_ptr<LocalCacheObject> object);
65
66 void remove(const std::string& key) {
67 std::unique_lock lck{mtx};
68 cachedObjects.erase(key);
69 }
70
71private:
72 std::unordered_map<std::string, std::unique_ptr<LocalCacheObject>> cachedObjects;
73 std::mutex mtx;
74};
75
77 friend class TransactionManager;
78
79public:
83 static_cast<common::transaction_t>(1) << 63;
84
85 Transaction(main::ClientContext& clientContext, TransactionType transactionType,
86 common::transaction_t transactionID, common::transaction_t startTS);
87
88 explicit Transaction(TransactionType transactionType) noexcept;
90 common::transaction_t startTS) noexcept;
91
93
94 TransactionType getType() const { return type; }
95 bool isReadOnly() const { return TransactionType::READ_ONLY == type; }
96 bool isWriteTransaction() const { return TransactionType::WRITE == type; }
97 bool isDummy() const { return TransactionType::DUMMY == type; }
98 bool isRecovery() const { return TransactionType::RECOVERY == type; }
99 common::transaction_t getID() const { return ID; }
100 common::transaction_t getStartTS() const { return startTS; }
101 common::transaction_t getCommitTS() const { return commitTS; }
102 int64_t getCurrentTS() const { return currentTS; }
103 main::ClientContext* getClientContext() const { return clientContext; }
104
106 // Note: We always force checkpoint for COPY_FROM statement.
107 if (statementType == common::StatementType::COPY_FROM) {
108 forceCheckpoint = true;
109 }
110 }
112 return getID() > DUMMY_TRANSACTION_ID && !isReadOnly();
113 }
114 bool shouldLogToWAL() const;
115
117
118 KUZU_API void commit(storage::WAL* wal);
119 void rollback(storage::WAL* wal);
120
121 uint64_t getEstimatedMemUsage() const;
122 storage::LocalStorage* getLocalStorage() const { return localStorage.get(); }
123 LocalCacheManager& getLocalCacheManager() { return localCacheManager; }
124 bool isUnCommitted(common::table_id_t tableID, common::offset_t nodeOffset) const {
125 return nodeOffset >= getMinUncommittedNodeOffset(tableID);
126 }
128 common::offset_t nodeOffset) const {
129 KU_ASSERT(isUnCommitted(tableID, nodeOffset));
130 return nodeOffset - getMinUncommittedNodeOffset(tableID);
131 }
133 common::row_idx_t localRowIdx) const {
134 return getMinUncommittedNodeOffset(tableID) + localRowIdx;
135 }
137 // The only case that minUncommittedNodeOffsets doesn't track the given tableID is when the
138 // table is newly created within the same transaction, thus the minUncommittedNodeOffsets
139 // should be 0.
140 return minUncommittedNodeOffsets.contains(tableID) ? minUncommittedNodeOffsets.at(tableID) :
141 0;
142 }
143 void pushCreateDropCatalogEntry(catalog::CatalogSet& catalogSet,
144 catalog::CatalogEntry& catalogEntry, bool isInternal, bool skipLoggingToWAL = false);
145 void pushAlterCatalogEntry(catalog::CatalogSet& catalogSet, catalog::CatalogEntry& catalogEntry,
146 const binder::BoundAlterInfo& alterInfo);
147 void pushSequenceChange(catalog::SequenceCatalogEntry* sequenceEntry, int64_t kCount,
148 const catalog::SequenceRollbackData& data);
150 common::row_idx_t numRows, const storage::VersionRecordHandler* versionRecordHandler) const;
152 common::row_idx_t numRows, const storage::VersionRecordHandler* versionRecordHandler) const;
153 void pushVectorUpdateInfo(storage::UpdateInfo& updateInfo, common::idx_t vectorIdx,
154 storage::VectorUpdateInfo& vectorUpdateInfo) const;
155
157
158private:
160 common::transaction_t startTS,
161 std::unordered_map<common::table_id_t, common::offset_t> minUncommittedNodeOffsets);
162
163private:
164 TransactionType type;
166 common::transaction_t startTS;
167 common::transaction_t commitTS;
168 int64_t currentTS;
169 main::ClientContext* clientContext;
170 std::unique_ptr<storage::LocalStorage> localStorage;
171 std::unique_ptr<storage::UndoBuffer> undoBuffer;
172 LocalCacheManager localCacheManager;
173 bool forceCheckpoint;
174 bool hasCatalogChanges;
175
176 // For each node table, we keep track of the minimum uncommitted node offset when the
177 // transaction starts. This is mainly used to assign offsets to local nodes and determine if a
178 // given node is transaction local or not.
179 std::unordered_map<common::table_id_t, common::offset_t> minUncommittedNodeOffsets;
180};
181
182// TODO(bmwinger): These shouldn't need to be exported
185
186} // namespace transaction
187} // namespace kuzu
#define KUZU_API
Definition api.h:25
#define KU_ASSERT(condition)
Definition assert.h:19
Contain client side configuration. We make profiler associated per query, so profiler is not maintain...
Definition client_context.h:68
Definition transaction.h:54
void remove(const std::string &key)
Definition transaction.h:66
LocalCacheObject & at(const std::string &key)
Definition transaction.h:60
bool put(std::unique_ptr< LocalCacheObject > object)
bool contains(const std::string &key)
Definition transaction.h:56
Definition transaction.h:37
std::string getKey() const
Definition transaction.h:43
LocalCacheObject(std::string key)
Definition transaction.h:39
T * cast()
Definition transaction.h:46
Definition transaction.h:76
storage::LocalStorage * getLocalStorage() const
Definition transaction.h:122
void checkForceCheckpoint(common::StatementType statementType)
Definition transaction.h:105
common::transaction_t getStartTS() const
Definition transaction.h:100
common::row_idx_t getLocalRowIdx(common::table_id_t tableID, common::offset_t nodeOffset) const
Definition transaction.h:127
Transaction(TransactionType transactionType) noexcept
Transaction(main::ClientContext &clientContext, TransactionType transactionType, common::transaction_t transactionID, common::transaction_t startTS)
bool isRecovery() const
Definition transaction.h:98
Transaction(TransactionType transactionType, common::transaction_t ID, common::transaction_t startTS) noexcept
common::offset_t getMinUncommittedNodeOffset(common::table_id_t tableID) const
Definition transaction.h:136
bool isUnCommitted(common::table_id_t tableID, common::offset_t nodeOffset) const
Definition transaction.h:124
common::transaction_t getCommitTS() const
Definition transaction.h:101
void pushAlterCatalogEntry(catalog::CatalogSet &catalogSet, catalog::CatalogEntry &catalogEntry, const binder::BoundAlterInfo &alterInfo)
bool shouldAppendToUndoBuffer() const
Definition transaction.h:111
bool isWriteTransaction() const
Definition transaction.h:96
static constexpr common::transaction_t DUMMY_TRANSACTION_ID
Definition transaction.h:80
TransactionType getType() const
Definition transaction.h:94
common::offset_t getUncommittedOffset(common::table_id_t tableID, common::row_idx_t localRowIdx) const
Definition transaction.h:132
void pushVectorUpdateInfo(storage::UpdateInfo &updateInfo, common::idx_t vectorIdx, storage::VectorUpdateInfo &vectorUpdateInfo) const
void pushInsertInfo(common::node_group_idx_t nodeGroupIdx, common::row_idx_t startRow, common::row_idx_t numRows, const storage::VersionRecordHandler *versionRecordHandler) const
LocalCacheManager & getLocalCacheManager()
Definition transaction.h:123
static constexpr common::transaction_t DUMMY_START_TIMESTAMP
Definition transaction.h:81
main::ClientContext * getClientContext() const
Definition transaction.h:103
void pushSequenceChange(catalog::SequenceCatalogEntry *sequenceEntry, int64_t kCount, const catalog::SequenceRollbackData &data)
common::transaction_t getID() const
Definition transaction.h:99
friend class TransactionManager
Definition transaction.h:77
bool isReadOnly() const
Definition transaction.h:95
KUZU_API void commit(storage::WAL *wal)
static constexpr common::transaction_t START_TRANSACTION_ID
Definition transaction.h:82
static Transaction getDummyTransactionFromExistingOne(const Transaction &other)
void pushDeleteInfo(common::node_group_idx_t nodeGroupIdx, common::row_idx_t startRow, common::row_idx_t numRows, const storage::VersionRecordHandler *versionRecordHandler) const
void rollback(storage::WAL *wal)
void pushCreateDropCatalogEntry(catalog::CatalogSet &catalogSet, catalog::CatalogEntry &catalogEntry, bool isInternal, bool skipLoggingToWAL=false)
int64_t getCurrentTS() const
Definition transaction.h:102
bool isDummy() const
Definition transaction.h:97
uint64_t getEstimatedMemUsage() const
Definition bind_input.h:12
Definition database.h:17
uint32_t idx_t
Definition types.h:45
uint64_t node_group_idx_t
Definition types.h:55
uint64_t transaction_t
Definition types.h:66
oid_t table_id_t
Definition types.h:72
uint64_t offset_t
Definition types.h:79
uint64_t row_idx_t
Definition types.h:52
StatementType
Definition statement_type.h:8
@ COPY_FROM
Definition statement_type.h:14
TO ku_dynamic_cast(FROM *old)
Definition cast.h:11
Definition bind_input.h:16
Definition copy_from_error.h:17
Definition kuzu_fwd.h:58
TransactionType
Definition transaction.h:34
@ CHECKPOINT
Definition transaction.h:34
@ READ_ONLY
Definition transaction.h:34
@ RECOVERY
Definition transaction.h:34
@ DUMMY
Definition transaction.h:34
@ WRITE
Definition transaction.h:34
KUZU_API Transaction DUMMY_CHECKPOINT_TRANSACTION
KUZU_API Transaction DUMMY_TRANSACTION
Definition array_utils.h:7