| 1 | //===----------------------------------------------------------------------===// |
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/execution/index/art/art_key.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/common/common.hpp" |
| 12 | #include "duckdb/common/exception.hpp" |
| 13 | #include "duckdb/common/radix.hpp" |
| 14 | #include "duckdb/common/types/string_type.hpp" |
| 15 | #include "duckdb/common/types/value.hpp" |
| 16 | #include "duckdb/storage/arena_allocator.hpp" |
| 17 | |
| 18 | namespace duckdb { |
| 19 | |
| 20 | class ARTKey { |
| 21 | public: |
| 22 | ARTKey(); |
| 23 | ARTKey(const data_ptr_t &data, const uint32_t &len); |
| 24 | ARTKey(ArenaAllocator &allocator, const uint32_t &len); |
| 25 | |
| 26 | uint32_t len; |
| 27 | data_ptr_t data; |
| 28 | |
| 29 | public: |
| 30 | template <class T> |
| 31 | static inline ARTKey CreateARTKey(ArenaAllocator &allocator, const LogicalType &type, T element) { |
| 32 | auto data = ARTKey::CreateData<T>(allocator, element); |
| 33 | return ARTKey(data, sizeof(element)); |
| 34 | } |
| 35 | |
| 36 | template <class T> |
| 37 | static inline ARTKey CreateARTKey(ArenaAllocator &allocator, const LogicalType &type, const Value &element) { |
| 38 | return CreateARTKey(allocator, type, element.GetValueUnsafe<T>()); |
| 39 | } |
| 40 | |
| 41 | template <class T> |
| 42 | static inline void CreateARTKey(ArenaAllocator &allocator, const LogicalType &type, ARTKey &key, T element) { |
| 43 | key.data = ARTKey::CreateData<T>(allocator, element); |
| 44 | key.len = sizeof(element); |
| 45 | } |
| 46 | |
| 47 | template <class T> |
| 48 | static inline void CreateARTKey(ArenaAllocator &allocator, const LogicalType &type, ARTKey &key, |
| 49 | const Value element) { |
| 50 | key.data = ARTKey::CreateData<T>(allocator, element.GetValueUnsafe<T>()); |
| 51 | key.len = sizeof(element); |
| 52 | } |
| 53 | |
| 54 | public: |
| 55 | data_t &operator[](size_t i) { |
| 56 | return data[i]; |
| 57 | } |
| 58 | const data_t &operator[](size_t i) const { |
| 59 | return data[i]; |
| 60 | } |
| 61 | bool operator>(const ARTKey &k) const; |
| 62 | bool operator<(const ARTKey &k) const; |
| 63 | bool operator>=(const ARTKey &k) const; |
| 64 | bool operator==(const ARTKey &k) const; |
| 65 | |
| 66 | inline bool ByteMatches(const ARTKey &other, const uint32_t &depth) const { |
| 67 | return data[depth] == other[depth]; |
| 68 | } |
| 69 | inline bool Empty() const { |
| 70 | return len == 0; |
| 71 | } |
| 72 | void ConcatenateARTKey(ArenaAllocator &allocator, ARTKey &concat_key); |
| 73 | |
| 74 | private: |
| 75 | template <class T> |
| 76 | static inline data_ptr_t CreateData(ArenaAllocator &allocator, T value) { |
| 77 | auto data = allocator.Allocate(size: sizeof(value)); |
| 78 | Radix::EncodeData<T>(data, value); |
| 79 | return data; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | template <> |
| 84 | ARTKey ARTKey::CreateARTKey(ArenaAllocator &allocator, const LogicalType &type, string_t value); |
| 85 | template <> |
| 86 | ARTKey ARTKey::CreateARTKey(ArenaAllocator &allocator, const LogicalType &type, const char *value); |
| 87 | template <> |
| 88 | void ARTKey::CreateARTKey(ArenaAllocator &allocator, const LogicalType &type, ARTKey &key, string_t value); |
| 89 | } // namespace duckdb |
| 90 | |