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