1 | #include "duckdb/execution/index/art/node.hpp" |
---|---|
2 | #include "duckdb/execution/index/art/leaf.hpp" |
3 | |
4 | #include <cstring> |
5 | |
6 | using namespace duckdb; |
7 | using namespace std; |
8 | |
9 | Leaf::Leaf(ART &art, unique_ptr<Key> value, row_t row_id) : Node(art, NodeType::NLeaf, 0) { |
10 | this->value = move(value); |
11 | this->capacity = 1; |
12 | this->row_ids = unique_ptr<row_t[]>(new row_t[this->capacity]); |
13 | this->row_ids[0] = row_id; |
14 | this->num_elements = 1; |
15 | } |
16 | |
17 | void Leaf::Insert(row_t row_id) { |
18 | // Grow array |
19 | if (num_elements == capacity) { |
20 | auto new_row_id = unique_ptr<row_t[]>(new row_t[capacity * 2]); |
21 | memcpy(new_row_id.get(), row_ids.get(), capacity * sizeof(row_t)); |
22 | capacity *= 2; |
23 | row_ids = move(new_row_id); |
24 | } |
25 | row_ids[num_elements++] = row_id; |
26 | } |
27 | |
28 | //! TODO: Maybe shrink array dynamically? |
29 | void Leaf::Remove(row_t row_id) { |
30 | idx_t entry_offset = INVALID_INDEX; |
31 | for (idx_t i = 0; i < num_elements; i++) { |
32 | if (row_ids[i] == row_id) { |
33 | entry_offset = i; |
34 | break; |
35 | } |
36 | } |
37 | if (entry_offset == INVALID_INDEX) { |
38 | return; |
39 | } |
40 | num_elements--; |
41 | for (idx_t j = entry_offset; j < num_elements; j++) { |
42 | row_ids[j] = row_ids[j + 1]; |
43 | } |
44 | } |
45 |