Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::ARTMerger Class Reference
Collaboration diagram for duckdb::ARTMerger:

Classes

struct  NodeEntry

Public Member Functions

 ARTMerger (ArenaAllocator &arena, ART &art)
void Init (Node &left, Node &right)
 Init the merge by setting the initial nodes.
ARTConflictType Merge ()
 Merge until (1) triggering constraint violation or (2) all nodes have been processed.

Private Member Functions

void Emplace (Node &left, Node &right, const GateStatus parent_status, const idx_t depth)
ARTConflictType MergeNodeAndInlined (NodeEntry &entry)
array_ptr< uint8_t > GetBytes (Node &leaf)
void MergeLeaves (NodeEntry &entry)
NodeChildren ExtractChildren (Node &node)
void MergeNodes (NodeEntry &entry)
void MergeNodeAndPrefix (Node &node, Node &prefix, const GateStatus parent_status, const idx_t parent_depth, const uint8_t pos)
void MergeNodeAndPrefix (Node &node, Node &prefix, const GateStatus parent_status, const idx_t parent_depth)
void MergePrefixes (NodeEntry &entry)

Private Attributes

ArenaAllocator & arena
 The arena holds any temporary memory allocated during the Merge phase.
ART & art
 The ART holding the node memory.
stack< NodeEntry > s
 The stack. While merging, NodeEntry elements are pushed onto of the stack.

Detailed Description

ARTMerger merges two ART nodes and their subtrees, until

  1. triggering a constraint violation.
  2. all nodes have been processed. Must be initialized via its Init function before calling Merge, otherwise, the stack is empty and there is nothing to do.

Constructor & Destructor Documentation

◆ ARTMerger()

duckdb::ARTMerger::ARTMerger ( ArenaAllocator & arena,
ART & art )
inline
150394 : arena(arena), art(art) {
150395 }
ART & art
The ART holding the node memory.
Definition duckdb.cpp:150422
ArenaAllocator & arena
The arena holds any temporary memory allocated during the Merge phase.
Definition duckdb.cpp:150420

Member Function Documentation

◆ Init()

void duckdb::ARTMerger::Init ( Node & left,
Node & right )

Init the merge by setting the initial nodes.

153126 {
153127 Emplace(left, right, GateStatus::GATE_NOT_SET, 0);
153128}
Here is the caller graph for this function:

◆ Merge()

ARTConflictType duckdb::ARTMerger::Merge ( )

Merge until (1) triggering constraint violation or (2) all nodes have been processed.

153130 {
153131 while (!s.empty()) {
153132 // Copy the entry so we can pop it.
153133 auto entry = s.top();
153134 s.pop();
153135
153136 const auto left_type = entry.left.GetType();
153137 const auto right_type = entry.right.GetType();
153138
153139 // Early-out due to a constraint violation.
153140 // If right is LEAF_INLINED, then left is also LEAF_INLINED.
153141 const auto duplicate_key =
153142 right_type == NType::LEAF_INLINED || entry.right.GetGateStatus() == GateStatus::GATE_SET;
153143 if (art.IsUnique() && duplicate_key) {
153144 return ARTConflictType::CONSTRAINT;
153145 }
153146
153147 if (left_type == NType::LEAF_INLINED) {
153148 // Both left and right are inlined leaves.
153149 D_ASSERT(right_type == NType::LEAF_INLINED);
153150 Leaf::MergeInlined(arena, art, entry.left, entry.right, entry.status, entry.depth);
153151 continue;
153152 }
153153
153154 if (right_type == NType::LEAF_INLINED) {
153155 // Left is any node except LEAF_INLINED, right is LEAF_INLINED.
153156 auto result = MergeNodeAndInlined(entry);
153157 if (result != ARTConflictType::NO_CONFLICT) {
153158 return result;
153159 }
153160 continue;
153161 }
153162
153163 if (entry.right.IsLeafNode()) {
153164 // Both left and right are leaf nodes.
153165 D_ASSERT(entry.left.IsLeafNode());
153166 MergeLeaves(entry);
153167 continue;
153168 }
153169
153170 if (entry.left.IsNode() && entry.right.IsNode()) {
153171 // Both left and right are nodes.
153172 MergeNodes(entry);
153173 continue;
153174 }
153175
153176 D_ASSERT(right_type == NType::PREFIX);
153177 if (left_type == NType::PREFIX) {
153178 // Both left and right are prefixes.
153179 MergePrefixes(entry);
153180 continue;
153181 }
153182 // Left is a node, right is a PREFIX.
153183 MergeNodeAndPrefix(entry.left, entry.right, entry.status, entry.depth);
153184 }
153185
153186 // We exhausted the stack.
153187 return ARTConflictType::NO_CONFLICT;
153188}
void MergeNodeAndPrefix(Node &node, Node &prefix, const GateStatus parent_status, const idx_t parent_depth, const uint8_t pos)
Definition duckdb.cpp:153321
stack< NodeEntry > s
The stack. While merging, NodeEntry elements are pushed onto of the stack.
Definition duckdb.cpp:150424
static void MergeInlined(ArenaAllocator &arena, ART &art, Node &left, Node &right, GateStatus status, idx_t depth)
Merge two inlined leaf nodes.
Definition duckdb.cpp:154292
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Emplace()

void duckdb::ARTMerger::Emplace ( Node & left,
Node & right,
const GateStatus parent_status,
const idx_t depth )
private
153190 {
153191 const auto left_type = left.GetType();
153192 const auto right_type = right.GetType();
153193
153194 if (left_type == NType::LEAF_INLINED) {
153195 swap(left, right);
153196 } else if (left_type == NType::PREFIX && right_type != NType::LEAF_INLINED) {
153197 swap(left, right);
153198 }
153199
153200 // left ONLY has GATE_SET, if it is the gate node.
153201 // When outside the gate, we propagate the parent_status (GATE_NOT_SET) and the depth.
153202 // When inside the gate, we already reset the depth, and we propagate the parent_status (GATE_SET).
153203 if (left.GetGateStatus() == GateStatus::GATE_NOT_SET) {
153204 s.emplace(left, right, parent_status, depth);
153205 return;
153206 }
153207
153208 // Enter a gate.
153209 // Reset the depth.
153210 D_ASSERT(parent_status == GateStatus::GATE_NOT_SET);
153211 s.emplace(left, right, GateStatus::GATE_SET, 0);
153212}
void swap(Mat &a, Mat &b)

◆ MergeNodeAndInlined()

ARTConflictType duckdb::ARTMerger::MergeNodeAndInlined ( NodeEntry & entry)
private
153214 {
153215 D_ASSERT(entry.right.GetType() == NType::LEAF_INLINED);
153216 D_ASSERT(entry.status == GateStatus::GATE_SET);
153217
153218 // We fall back to the ART insertion code.
153219 auto row_id_key = ARTKey::CreateARTKey<row_t>(arena, entry.right.GetRowId());
153220 return ARTOperator::Insert(arena, art, entry.left, row_id_key, entry.depth, row_id_key, GateStatus::GATE_SET,
153221 DeleteIndexInfo(), IndexAppendMode::DEFAULT);
153222}
static ARTConflictType Insert(ArenaAllocator &arena, ART &art, Node &node, const ARTKey &key, idx_t depth, const ARTKey &row_id, GateStatus status, DeleteIndexInfo delete_index_info, const IndexAppendMode append_mode)
Definition duckdb.cpp:150573

◆ GetBytes()

array_ptr< uint8_t > duckdb::ARTMerger::GetBytes ( Node & leaf)
private
153224 {
153225 const auto type = leaf.GetType();
153226 switch (type) {
153227 case NType::NODE_7_LEAF:
153228 return Node::Ref<Node7Leaf>(art, leaf, type).GetBytes();
153229 case NType::NODE_15_LEAF:
153230 return Node::Ref<Node15Leaf>(art, leaf, type).GetBytes();
153231 case NType::NODE_256_LEAF:
153232 return Node::Ref<Node256Leaf>(art, leaf, type).GetBytes(arena);
153233 default:
153234 throw InternalException("invalid node type for ARTMerger::GetBytes: %s", EnumUtil::ToString(type));
153235 }
153236}
static NODE & Ref(const ART &art, const Node ptr, const NType type)
Get a reference to a node.
Definition duckdb.cpp:11976

◆ MergeLeaves()

void duckdb::ARTMerger::MergeLeaves ( NodeEntry & entry)
private
153238 {
153239 D_ASSERT(entry.left.IsLeafNode());
153240 D_ASSERT(entry.right.IsLeafNode());
153241 D_ASSERT(entry.left.GetGateStatus() == GateStatus::GATE_NOT_SET);
153242 D_ASSERT(entry.right.GetGateStatus() == GateStatus::GATE_NOT_SET);
153243
153244 // Merge the smaller leaf into the bigger leaf.
153245 if (entry.left.GetType() < entry.right.GetType()) {
153246 swap(entry.left, entry.right);
153247 }
153248
153249 // Get the bytes of the right node.
153250 // Then, copy them into left.
153251 auto bytes = GetBytes(entry.right);
153252
153253 // FIXME: Obtain a reference to left once and
153254 // FIXME: handle the different node type combinations.
153255 for (idx_t i = 0; i < bytes.size(); i++) {
153256 Node::InsertChild(art, entry.left, bytes[i]);
153257 }
153258 Node::FreeNode(art, entry.right);
153259}
static void FreeNode(ART &art, Node &node)
Free the node.
Definition duckdb.cpp:154622
static void InsertChild(ART &art, Node &node, const uint8_t byte, const Node child=Node())
Insert the child at byte.
Definition duckdb.cpp:154714
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ ExtractChildren()

NodeChildren duckdb::ARTMerger::ExtractChildren ( Node & node)
private
153261 {
153262 const auto type = node.GetType();
153263 switch (type) {
153264 case NType::NODE_4:
153265 return Node::Ref<Node4>(art, node, type).ExtractChildren(arena);
153266 case NType::NODE_16:
153267 return Node::Ref<Node16>(art, node, type).ExtractChildren(arena);
153268 case NType::NODE_48:
153269 return Node::Ref<Node48>(art, node, type).ExtractChildren(arena);
153270 case NType::NODE_256:
153271 return Node::Ref<Node256>(art, node, type).ExtractChildren(arena);
153272 default:
153273 throw InternalException("invalid node type for ARTMerger::GetChildren: %s", EnumUtil::ToString(type));
153274 }
153275}

◆ MergeNodes()

void duckdb::ARTMerger::MergeNodes ( NodeEntry & entry)
private
153277 {
153278 D_ASSERT(entry.left.IsNode());
153279 D_ASSERT(entry.right.IsNode());
153280
153281 // Merge the smaller node into the bigger node.
153282 if (entry.left.GetType() < entry.right.GetType()) {
153283 swap(entry.left, entry.right);
153284 }
153285
153286 // Get the children of the right node.
153287 // Then, copy them into left.
153288 auto children = ExtractChildren(entry.right);
153289 // As long as the arena is valid,
153290 // the copied-out nodes (and their references) are valid.
153291 Node::FreeNode(art, entry.right);
153292
153293 // First, we iterate and insert children.
153294 // This might grow the node, so we need to do it prior to Emplace.
153295 vector<idx_t> remaining;
153296 for (idx_t i = 0; i < children.bytes.size(); i++) {
153297 const auto byte = children.bytes[i];
153298 auto child = entry.left.GetChildMutable(art, byte);
153299
153300 if (!child) {
153301 // There is no child at this byte.
153302 // We can insert the right node's child at byte and are done.
153303 auto &right_child = children.children[i];
153304 Node::InsertChild(art, entry.left, byte, right_child);
153305 continue;
153306 }
153307 // There is a left and a right child at this byte.
153308 // We remember to emplace the two children.
153309 remaining.emplace_back(i);
153310 }
153311
153312 // Emplace all remaining children.
153313 for (idx_t i = 0; i < remaining.size(); i++) {
153314 const auto byte = children.bytes[remaining[i]];
153315 auto &right_child = children.children[remaining[i]];
153316 auto child = entry.left.GetChildMutable(art, byte);
153317 Emplace(*child, right_child, entry.status, entry.depth + 1);
153318 }
153319}

◆ MergeNodeAndPrefix() [1/2]

void duckdb::ARTMerger::MergeNodeAndPrefix ( Node & node,
Node & prefix,
const GateStatus parent_status,
const idx_t parent_depth,
const uint8_t pos )
private

Merges a node and a prefix. pos determines up to which position we need to reduce the prefix. If we call into this function via MergePrefixes (case: one prefix contains the other), then pos is the length of the shorter prefix.

153322 {
153323 D_ASSERT(node.IsNode());
153324 D_ASSERT(prefix.GetType() == NType::PREFIX);
153325
153326 // Get the child at the prefix byte, or nullptr, if there is no child.
153327 const auto byte = Prefix::GetByte(art, prefix, pos);
153328 auto child = node.GetChildMutable(art, byte);
153329
153330 // Reduce the prefix to the bytes after pos.
153331 Prefix::Reduce(art, prefix, pos);
153332
153333 if (child) {
153334 // Iterate on the child and the remaining prefix.
153335 Emplace(*child, prefix, parent_status, parent_depth + 1);
153336 return;
153337 }
153338
153339 // There is no child at this prefix byte,
153340 // so we can insert the remaining prefix and are done.
153341 Node::InsertChild(art, node, byte, prefix);
153342 prefix.Clear();
153343}
static void Reduce(ART &art, Node &node, const idx_t pos)
Definition duckdb.cpp:155519
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MergeNodeAndPrefix() [2/2]

void duckdb::ARTMerger::MergeNodeAndPrefix ( Node & node,
Node & prefix,
const GateStatus parent_status,
const idx_t parent_depth )
private
153345 {
153346 D_ASSERT(node.IsNode());
153347 D_ASSERT(prefix.GetType() == NType::PREFIX);
153348
153349 MergeNodeAndPrefix(node, prefix, parent_status, parent_depth, 0);
153350}

◆ MergePrefixes()

void duckdb::ARTMerger::MergePrefixes ( NodeEntry & entry)
private
153352 {
153353 D_ASSERT(entry.left.GetType() == NType::PREFIX);
153354 D_ASSERT(entry.right.GetType() == NType::PREFIX);
153355
153356 // We traverse prefixes until we
153357 // Case 1: find a position where they differ.
153358 // Case 2: find that they are the same.
153359 // Case 3: find that one prefix contains the other.
153360
153361 // Until we reach one of these cases, we keep reducing
153362 // the right prefix (and freeing the fully reduced nodes).
153363 // We can do so because up to any of these three cases,
153364 // the prefixes are the same. That means, we only need to keep
153365 // one of them around.
153366
153367 Prefix l_prefix(art, entry.left, true);
153368 Prefix r_prefix(art, entry.right, true);
153369 const auto count = art.PrefixCount();
153370
153371 // Find a byte at pos where the prefixes differ.
153372 // If they match up to max_count, then pos stays invalid.
153373 const auto max_count = MinValue(l_prefix.data[count], r_prefix.data[count]);
153374 optional_idx pos;
153375 for (idx_t i = 0; i < max_count; i++) {
153376 if (l_prefix.data[i] != r_prefix.data[i]) {
153377 pos = i;
153378 break;
153379 }
153380 }
153381
153382 if (pos.IsValid()) {
153383 // The prefixes differ at pos.
153384 // We split the left prefix, and reduce the right prefix.
153385 // Then, we insert both remainders into a new Node4.
153386 // Then, we are done.
153387 const auto cast_pos = UnsafeNumericCast<uint8_t>(pos.GetIndex());
153388 const auto l_byte = Prefix::GetByte(art, entry.left, cast_pos);
153389 const auto r_byte = Prefix::GetByte(art, entry.right, cast_pos);
153390
153391 // Split and reduce.
153392 reference<Node> ref(entry.left);
153393 Node l_child;
153394 const auto status = Prefix::Split(art, ref, l_child, cast_pos);
153395 Prefix::Reduce(art, entry.right, cast_pos);
153396
153397 Node4::New(art, ref);
153398 ref.get().SetGateStatus(status);
153399
153400 Node4::InsertChild(art, ref, l_byte, l_child);
153401 Node4::InsertChild(art, ref, r_byte, entry.right);
153402 entry.right.Clear();
153403 return;
153404 }
153405
153406 if (l_prefix.data[count] == r_prefix.data[count]) {
153407 // The prefixes match.
153408 // Free the right prefix, but keep the reference to its child alive.
153409 // Then, iterate on the left and right (reduced) child.
153410 auto r_child = *r_prefix.ptr;
153411 Node::FreeNode(art, entry.right);
153412 entry.right = r_child;
153413
153414 auto depth = entry.depth + l_prefix.data[count];
153415 Emplace(*l_prefix.ptr, entry.right, entry.status, depth);
153416 return;
153417 }
153418
153419 // max_count indexes the byte after the exhausted prefix in a child node.
153420 if (r_prefix.data[count] == max_count) {
153421 // We exhausted the right prefix.
153422 // Ensure that we continue merging into left.
153423 swap(entry.left, entry.right);
153424 MergeNodeAndPrefix(*r_prefix.ptr, entry.right, entry.status, entry.depth + max_count, max_count);
153425 return;
153426 }
153427
153428 // We exhausted the left prefix.
153429 MergeNodeAndPrefix(*l_prefix.ptr, entry.right, entry.status, entry.depth + max_count, max_count);
153430}
static NodeHandle< BaseNode > New(ART &art, Node &node)
Definition duckdb.cpp:57961
static void InsertChild(ART &art, Node &node, const uint8_t byte, const Node child)
Insert a child at byte.
Definition duckdb.cpp:153682
static GateStatus Split(ART &art, reference< Node > &node, Node &child, const uint8_t pos)
Definition duckdb.cpp:155544
double depth(InputArray R, InputArray t, InputArray X)

The documentation for this class was generated from the following file: