| 1 | //===----------------------------------------------------------------------===// |
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/catalog/mapping_value.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/catalog/catalog_entry.hpp" |
| 12 | #include "duckdb/catalog/catalog_set.hpp" |
| 13 | |
| 14 | namespace duckdb { |
| 15 | struct AlterInfo; |
| 16 | |
| 17 | class ClientContext; |
| 18 | |
| 19 | struct EntryIndex { |
| 20 | EntryIndex() : catalog(nullptr), index(DConstants::INVALID_INDEX) { |
| 21 | } |
| 22 | EntryIndex(CatalogSet &catalog, idx_t index) : catalog(&catalog), index(index) { |
| 23 | auto entry = catalog.entries.find(x: index); |
| 24 | if (entry == catalog.entries.end()) { |
| 25 | throw InternalException("EntryIndex - Catalog entry not found in constructor!?" ); |
| 26 | } |
| 27 | catalog.entries[index].reference_count++; |
| 28 | } |
| 29 | ~EntryIndex() { |
| 30 | if (!catalog) { |
| 31 | return; |
| 32 | } |
| 33 | auto entry = catalog->entries.find(x: index); |
| 34 | D_ASSERT(entry != catalog->entries.end()); |
| 35 | auto remaining_ref = --entry->second.reference_count; |
| 36 | if (remaining_ref == 0) { |
| 37 | catalog->entries.erase(x: index); |
| 38 | } |
| 39 | catalog = nullptr; |
| 40 | } |
| 41 | // disable copy constructors |
| 42 | EntryIndex(const EntryIndex &other) = delete; |
| 43 | EntryIndex &operator=(const EntryIndex &) = delete; |
| 44 | //! enable move constructors |
| 45 | EntryIndex(EntryIndex &&other) noexcept { |
| 46 | catalog = nullptr; |
| 47 | index = DConstants::INVALID_INDEX; |
| 48 | std::swap(a&: catalog, b&: other.catalog); |
| 49 | std::swap(a&: index, b&: other.index); |
| 50 | } |
| 51 | EntryIndex &operator=(EntryIndex &&other) noexcept { |
| 52 | std::swap(a&: catalog, b&: other.catalog); |
| 53 | std::swap(a&: index, b&: other.index); |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | unique_ptr<CatalogEntry> &GetEntry() { |
| 58 | auto entry = catalog->entries.find(x: index); |
| 59 | if (entry == catalog->entries.end()) { |
| 60 | throw InternalException("EntryIndex - Catalog entry not found!?" ); |
| 61 | } |
| 62 | return entry->second.entry; |
| 63 | } |
| 64 | idx_t GetIndex() { |
| 65 | return index; |
| 66 | } |
| 67 | EntryIndex Copy() { |
| 68 | if (catalog) { |
| 69 | return EntryIndex(*catalog, index); |
| 70 | } else { |
| 71 | return EntryIndex(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | CatalogSet *catalog; |
| 77 | idx_t index; |
| 78 | }; |
| 79 | |
| 80 | struct MappingValue { |
| 81 | explicit MappingValue(EntryIndex index_p) |
| 82 | : index(std::move(index_p)), timestamp(0), deleted(false), parent(nullptr) { |
| 83 | } |
| 84 | |
| 85 | EntryIndex index; |
| 86 | transaction_t timestamp; |
| 87 | bool deleted; |
| 88 | unique_ptr<MappingValue> child; |
| 89 | MappingValue *parent; |
| 90 | }; |
| 91 | |
| 92 | } // namespace duckdb |
| 93 | |