Kùzu C++ API
Loading...
Searching...
No Matches
transaction.h
Go to the documentation of this file.
1#pragma once
2
3#include "statement_type.h"
4#include "types.h"
5
6namespace kuzu {
7namespace catalog {
8class CatalogEntry;
9class CatalogSet;
10class SequenceCatalogEntry;
11struct SequenceRollbackData;
12} // namespace catalog
13namespace main {
14class ClientContext;
15} // namespace main
16namespace storage {
17class LocalStorage;
18class UndoBuffer;
19class WAL;
20class VersionInfo;
21class UpdateInfo;
22struct VectorUpdateInfo;
23class ChunkedNodeGroup;
24class VersionRecordHandler;
25} // namespace storage
26namespace transaction {
27class TransactionManager;
28
30
32 friend class TransactionManager;
33
34public:
38 static_cast<common::transaction_t>(1) << 63;
39
40 Transaction(main::ClientContext& clientContext, TransactionType transactionType,
41 common::transaction_t transactionID, common::transaction_t startTS);
42
43 explicit Transaction(TransactionType transactionType) noexcept;
45 common::transaction_t startTS) noexcept;
46
48
49 TransactionType getType() const { return type; }
50 bool isReadOnly() const { return TransactionType::READ_ONLY == type; }
51 bool isWriteTransaction() const { return TransactionType::WRITE == type; }
52 bool isDummy() const { return TransactionType::DUMMY == type; }
53 bool isRecovery() const { return TransactionType::RECOVERY == type; }
54 common::transaction_t getID() const { return ID; }
55 common::transaction_t getStartTS() const { return startTS; }
56 common::transaction_t getCommitTS() const { return commitTS; }
57 int64_t getCurrentTS() const { return currentTS; }
58 main::ClientContext* getClientContext() const { return clientContext; }
59
61 // Note: We always force checkpoint for COPY_FROM statement.
62 if (statementType == common::StatementType::COPY_FROM) {
63 forceCheckpoint = true;
64 }
65 }
67 return getID() > DUMMY_TRANSACTION_ID && !isReadOnly();
68 }
69 bool shouldLogToWAL() const;
70
72
73 KUZU_API void commit(storage::WAL* wal) const;
74 void rollback(storage::WAL* wal) const;
75
76 uint64_t getEstimatedMemUsage() const;
77 storage::LocalStorage* getLocalStorage() const { return localStorage.get(); }
79 return maxCommittedNodeOffsets.contains(tableID);
80 }
82 maxCommittedNodeOffsets[tableID] = offset;
83 }
84 bool isUnCommitted(common::table_id_t tableID, common::offset_t nodeOffset) const {
85 return nodeOffset >= getMinUncommittedNodeOffset(tableID);
86 }
88 common::offset_t nodeOffset) const {
89 KU_ASSERT(isUnCommitted(tableID, nodeOffset));
90 return nodeOffset - getMinUncommittedNodeOffset(tableID);
91 }
93 common::row_idx_t localRowIdx) const {
94 return getMinUncommittedNodeOffset(tableID) + localRowIdx;
95 }
97 // The only case that minUncommittedNodeOffsets doesn't track the given tableID is when the
98 // table is newly created within the same transaction, thus the minUncommittedNodeOffsets
99 // should be 0.
100 return minUncommittedNodeOffsets.contains(tableID) ? minUncommittedNodeOffsets.at(tableID) :
101 0;
102 }
104 KU_ASSERT(maxCommittedNodeOffsets.contains(tableID));
105 return maxCommittedNodeOffsets.at(tableID);
106 }
108 common::offset_t uncommittedOffset) const {
109 KU_ASSERT(maxCommittedNodeOffsets.contains(tableID));
110 return maxCommittedNodeOffsets.at(tableID) + getLocalRowIdx(tableID, uncommittedOffset);
111 }
112
113 void pushCatalogEntry(catalog::CatalogSet& catalogSet, catalog::CatalogEntry& catalogEntry,
114 bool skipLoggingToWAL = false) const;
115 void pushSequenceChange(catalog::SequenceCatalogEntry* sequenceEntry, int64_t kCount,
116 const catalog::SequenceRollbackData& data) const;
118 common::row_idx_t numRows, const storage::VersionRecordHandler* versionRecordHandler) const;
120 common::row_idx_t numRows, const storage::VersionRecordHandler* versionRecordHandler) const;
121 void pushVectorUpdateInfo(storage::UpdateInfo& updateInfo, common::idx_t vectorIdx,
122 storage::VectorUpdateInfo& vectorUpdateInfo) const;
123
125
126private:
128 common::transaction_t startTS,
129 std::unordered_map<common::table_id_t, common::offset_t> minUncommittedNodeOffsets,
130 std::unordered_map<common::table_id_t, common::offset_t> maxCommittedNodeOffsets);
131
132private:
133 TransactionType type;
135 common::transaction_t startTS;
136 common::transaction_t commitTS;
137 int64_t currentTS;
138 main::ClientContext* clientContext;
139 std::unique_ptr<storage::LocalStorage> localStorage;
140 std::unique_ptr<storage::UndoBuffer> undoBuffer;
141 bool forceCheckpoint;
142
143 // For each node table, we keep track of the minimum uncommitted node offset when the
144 // transaction starts. This is mainly used to assign offsets to local nodes and determine if a
145 // given node is transaction local or not.
146 std::unordered_map<common::table_id_t, common::offset_t> minUncommittedNodeOffsets;
147 // For each node table, we keep track of committed node offset when the transaction commits.
148 // This is mainly used to shift bound/nbr node offsets for rel tables within the same
149 // transaction.
150 std::unordered_map<common::table_id_t, common::offset_t> maxCommittedNodeOffsets;
151};
152
153// TODO(bmwinger): These shouldn't need to be exported
156
157} // namespace transaction
158} // 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:61
Definition transaction.h:31
storage::LocalStorage * getLocalStorage() const
Definition transaction.h:77
void checkForceCheckpoint(common::StatementType statementType)
Definition transaction.h:60
common::transaction_t getStartTS() const
Definition transaction.h:55
void setMaxCommittedNodeOffset(common::table_id_t tableID, common::offset_t offset)
Definition transaction.h:81
common::row_idx_t getLocalRowIdx(common::table_id_t tableID, common::offset_t nodeOffset) const
Definition transaction.h:87
Transaction(TransactionType transactionType) noexcept
Transaction(main::ClientContext &clientContext, TransactionType transactionType, common::transaction_t transactionID, common::transaction_t startTS)
bool hasNewlyInsertedNodes(common::table_id_t tableID) const
Definition transaction.h:78
bool isRecovery() const
Definition transaction.h:53
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:96
bool isUnCommitted(common::table_id_t tableID, common::offset_t nodeOffset) const
Definition transaction.h:84
common::transaction_t getCommitTS() const
Definition transaction.h:56
void pushCatalogEntry(catalog::CatalogSet &catalogSet, catalog::CatalogEntry &catalogEntry, bool skipLoggingToWAL=false) const
bool shouldAppendToUndoBuffer() const
Definition transaction.h:66
bool isWriteTransaction() const
Definition transaction.h:51
static constexpr common::transaction_t DUMMY_TRANSACTION_ID
Definition transaction.h:35
TransactionType getType() const
Definition transaction.h:49
common::offset_t getUncommittedOffset(common::table_id_t tableID, common::row_idx_t localRowIdx) const
Definition transaction.h:92
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
common::offset_t getCommittedOffsetFromUncommitted(common::table_id_t tableID, common::offset_t uncommittedOffset) const
Definition transaction.h:107
static constexpr common::transaction_t DUMMY_START_TIMESTAMP
Definition transaction.h:36
main::ClientContext * getClientContext() const
Definition transaction.h:58
common::transaction_t getID() const
Definition transaction.h:54
void pushSequenceChange(catalog::SequenceCatalogEntry *sequenceEntry, int64_t kCount, const catalog::SequenceRollbackData &data) const
friend class TransactionManager
Definition transaction.h:32
bool isReadOnly() const
Definition transaction.h:50
void rollback(storage::WAL *wal) const
static constexpr common::transaction_t START_TRANSACTION_ID
Definition transaction.h:37
static Transaction getDummyTransactionFromExistingOne(const Transaction &other)
KUZU_API void commit(storage::WAL *wal) const
void pushDeleteInfo(common::node_group_idx_t nodeGroupIdx, common::row_idx_t startRow, common::row_idx_t numRows, const storage::VersionRecordHandler *versionRecordHandler) const
int64_t getCurrentTS() const
Definition transaction.h:57
bool isDummy() const
Definition transaction.h:52
common::offset_t getMaxCommittedNodeOffset(common::table_id_t tableID) const
Definition transaction.h:103
uint64_t getEstimatedMemUsage() const
uint32_t idx_t
Definition types.h:44
uint64_t node_group_idx_t
Definition types.h:54
uint64_t transaction_t
Definition types.h:64
oid_t table_id_t
Definition types.h:70
uint64_t offset_t
Definition types.h:77
uint64_t row_idx_t
Definition types.h:51
StatementType
Definition statement_type.h:8
TransactionType
Definition transaction.h:29
KUZU_API Transaction DUMMY_CHECKPOINT_TRANSACTION
KUZU_API Transaction DUMMY_TRANSACTION
Definition array_utils.h:7