Kùzu C++ API
Loading...
Searching...
No Matches
mask.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <mutex>
5
6#include "constants.h"
7#include "internal_id_t.h"
8
9namespace kuzu {
10namespace common {
11
12// Note: Classes in this file are NOT thread-safe.
18
19struct MaskData {
20 uint8_t* data;
21
22 explicit MaskData(uint64_t size, uint8_t defaultVal = 0) : size{size} {
23 dataBuffer = std::make_unique<uint8_t[]>(size);
24 data = dataBuffer.get();
25 std::fill(data, data + size, defaultVal);
26 }
27
28 inline void setMask(uint64_t pos, uint8_t maskValue) const { data[pos] = maskValue; }
29 inline bool isMasked(uint64_t pos, uint8_t trueMaskVal) const {
30 return data[pos] == trueMaskVal;
31 }
32 inline uint8_t getMaskValue(uint64_t pos) const { return data[pos]; }
33 inline uint64_t getSize() const { return size; }
34
35private:
36 std::unique_ptr<uint8_t[]> dataBuffer;
37 uint64_t size;
38};
39
40// MaskCollection represents multiple mask on the same domain with AND semantic.
42public:
43 MaskCollection() : numMasks{0} {}
44
45 void init(common::offset_t maxOffset) {
46 std::unique_lock lck{mtx};
47 if (maskData != nullptr) {
48 // MaskCollection might be initialized repeatedly. Because multiple semiMasker can
49 // hold the same mask.
50 return;
51 }
52 maskData = std::make_unique<MaskData>(maxOffset + 1);
53 }
54
55 // Return true if any offset between [startOffset, endOffset] is masked. Otherwise return false.
56 bool isMasked(common::offset_t startOffset, common::offset_t endOffset) const {
57 auto offset = startOffset;
58 auto numMasked = 0u;
59 while (offset <= endOffset) {
60 numMasked += maskData->isMasked(offset++, numMasks);
61 }
62 return numMasked > 0;
63 }
64 // Increment mask value for the given nodeOffset if its current mask value is equal to
65 // the specified `currentMaskValue`.
66 // Note: blindly update mask does not parallelize well, so we minimize write by first checking
67 // if the mask is set to true (mask value is equal to the expected currentMaskValue) or not.
68 void incrementMaskValue(common::offset_t offset, uint8_t currentMaskValue) {
69 if (offset >= maskData->getSize()) [[unlikely]] { // Handle uncommitted node offsets.
70 return;
71 }
72 if (maskData->isMasked(offset, currentMaskValue)) {
73 maskData->setMask(offset, currentMaskValue + 1);
74 }
75 }
76
77 uint8_t getNumMasks() const { return numMasks; }
78 void incrementNumMasks() { numMasks++; }
79
80private:
81 std::mutex mtx;
82 std::unique_ptr<MaskData> maskData;
83 uint8_t numMasks;
84};
85
87public:
90 virtual ~NodeSemiMask() = default;
91
94
95 virtual void init() = 0;
96
97 virtual void incrementMaskValue(common::offset_t nodeOffset, uint8_t currentMaskValue) = 0;
98 virtual bool isMasked(common::offset_t startNodeOffset, common::offset_t endNodeOffset) = 0;
99
100 bool isEnabled() const { return getNumMasks() > 0; }
101 uint8_t getNumMasks() const { return maskCollection.getNumMasks(); }
103
104protected:
108};
109
111public:
114
115 void init() override {
117 return;
118 }
120 }
121
122 void incrementMaskValue(common::offset_t nodeOffset, uint8_t currentMaskValue) override {
123 maskCollection.incrementMaskValue(nodeOffset, currentMaskValue);
124 }
125
126 bool isMasked(common::offset_t startNodeOffset, common::offset_t endNodeOffset) override {
127 return maskCollection.isMasked(startNodeOffset, endNodeOffset);
128 }
129};
130
132public:
135
136 void init() override {
138 return;
139 }
141 }
142
143 void incrementMaskValue(uint64_t nodeOffset, uint8_t currentMaskValue) override {
144 maskCollection.incrementMaskValue(MaskUtil::getVectorIdx(nodeOffset), currentMaskValue);
145 }
146
147 bool isMasked(common::offset_t startNodeOffset, common::offset_t endNodeOffset) override {
148 return maskCollection.isMasked(MaskUtil::getVectorIdx(startNodeOffset),
149 MaskUtil::getVectorIdx(endNodeOffset));
150 }
151};
152
153} // namespace common
154} // namespace kuzu
Definition mask.h:41
uint8_t getNumMasks() const
Definition mask.h:77
MaskCollection()
Definition mask.h:43
bool isMasked(common::offset_t startOffset, common::offset_t endOffset) const
Definition mask.h:56
void incrementMaskValue(common::offset_t offset, uint8_t currentMaskValue)
Definition mask.h:68
void incrementNumMasks()
Definition mask.h:78
void init(common::offset_t maxOffset)
Definition mask.h:45
void incrementMaskValue(common::offset_t nodeOffset, uint8_t currentMaskValue) override
Definition mask.h:122
bool isMasked(common::offset_t startNodeOffset, common::offset_t endNodeOffset) override
Definition mask.h:126
NodeOffsetLevelSemiMask(common::table_id_t tableID, common::offset_t maxOffset)
Definition mask.h:112
void init() override
Definition mask.h:115
Definition mask.h:86
uint8_t getNumMasks() const
Definition mask.h:101
bool isEnabled() const
Definition mask.h:100
void incrementNumMasks()
Definition mask.h:102
virtual void incrementMaskValue(common::offset_t nodeOffset, uint8_t currentMaskValue)=0
common::offset_t getMaxOffset() const
Definition mask.h:93
common::table_id_t tableID
Definition mask.h:105
virtual bool isMasked(common::offset_t startNodeOffset, common::offset_t endNodeOffset)=0
virtual ~NodeSemiMask()=default
MaskCollection maskCollection
Definition mask.h:107
common::table_id_t getTableID() const
Definition mask.h:92
NodeSemiMask(common::table_id_t tableID, common::offset_t maxOffset)
Definition mask.h:88
common::offset_t maxOffset
Definition mask.h:106
bool isMasked(common::offset_t startNodeOffset, common::offset_t endNodeOffset) override
Definition mask.h:147
void incrementMaskValue(uint64_t nodeOffset, uint8_t currentMaskValue) override
Definition mask.h:143
NodeVectorLevelSemiMask(common::table_id_t tableID, common::offset_t maxOffset)
Definition mask.h:133
void init() override
Definition mask.h:136
constexpr offset_t INVALID_OFFSET
Definition internal_id_t.h:23
uint64_t table_id_t
Definition internal_id_t.h:14
uint64_t offset_t
Definition internal_id_t.h:22
constexpr uint64_t DEFAULT_VECTOR_CAPACITY_LOG_2
Definition constants.h:11
Definition alter_type.h:5
Definition mask.h:19
uint8_t * data
Definition mask.h:20
uint8_t getMaskValue(uint64_t pos) const
Definition mask.h:32
void setMask(uint64_t pos, uint8_t maskValue) const
Definition mask.h:28
uint64_t getSize() const
Definition mask.h:33
bool isMasked(uint64_t pos, uint8_t trueMaskVal) const
Definition mask.h:29
MaskData(uint64_t size, uint8_t defaultVal=0)
Definition mask.h:22
Definition mask.h:13
static common::offset_t getVectorIdx(common::offset_t offset)
Definition mask.h:14