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::ARTOperator Class Reference

ARTOperator provides functionality for different ART operations. More...

Static Public Member Functions

static unsafe_optional_ptr< const Node > Lookup (ART &art, const Node &node, const ARTKey &key, idx_t depth)
static bool LookupInLeaf (ART &art, const Node &node, const ARTKey &rowid)
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)
static bool Delete (ART &art, Node &node, const ARTKey &key, const ARTKey &row_id)

Static Private Member Functions

static ARTConflictType InsertIntoInlined (ArenaAllocator &arena, ART &art, Node &node, const ARTKey &key, const ARTKey &row_id, const idx_t depth, const GateStatus status, DeleteIndexInfo delete_index_info, const IndexAppendMode append_mode)
static void InsertIntoNode (ART &art, Node &node, const ARTKey &key, const ARTKey &row_id, const idx_t depth, const GateStatus status)
static void InsertIntoPrefix (ART &art, reference< Node > &node_ref, const ARTKey &key, const ARTKey &row_id, const idx_t pos, const idx_t depth, const GateStatus status)

Detailed Description

ARTOperator provides functionality for different ART operations.

Member Function Documentation

◆ Lookup()

unsafe_optional_ptr< const Node > duckdb::ARTOperator::Lookup ( ART & art,
const Node & node,
const ARTKey & key,
idx_t depth )
inlinestatic

Lookup returns a pointer to the leaf matching the key, or nullptr, if no such leaf exists.

150475 {
150476 reference<const Node> ref(node);
150477
150478 while (ref.get().HasMetadata()) {
150479 // Return the leaf.
150480 if (ref.get().IsAnyLeaf() || ref.get().GetGateStatus() == GateStatus::GATE_SET) {
150481 return unsafe_optional_ptr<const Node>(ref.get());
150482 }
150483
150484 // Traverse the prefix.
150485 if (ref.get().GetType() == NType::PREFIX) {
150486 Prefix prefix(art, ref.get());
150487 for (idx_t i = 0; i < prefix.data[art.PrefixCount()]; i++) {
150488 if (prefix.data[i] != key[depth]) {
150489 // The key and the prefix don't match.
150490 return nullptr;
150491 }
150492 depth++;
150493 }
150494 ref = *prefix.ptr;
150495 continue;
150496 }
150497
150498 // Get the child node.
150499 D_ASSERT(depth < key.len);
150500 auto child = ref.get().GetChild(art, key[depth]);
150501
150502 // No child at the key byte, return nullptr.
150503 if (!child) {
150504 return nullptr;
150505 }
150506
150507 // Continue in the child.
150508 ref = *child;
150509 D_ASSERT(ref.get().HasMetadata());
150510 depth++;
150511 }
150512
150513 return nullptr;
150514 }
double depth(InputArray R, InputArray t, InputArray X)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LookupInLeaf()

bool duckdb::ARTOperator::LookupInLeaf ( ART & art,
const Node & node,
const ARTKey & rowid )
inlinestatic

LookupInLeaf returns true if the rowid is in the leaf: 1) If the leaf is an inlined leaf, check if the rowid matches. 2) If the leaf is a gate node, perform a search in the nested ART for the rowid.

150519 {
150520 reference<const Node> ref(node);
150521 idx_t depth = 0;
150522
150523 while (ref.get().HasMetadata()) {
150524 const auto type = ref.get().GetType();
150525 switch (type) {
150526 case NType::LEAF_INLINED: {
150527 return ref.get().GetRowId() == rowid.GetRowId();
150528 }
150529 case NType::LEAF: {
150530 throw InternalException("Invalid node type (LEAF) for ARTOperator::NestedLookup.");
150531 }
150532 case NType::NODE_7_LEAF:
150533 case NType::NODE_15_LEAF:
150534 case NType::NODE_256_LEAF: {
150535 D_ASSERT(depth + 1 == Prefix::ROW_ID_SIZE);
150536 const auto byte = rowid[Prefix::ROW_ID_COUNT];
150537 return ref.get().HasByte(art, byte);
150538 }
150539 case NType::NODE_4:
150540 case NType::NODE_16:
150541 case NType::NODE_48:
150542 case NType::NODE_256: {
150543 D_ASSERT(depth < Prefix::ROW_ID_SIZE);
150544 auto child = ref.get().GetChild(art, rowid[depth]);
150545 if (child) {
150546 // Continue in the child.
150547 ref = *child;
150548 depth++;
150549 D_ASSERT(ref.get().HasMetadata());
150550 continue;
150551 }
150552 return false;
150553 }
150554 case NType::PREFIX: {
150555 Prefix prefix(art, ref.get());
150556 for (idx_t i = 0; i < prefix.data[art.PrefixCount()]; i++) {
150557 if (prefix.data[i] != rowid[depth]) {
150558 // The key and the prefix don't match.
150559 return false;
150560 }
150561 depth++;
150562 }
150563 ref = *prefix.ptr;
150564 }
150565 }
150566 }
150567 return false;
150568 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Insert()

ARTConflictType duckdb::ARTOperator::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 )
inlinestatic

Insert a key and its row ID into the node. Starts at depth (in the key). status indicates if the insert happens inside a gate or not.

150575 {
150576 reference<Node> active_node_ref(node);
150577 reference<const ARTKey> active_key_ref(key);
150578
150579 // Early-out, if the node is empty.
150580 if (!node.HasMetadata()) {
150581 D_ASSERT(depth == 0);
150582 if (status == GateStatus::GATE_SET) {
150583 Leaf::New(node, row_id.GetRowId());
150584 return ARTConflictType::NO_CONFLICT;
150585 }
150586
150587 Prefix::New(art, active_node_ref, active_key_ref.get(), depth, active_key_ref.get().len);
150588 Leaf::New(active_node_ref, row_id.GetRowId());
150589 return ARTConflictType::NO_CONFLICT;
150590 }
150591
150592 while (active_node_ref.get().HasMetadata()) {
150593 auto &active_node = active_node_ref.get();
150594 auto &active_key = active_key_ref.get();
150595
150596 // status is GATE_SET, if we've passed a gate in the previous iteration.
150597 // In that case, we have not adjusted the reference.
150598 if (status == GateStatus::GATE_NOT_SET && active_node.GetGateStatus() == GateStatus::GATE_SET) {
150599 if (!art.IsUnique()) {
150600 // Enter a gate.
150601 active_key_ref = row_id;
150602 depth = 0;
150603 status = GateStatus::GATE_SET;
150604 continue;
150605 }
150606 // Unique indexes can have duplicates, if another transaction DELETE + INSERT
150607 // the same key. In that case, the previous value must be kept alive until all
150608 // other transactions do not depend on it anymore.
150609
150610 // We restrict this transactionality to two-value leaves, so any subsequent
150611 // incoming transaction must fail here.
150612 return ARTConflictType::TRANSACTION;
150613 }
150614
150615 const auto type = active_node.GetType();
150616 switch (type) {
150617 case NType::LEAF_INLINED: {
150618 return InsertIntoInlined(arena, art, active_node, key, row_id, depth, status, delete_index_info,
150619 append_mode);
150620 }
150621 case NType::LEAF: {
150622 Leaf::TransformToNested(art, active_node);
150623 continue;
150624 }
150625 case NType::NODE_7_LEAF:
150626 case NType::NODE_15_LEAF:
150627 case NType::NODE_256_LEAF: {
150628 // Row IDs are unique; there are never any duplicate byte conflicts.
150629 auto byte = active_key[Prefix::ROW_ID_COUNT];
150630 Node::InsertChild(art, active_node, byte);
150631 return ARTConflictType::NO_CONFLICT;
150632 }
150633 case NType::NODE_4:
150634 case NType::NODE_16:
150635 case NType::NODE_48:
150636 case NType::NODE_256: {
150637 D_ASSERT(depth < active_key.len);
150638 auto child = active_node.GetChildMutable(art, active_key[depth]);
150639 if (child) {
150640 // Continue in the child.
150641 active_node_ref = *child;
150642 depth++;
150643 D_ASSERT(active_node_ref.get().HasMetadata());
150644 continue;
150645 }
150646 InsertIntoNode(art, active_node, key, row_id, depth, status);
150647 return ARTConflictType::NO_CONFLICT;
150648 }
150649 case NType::PREFIX: {
150650 Prefix prefix(art, active_node, true);
150651 for (idx_t i = 0; i < prefix.data[art.PrefixCount()]; i++) {
150652 if (prefix.data[i] != active_key[depth]) {
150653 // The active key and the prefix don't match.
150654 InsertIntoPrefix(art, active_node_ref, active_key, row_id, i, depth, status);
150655 return ARTConflictType::NO_CONFLICT;
150656 }
150657 depth++;
150658 }
150659 active_node_ref = *prefix.ptr;
150660 D_ASSERT(active_node_ref.get().HasMetadata());
150661 continue;
150662 }
150663 default:
150664 throw InternalException("Invalid node type for ARTOperator::Insert.");
150665 }
150666 }
150667 throw InternalException("node without metadata in ARTOperator::Insert");
150668 }
static void New(Node &node, const row_t row_id)
Inline a row ID into a node pointer.
Definition duckdb.cpp:154285
static void TransformToNested(ART &art, Node &node)
Transforms a deprecated leaf to a nested leaf.
Definition duckdb.cpp:154349
static void InsertChild(ART &art, Node &node, const uint8_t byte, const Node child=Node())
Insert the child at byte.
Definition duckdb.cpp:154714
static void New(ART &art, reference< Node > &ref, const ARTKey &key, const idx_t depth, idx_t count)
Get a new list of prefix nodes. The node reference holds the child of the last prefix node.
Definition duckdb.cpp:155477
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Delete()

bool duckdb::ARTOperator::Delete ( ART & art,
Node & node,
const ARTKey & key,
const ARTKey & row_id )
inlinestatic

Delete a key and its row ID. Assumes that deletion starts at the root of the tree.

150672 {
150673 // If we need to compress a Node4 into a one-way node,
150674 // then we need the previous prefix before the Node4.
150675 Node empty;
150676 reference<Node> greatgrandparent(empty);
150677 reference<Node> grandparent(empty);
150678 reference<Node> parent(node);
150679 reference<Node> current(node);
150680 reference<const ARTKey> current_key(key);
150681
150682 idx_t grandparent_depth = 0;
150683 idx_t parent_depth = 0;
150684 idx_t depth = 0;
150685 auto status = GateStatus::GATE_NOT_SET;
150686 auto passed_node = false;
150687
150688 while (current.get().HasMetadata()) {
150689 // Enter gate.
150690 if (status == GateStatus::GATE_NOT_SET && current.get().GetGateStatus() == GateStatus::GATE_SET) {
150691 status = GateStatus::GATE_SET;
150692 current_key = row_id;
150693 depth = 0;
150694 continue;
150695 }
150696
150697 const auto type = current.get().GetType();
150698 switch (type) {
150699 case NType::LEAF_INLINED: {
150700 if (current.get().GetRowId() != row_id.GetRowId()) {
150701 return false;
150702 }
150703 if (!passed_node && parent.get().GetType() == NType::PREFIX) {
150704 // The tree contains exactly one element with a prefix.
150705 Node::FreeTree(art, parent);
150706 return true;
150707 }
150708 if (parent.get().GetType() == NType::PREFIX) {
150709 // We might have to compress:
150710 // PREFIX (greatgrandparent) - Node4 (grandparent) - PREFIX - INLINED_LEAF.
150711 // The parent does not have to be passed in, as it is a child of the possibly being compressed N4.
150712 // Then, when we delete that child, we also free it.
150713 Node::DeleteChild(art, grandparent, greatgrandparent, current_key.get()[grandparent_depth], status,
150714 row_id);
150715 return true;
150716 }
150717 Node::DeleteChild(art, parent, grandparent, current_key.get()[parent_depth], status, row_id);
150718 return true;
150719 }
150720 case NType::LEAF: {
150721 D_ASSERT(status == GateStatus::GATE_NOT_SET);
150722 Leaf::TransformToNested(art, current);
150723 break;
150724 }
150725 case NType::PREFIX: {
150726 greatgrandparent = grandparent;
150727 grandparent = parent;
150728 parent = current;
150729 grandparent_depth = parent_depth;
150730 parent_depth = depth;
150731
150732 // Traverse a prefix chain until the next non-prefix node or gate.
150733 while (current.get().GetType() == NType::PREFIX) {
150734 Prefix prefix(art, current, true);
150735 for (idx_t i = 0; i < prefix.data[art.PrefixCount()]; i++) {
150736 if (prefix.data[i] != current_key.get()[depth]) {
150737 return false;
150738 }
150739 depth++;
150740 }
150741 current = *prefix.ptr;
150742 if (current.get().GetGateStatus() == GateStatus::GATE_SET) {
150743 break;
150744 }
150745 }
150746 break;
150747 }
150748 case NType::NODE_4:
150749 case NType::NODE_16:
150750 case NType::NODE_48:
150751 case NType::NODE_256: {
150752 passed_node = true;
150753 greatgrandparent = grandparent;
150754 grandparent = parent;
150755 parent = current;
150756 grandparent_depth = parent_depth;
150757 parent_depth = depth;
150758
150759 auto child = current.get().GetChildMutable(art, current_key.get()[depth]);
150760 if (!child) {
150761 // No child at the byte: nothing to erase.
150762 return false;
150763 }
150764
150765 current = *child;
150766 depth++;
150767 break;
150768 }
150769 case NType::NODE_7_LEAF:
150770 case NType::NODE_15_LEAF:
150771 case NType::NODE_256_LEAF: {
150772 const auto byte = current_key.get()[depth];
150773 if (current.get().HasByte(art, byte)) {
150774 Node::DeleteChild(art, current, parent, byte, status, row_id);
150775 }
150776 return true;
150777 }
150778 }
150779 }
150780 return false;
150781 }
static void DeleteChild(ART &art, Node &node, Node &prefix, const uint8_t byte, const GateStatus status, const ARTKey &row_id)
Delete the child at byte.
Definition duckdb.cpp:154742
static void FreeTree(ART &art, Node &node)
Free the node and its children.
Definition duckdb.cpp:154628
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InsertIntoInlined()

ARTConflictType duckdb::ARTOperator::InsertIntoInlined ( ArenaAllocator & arena,
ART & art,
Node & node,
const ARTKey & key,
const ARTKey & row_id,
const idx_t depth,
const GateStatus status,
DeleteIndexInfo delete_index_info,
const IndexAppendMode append_mode )
inlinestaticprivate
150786 {
150787 Node row_id_node;
150788 Leaf::New(row_id_node, row_id.GetRowId());
150789
150790 if (!art.IsUnique() || append_mode == IndexAppendMode::INSERT_DUPLICATES) {
150791 Leaf::MergeInlined(arena, art, node, row_id_node, status, depth);
150792 return ARTConflictType::NO_CONFLICT;
150793 }
150794
150795 if (delete_index_info.delete_indexes) {
150796 // Lookup in the delete_art.
150797 for (auto &delete_index : *delete_index_info.delete_indexes) {
150798 auto &delete_art = delete_index.get().Cast<ART>();
150799 auto delete_leaf = Lookup(delete_art, delete_art.tree, key, 0);
150800 if (!delete_leaf) {
150801 continue;
150802 }
150803
150804 // The row ID has changed.
150805 // Thus, the local index has a newer (local) row ID, and this is a constraint violation.
150806 D_ASSERT(delete_leaf->GetType() == NType::LEAF_INLINED);
150807 auto deleted_row_id = delete_leaf->GetRowId();
150808 auto this_row_id = node.GetRowId();
150809 if (deleted_row_id != this_row_id) {
150810 continue;
150811 }
150812
150813 // The deleted key and its row ID match the current key and its row ID.
150814 Leaf::MergeInlined(arena, art, node, row_id_node, status, depth);
150815 return ARTConflictType::NO_CONFLICT;
150816 }
150817 }
150818 if (append_mode == IndexAppendMode::IGNORE_DUPLICATES) {
150819 return ARTConflictType::NO_CONFLICT;
150820 }
150821 return ARTConflictType::CONSTRAINT;
150822 }
static unsafe_optional_ptr< const Node > Lookup(ART &art, const Node &node, const ARTKey &key, idx_t depth)
Definition duckdb.cpp:150475
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

◆ InsertIntoNode()

void duckdb::ARTOperator::InsertIntoNode ( ART & art,
Node & node,
const ARTKey & key,
const ARTKey & row_id,
const idx_t depth,
const GateStatus status )
inlinestaticprivate
150825 {
150826 if (status == GateStatus::GATE_SET) {
150827 // Inside gates, we compress prefixes that only have an inlined
150828 // row ID as their child.
150829 Node row_id_node;
150830 Leaf::New(row_id_node, row_id.GetRowId());
150831 Node::InsertChild(art, node, row_id[depth], row_id_node);
150832 return;
150833 }
150834
150835 Node leaf;
150836 reference<Node> leaf_ref(leaf);
150837 if (depth + 1 < key.len) {
150838 // Outside of gates, we create a prefix for the inlined leaf.
150839 auto count = key.len - depth - 1;
150840 Prefix::New(art, leaf_ref, key, depth + 1, count);
150841 }
150842
150843 // Create and insert the inlined leaf.
150844 Leaf::New(leaf_ref, row_id.GetRowId());
150845 Node::InsertChild(art, node, key[depth], leaf);
150846 }

◆ InsertIntoPrefix()

void duckdb::ARTOperator::InsertIntoPrefix ( ART & art,
reference< Node > & node_ref,
const ARTKey & key,
const ARTKey & row_id,
const idx_t pos,
const idx_t depth,
const GateStatus status )
inlinestaticprivate
150849 {
150850 const auto cast_pos = UnsafeNumericCast<uint8_t>(pos);
150851 const auto byte = Prefix::GetByte(art, node_ref, cast_pos);
150852
150853 Node child;
150854 const auto split_status = Prefix::Split(art, node_ref, child, cast_pos);
150855
150856 Node4::New(art, node_ref);
150857 node_ref.get().SetGateStatus(split_status);
150858
150859 Node4::InsertChild(art, node_ref, byte, child);
150860 InsertIntoNode(art, node_ref, key, row_id, depth, status);
150861 }
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

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