From 96ac7bd1c5c9a9e78d4183cc07f339313066ab50 Mon Sep 17 00:00:00 2001 From: jbajic Date: Mon, 30 Jan 2023 12:06:09 +0100 Subject: [PATCH] Move out index splitting --- src/storage/v3/indices.hpp | 67 ++++++++++++++++++++++++++++++++++--- src/storage/v3/splitter.cpp | 7 ++-- src/storage/v3/splitter.hpp | 33 ------------------ 3 files changed, 66 insertions(+), 41 deletions(-) diff --git a/src/storage/v3/indices.hpp b/src/storage/v3/indices.hpp index 78a3ee072..56555c31e 100644 --- a/src/storage/v3/indices.hpp +++ b/src/storage/v3/indices.hpp @@ -18,6 +18,8 @@ #include #include "storage/v3/config.hpp" +#include "storage/v3/id_types.hpp" +#include "storage/v3/key_store.hpp" #include "storage/v3/property_value.hpp" #include "storage/v3/transaction.hpp" #include "storage/v3/vertex_accessor.hpp" @@ -40,6 +42,8 @@ class LabelIndex { bool operator==(const Entry &rhs) const { return vertex == rhs.vertex && timestamp == rhs.timestamp; } }; + using IndexType = LabelId; + public: using IndexContainer = std::set; @@ -118,9 +122,36 @@ class LabelIndex { void Clear() { index_.clear(); } - [[nodiscard]] bool Empty() const noexcept { return index_.empty(); } + std::map SplitIndexEntries( + const PrimaryKey &split_key, + std::map> &vertex_entry_map) { + if (index_.empty()) { + return {}; + } - std::map &GetIndex() noexcept { return index_; } + // Cloned index entries will contain new index entry iterators, but old + // vertices address which need to be adjusted after extracting vertices + std::map cloned_indices; + for (auto &[index_type_val, index] : index_) { + auto entry_it = index.begin(); + auto &cloned_indices_container = cloned_indices[index_type_val]; + while (entry_it != index.end()) { + // We need to save the next pointer since the current one will be + // invalidated after extract + auto next_entry_it = std::next(entry_it); + if (entry_it->vertex->first > split_key) { + [[maybe_unused]] const auto &[inserted_entry_it, inserted, node] = + cloned_indices_container.insert(index.extract(entry_it)); + MG_ASSERT(inserted, "Failed to extract index entry!"); + + vertex_entry_map[index_type_val].insert({inserted_entry_it->vertex, inserted_entry_it}); + } + entry_it = next_entry_it; + } + } + + return cloned_indices; + } private: std::map index_; @@ -141,6 +172,7 @@ class LabelPropertyIndex { bool operator<(const PropertyValue &rhs) const; bool operator==(const PropertyValue &rhs) const; }; + using IndexType = std::pair; public: using IndexContainer = std::set; @@ -237,9 +269,36 @@ class LabelPropertyIndex { void Clear() { index_.clear(); } - [[nodiscard]] bool Empty() const noexcept { return index_.empty(); } + std::map SplitIndexEntries( + const PrimaryKey &split_key, + std::map> &vertex_entry_map) { + if (index_.empty()) { + return {}; + } - std::map, IndexContainer> &GetIndex() noexcept { return index_; } + // Cloned index entries will contain new index entry iterators, but old + // vertices address which need to be adjusted after extracting vertices + std::map cloned_indices; + for (auto &[index_type_val, index] : index_) { + auto entry_it = index.begin(); + auto &cloned_index_container = cloned_indices[index_type_val]; + while (entry_it != index.end()) { + // We need to save the next pointer since the current one will be + // invalidated after extract + auto next_entry_it = std::next(entry_it); + if (entry_it->vertex->first > split_key) { + [[maybe_unused]] const auto &[inserted_entry_it, inserted, node] = + cloned_index_container.insert(index.extract(entry_it)); + MG_ASSERT(inserted, "Failed to extract index entry!"); + + vertex_entry_map[index_type_val].insert({inserted_entry_it->vertex, inserted_entry_it}); + } + entry_it = next_entry_it; + } + } + + return cloned_indices; + } private: std::map, IndexContainer> index_; diff --git a/src/storage/v3/splitter.cpp b/src/storage/v3/splitter.cpp index 375d3aa60..1d36f4ff8 100644 --- a/src/storage/v3/splitter.cpp +++ b/src/storage/v3/splitter.cpp @@ -77,10 +77,9 @@ VertexContainer Splitter::CollectVertices(SplitData &data, std::set &c std::multimap> label_property_vertex_entry_map; - data.label_indices = - CollectIndexEntries(indices_.label_index, split_key, label_index_vertex_entry_map); - data.label_property_indices = CollectIndexEntries>( - indices_.label_property_index, split_key, label_property_vertex_entry_map); + data.label_indices = indices_.label_index.SplitIndexEntries(split_key, label_index_vertex_entry_map); + data.label_property_indices = + indices_.label_property_index.SplitIndexEntries(split_key, label_property_vertex_entry_map); // This is needed to replace old vertex pointers in index entries with new ones const auto update_indices = [](auto &entry_vertex_map, auto &updating_index, const auto *old_vertex_ptr, auto &new_vertex_ptr) { diff --git a/src/storage/v3/splitter.hpp b/src/storage/v3/splitter.hpp index 17e8265eb..7afa3422c 100644 --- a/src/storage/v3/splitter.hpp +++ b/src/storage/v3/splitter.hpp @@ -74,39 +74,6 @@ class Splitter final { const std::set &collected_transactions_start_id, VertexContainer &cloned_vertices, EdgeContainer &cloned_edges); - template - requires utils::SameAsAnyOf - std::map CollectIndexEntries( - IndexMap &index, const PrimaryKey &split_key, - std::map> - &vertex_entry_map) { - if (index.Empty()) { - return {}; - } - - // Cloned index entries will contain new index entry iterators, but old - // vertices address which need to be adjusted after extracting vertices - std::map cloned_indices; - for (auto &[index_type_val, index] : index.GetIndex()) { - auto entry_it = index.begin(); - while (entry_it != index.end()) { - // We need to save the next pointer since the current one will be - // invalidated after extract - auto next_entry_it = std::next(entry_it); - if (entry_it->vertex->first > split_key) { - [[maybe_unused]] const auto &[inserted_entry_it, inserted, node] = - cloned_indices[index_type_val].insert(index.extract(entry_it)); - MG_ASSERT(inserted, "Failed to extract index entry!"); - - vertex_entry_map[index_type_val].insert({inserted_entry_it->vertex, inserted_entry_it}); - } - entry_it = next_entry_it; - } - } - - return cloned_indices; - } - static void ScanDeltas(std::set &collected_transactions_start_id, Delta *delta); static void AdjustClonedTransaction(Transaction &cloned_transaction, const Transaction &transaction,