Kùzu C++ API
Loading...
Searching...
No Matches
node_group.h
Go to the documentation of this file.
1#pragma once
2
3#include "uniq_lock.h"
4#include "residency_state.h"
6#include "group_collection.h"
7
8namespace kuzu {
9namespace transaction {
10class Transaction;
11} // namespace transaction
12
13namespace storage {
14
15struct TableAddColumnState;
16class NodeGroup;
18 // Index of committed but not yet checkpointed chunked group to scan.
21 // State of each chunk in the checkpointed chunked group.
22 std::vector<ChunkState> chunkStates;
23
24 explicit NodeGroupScanState(common::idx_t numChunks) { chunkStates.resize(numChunks); }
25 virtual ~NodeGroupScanState() = default;
27
28 virtual void resetState() {
30 nextRowToScan = 0;
31 for (auto& chunkState : chunkStates) {
32 chunkState.resetState();
33 }
34 }
35
36 template<class TARGET>
40 template<class TARGETT>
44};
45
46class MemoryManager;
48 std::vector<common::column_id_t> columnIDs;
49 std::vector<std::unique_ptr<Column>> columns;
50 BMFileHandle& dataFH;
51 MemoryManager* mm;
52
53 NodeGroupCheckpointState(std::vector<common::column_id_t> columnIDs,
54 std::vector<std::unique_ptr<Column>> columns, BMFileHandle& dataFH, MemoryManager* mm)
55 : columnIDs{std::move(columnIDs)}, columns{std::move(columns)}, dataFH{dataFH}, mm{mm} {}
56 virtual ~NodeGroupCheckpointState() = default;
57
58 template<typename T>
62 template<typename T>
66};
67
69
72
73 constexpr NodeGroupScanResult() noexcept = default;
74 constexpr NodeGroupScanResult(common::row_idx_t startRow, common::row_idx_t numRows) noexcept
76
77 bool operator==(const NodeGroupScanResult& other) const {
78 return startRow == other.startRow && numRows == other.numRows;
79 }
80};
81
82static auto NODE_GROUP_SCAN_EMMPTY_RESULT = NodeGroupScanResult{};
83
84struct TableScanState;
85class NodeGroup {
86public:
94 std::unique_ptr<ChunkedNodeGroup> chunkedNodeGroup,
99 for (auto i = 0u; i < chunkedNodeGroup->getNumColumns(); i++) {
100 dataTypes.push_back(chunkedNodeGroup->getColumnChunk(i).getDataType().copy());
101 }
102 const auto lock = chunkedGroups.lock();
103 chunkedGroups.appendGroup(lock, std::move(chunkedNodeGroup));
104 }
109 virtual ~NodeGroup() = default;
110
111 virtual bool isEmpty() const { return numRows.load() == 0; }
112 virtual common::row_idx_t getNumRows() const { return numRows.load(); }
114 nextRowToAppend += numRowsToAppend;
115 }
117 bool isFull() const { return numRows.load() == capacity; }
118 const std::vector<common::LogicalType>& getDataTypes() const { return dataTypes; }
121 ChunkedNodeGroup& chunkedGroup, common::row_idx_t startRowIdx,
122 common::row_idx_t numRowsToAppend);
124 const std::vector<ColumnChunk*>& chunkedGroup, common::row_idx_t startRowIdx,
125 common::row_idx_t numRowsToAppend);
126 void append(const transaction::Transaction* transaction,
127 const std::vector<common::ValueVector*>& vectors, common::row_idx_t startRowIdx,
128 common::row_idx_t numRowsToAppend);
129
131 std::unique_ptr<ChunkedNodeGroup> chunkedGroup);
132
135 TableScanState& state);
137
138 bool lookup(const common::UniqLock& lock, transaction::Transaction* transaction,
139 const TableScanState& state);
140 bool lookup(transaction::Transaction* transaction, const TableScanState& state);
141
142 void update(transaction::Transaction* transaction, common::row_idx_t rowIdxInGroup,
143 common::column_id_t columnID, const common::ValueVector& propertyVector);
144 bool delete_(const transaction::Transaction* transaction, common::row_idx_t rowIdxInGroup);
145
147 virtual void addColumn(transaction::Transaction* transaction,
148 TableAddColumnState& addColumnState, BMFileHandle* dataFH);
149
150 void flush(BMFileHandle& dataFH);
151
153
156
157 virtual void serialize(common::Serializer& serializer);
158 static std::unique_ptr<NodeGroup> deserialize(common::Deserializer& deSer);
159
161 const auto lock = chunkedGroups.lock();
162 return chunkedGroups.getNumGroups(lock);
163 }
165 const auto lock = chunkedGroups.lock();
166 return chunkedGroups.getGroup(lock, groupIdx);
167 }
168
169 template<class TARGET>
170 TARGET& cast() {
172 }
173 template<class TARGETT>
174 const TARGETT& cast() const {
176 }
177
178 bool isVisible(const transaction::Transaction* transaction, common::row_idx_t rowIdxInGroup);
179 bool isDeleted(const transaction::Transaction* transaction, common::offset_t offsetInGroup);
180 bool isInserted(const transaction::Transaction* transaction, common::offset_t offsetInGroup);
181
182private:
183 ChunkedNodeGroup* findChunkedGroupFromRowIdx(const common::UniqLock& lock,
184 common::row_idx_t rowIdx);
185 ChunkedNodeGroup* findChunkedGroupFromRowIdxNoLock(common::row_idx_t rowIdx);
186
188
189 std::unique_ptr<ChunkedNodeGroup> checkpointInMemOnly(const common::UniqLock& lock,
191 std::unique_ptr<ChunkedNodeGroup> checkpointInMemAndOnDisk(const common::UniqLock& lock,
193 std::unique_ptr<VersionInfo> checkpointVersionInfo(const common::UniqLock& lock,
194 const transaction::Transaction* transaction);
195
196 template<ResidencyState SCAN_RESIDENCY_STATE>
197 common::row_idx_t getNumResidentRows(const common::UniqLock& lock);
198 template<ResidencyState SCAN_RESIDENCY_STATE>
199 std::unique_ptr<ChunkedNodeGroup> scanAllInsertedAndVersions(const common::UniqLock& lock,
200 const std::vector<common::column_id_t>& columnIDs, const std::vector<Column*>& columns);
201
202 static void populateNodeID(common::ValueVector& nodeIDVector, common::table_id_t tableID,
204
205protected:
209 std::atomic<common::row_idx_t> numRows;
210 // `nextRowToAppend` is a cursor to allow us to pre-reserve a set of rows to append before
211 // acutally appending data. This is an optimization to reduce lock-contention when appending in
212 // parallel.
213 // TODO(Guodong): Remove this field.
216 std::vector<common::LogicalType> dataTypes;
218};
219
220} // namespace storage
221} // namespace kuzu
Definition deserializer.h:15
Definition serializer.h:15
Definition value_vector.h:20
Definition chunked_node_group.h:30
Definition group_collection.h:14
Definition node_group.h:85
uint64_t getEstimatedMemoryUsage()
virtual void initializeScanState(transaction::Transaction *transaction, TableScanState &state)
bool isFull() const
Definition node_group.h:117
const TARGETT & cast() const
Definition node_group.h:174
bool delete_(const transaction::Transaction *transaction, common::row_idx_t rowIdxInGroup)
ChunkedNodeGroup * getChunkedNodeGroup(common::node_group_idx_t groupIdx)
Definition node_group.h:164
void moveNextRowToAppend(common::row_idx_t numRowsToAppend)
Definition node_group.h:113
common::row_idx_t capacity
Definition node_group.h:215
common::row_idx_t getNumRowsLeftToAppend() const
Definition node_group.h:116
common::row_idx_t getNumDeletedRows(const transaction::Transaction *transaction)
common::row_idx_t nextRowToAppend
Definition node_group.h:214
void update(transaction::Transaction *transaction, common::row_idx_t rowIdxInGroup, common::column_id_t columnID, const common::ValueVector &propertyVector)
std::atomic< common::row_idx_t > numRows
Definition node_group.h:209
common::row_idx_t append(const transaction::Transaction *transaction, const std::vector< ColumnChunk * > &chunkedGroup, common::row_idx_t startRowIdx, common::row_idx_t numRowsToAppend)
NodeGroup(const common::node_group_idx_t nodeGroupIdx, const bool enableCompression, common::row_idx_t capacity, NodeGroupDataFormat format)
Definition node_group.h:105
common::node_group_idx_t nodeGroupIdx
Definition node_group.h:206
GroupCollection< ChunkedNodeGroup > chunkedGroups
Definition node_group.h:217
bool isDeleted(const transaction::Transaction *transaction, common::offset_t offsetInGroup)
virtual NodeGroupScanResult scan(transaction::Transaction *transaction, TableScanState &state)
common::row_idx_t append(const transaction::Transaction *transaction, ChunkedNodeGroup &chunkedGroup, common::row_idx_t startRowIdx, common::row_idx_t numRowsToAppend)
bool isVisible(const transaction::Transaction *transaction, common::row_idx_t rowIdxInGroup)
virtual void serialize(common::Serializer &serializer)
NodeGroup(const common::node_group_idx_t nodeGroupIdx, const bool enableCompression, std::vector< common::LogicalType > dataTypes, common::row_idx_t capacity=common::StorageConstants::NODE_GROUP_SIZE, NodeGroupDataFormat format=NodeGroupDataFormat::REGULAR)
Definition node_group.h:87
virtual ~NodeGroup()=default
std::vector< common::LogicalType > dataTypes
Definition node_group.h:216
bool isInserted(const transaction::Transaction *transaction, common::offset_t offsetInGroup)
virtual bool isEmpty() const
Definition node_group.h:111
void flush(BMFileHandle &dataFH)
bool lookup(transaction::Transaction *transaction, const TableScanState &state)
virtual common::row_idx_t getNumRows() const
Definition node_group.h:112
NodeGroupDataFormat format
Definition node_group.h:207
bool enableCompression
Definition node_group.h:208
const std::vector< common::LogicalType > & getDataTypes() const
Definition node_group.h:118
TARGET & cast()
Definition node_group.h:170
bool lookup(const common::UniqLock &lock, transaction::Transaction *transaction, const TableScanState &state)
virtual void checkpoint(NodeGroupCheckpointState &state)
void merge(transaction::Transaction *transaction, std::unique_ptr< ChunkedNodeGroup > chunkedGroup)
NodeGroupDataFormat getFormat() const
Definition node_group.h:119
virtual void addColumn(transaction::Transaction *transaction, TableAddColumnState &addColumnState, BMFileHandle *dataFH)
void initializeScanState(transaction::Transaction *transaction, const common::UniqLock &lock, TableScanState &state)
NodeGroup(const common::node_group_idx_t nodeGroupIdx, const bool enableCompression, std::unique_ptr< ChunkedNodeGroup > chunkedNodeGroup, common::row_idx_t capacity=common::StorageConstants::NODE_GROUP_SIZE, NodeGroupDataFormat format=NodeGroupDataFormat::REGULAR)
Definition node_group.h:93
common::node_group_idx_t getNumChunkedGroups()
Definition node_group.h:160
static std::unique_ptr< NodeGroup > deserialize(common::Deserializer &deSer)
void append(const transaction::Transaction *transaction, const std::vector< common::ValueVector * > &vectors, common::row_idx_t startRowIdx, common::row_idx_t numRowsToAppend)
Definition transaction.h:28
uint32_t idx_t
Definition types.h:39
uint64_t node_group_idx_t
Definition types.h:49
property_id_t column_id_t
Definition types.h:36
constexpr row_idx_t INVALID_ROW_IDX
Definition types.h:47
uint64_t table_id_t
Definition internal_id_t.h:14
TO ku_dynamic_cast(FROM old)
Definition cast.h:11
uint64_t offset_t
Definition internal_id_t.h:22
uint64_t row_idx_t
Definition types.h:46
NodeGroupDataFormat
Definition chunked_node_group.h:28
Definition alter_type.h:5
static constexpr uint64_t NODE_GROUP_SIZE
Definition constants.h:103
Definition uniq_lock.h:8
Definition node_group.h:47
BMFileHandle & dataFH
Definition node_group.h:50
NodeGroupCheckpointState(std::vector< common::column_id_t > columnIDs, std::vector< std::unique_ptr< Column > > columns, BMFileHandle &dataFH, MemoryManager *mm)
Definition node_group.h:53
const T & cast() const
Definition node_group.h:59
MemoryManager * mm
Definition node_group.h:51
T & cast()
Definition node_group.h:63
std::vector< std::unique_ptr< Column > > columns
Definition node_group.h:49
std::vector< common::column_id_t > columnIDs
Definition node_group.h:48
Definition node_group.h:68
common::row_idx_t startRow
Definition node_group.h:70
common::row_idx_t numRows
Definition node_group.h:71
constexpr NodeGroupScanResult() noexcept=default
bool operator==(const NodeGroupScanResult &other) const
Definition node_group.h:77
Definition node_group.h:17
TARGET & cast()
Definition node_group.h:37
NodeGroupScanState(common::idx_t numChunks)
Definition node_group.h:24
std::vector< ChunkState > chunkStates
Definition node_group.h:22
common::idx_t chunkedGroupIdx
Definition node_group.h:19
DELETE_COPY_DEFAULT_MOVE(NodeGroupScanState)
common::row_idx_t nextRowToScan
Definition node_group.h:20
virtual ~NodeGroupScanState()=default
virtual void resetState()
Definition node_group.h:28
const TARGETT & constCast()
Definition node_group.h:41
Definition table.h:18