From fc0a71f3d593b3d433cf16052a712bd2f57d47fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Pu=C5=A1i=C4=87?= Date: Fri, 9 Feb 2024 20:55:25 +0100 Subject: [PATCH] Implement review suggestions (on smarter naming, types and calls) --- include/mgp.hpp | 2 +- query_modules/text_search_module.cpp | 2 +- src/query/db_accessor.hpp | 2 +- src/query/dump.cpp | 2 +- src/query/interpreter.cpp | 4 +- src/storage/v2/disk/storage.cpp | 14 +++---- src/storage/v2/durability/durability.cpp | 8 ++-- src/storage/v2/durability/metadata.hpp | 2 +- src/storage/v2/durability/snapshot.cpp | 6 +-- src/storage/v2/durability/wal.cpp | 4 +- src/storage/v2/indices/indices.cpp | 8 ++-- src/storage/v2/indices/indices.hpp | 2 +- src/storage/v2/indices/text_index.cpp | 47 ++++++++++++------------ src/storage/v2/indices/text_index.hpp | 15 ++++---- src/storage/v2/inmemory/storage.cpp | 14 +++---- src/storage/v2/metadata_delta.hpp | 6 +-- src/storage/v2/storage.cpp | 6 +-- src/storage/v2/storage.hpp | 17 +++++---- 18 files changed, 81 insertions(+), 80 deletions(-) diff --git a/include/mgp.hpp b/include/mgp.hpp index 7f45668ac..f648ad372 100644 --- a/include/mgp.hpp +++ b/include/mgp.hpp @@ -4340,7 +4340,7 @@ inline List ListAllLabelPropertyIndices(mgp_graph *memgraph_graph) { return List(label_property_indices); } -inline List SearchTextIndex(mgp_graph *memgraph_graph, std::string_view index_name, std::string_view search_query) { +inline List RunTextSearchQuery(mgp_graph *memgraph_graph, std::string_view index_name, std::string_view search_query) { auto results_or_error = Map(mgp::MemHandlerCallback(graph_search_text_index, memgraph_graph, index_name.data(), search_query.data())); auto maybe_error = results_or_error["error_msg"].ValueString(); diff --git a/query_modules/text_search_module.cpp b/query_modules/text_search_module.cpp index 71dd6510e..56c266239 100644 --- a/query_modules/text_search_module.cpp +++ b/query_modules/text_search_module.cpp @@ -41,7 +41,7 @@ void TextSearch::Search(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *r } // 2. Run a text search of that index and return the search results - for (const auto &node : mgp::SearchTextIndex(memgraph_graph, index_name, search_query)) { + for (const auto &node : mgp::RunTextSearchQuery(memgraph_graph, index_name, search_query)) { auto record = record_factory.NewRecord(); record.Insert(TextSearch::kReturnNode.data(), node.ValueNode()); } diff --git a/src/query/db_accessor.hpp b/src/query/db_accessor.hpp index 7a4e20922..4086211db 100644 --- a/src/query/db_accessor.hpp +++ b/src/query/db_accessor.hpp @@ -576,7 +576,7 @@ class DbAccessor final { void TextIndexUpdateVertex(VertexAccessor *vertex) { accessor_->TextIndexUpdateVertex(&vertex->impl_); } - void TextIndexUpdateVertex(VertexAccessor *vertex, std::vector removed_labels) { + void TextIndexUpdateVertex(VertexAccessor *vertex, const std::vector &removed_labels) { accessor_->TextIndexUpdateVertex(&vertex->impl_, removed_labels); } diff --git a/src/query/dump.cpp b/src/query/dump.cpp index 75164755f..bb4b1d4c8 100644 --- a/src/query/dump.cpp +++ b/src/query/dump.cpp @@ -392,7 +392,7 @@ PullPlanDump::PullChunk PullPlanDump::CreateTextIndicesPullChunk() { if (!indices_info_) { indices_info_.emplace(dba_->ListAllIndices()); } - const auto &text = indices_info_->text; + const auto &text = indices_info_->text_indices; size_t local_counter = 0; while (global_index < text.size() && (!n || local_counter < *n)) { diff --git a/src/query/interpreter.cpp b/src/query/interpreter.cpp index 8a007c26d..5215bdfe6 100644 --- a/src/query/interpreter.cpp +++ b/src/query/interpreter.cpp @@ -3431,7 +3431,7 @@ PreparedQuery PrepareDatabaseInfoQuery(ParsedQuery parsed_query, bool in_explici auto info = dba->ListAllIndices(); auto storage_acc = database->Access(); std::vector> results; - results.reserve(info.label.size() + info.label_property.size() + info.text.size()); + results.reserve(info.label.size() + info.label_property.size() + info.text_indices.size()); for (const auto &item : info.label) { results.push_back({TypedValue(label_index_mark), TypedValue(storage->LabelToName(item)), TypedValue(), TypedValue(static_cast(storage_acc->ApproximateVertexCount(item)))}); @@ -3442,7 +3442,7 @@ PreparedQuery PrepareDatabaseInfoQuery(ParsedQuery parsed_query, bool in_explici TypedValue(storage->PropertyToName(item.second)), TypedValue(static_cast(storage_acc->ApproximateVertexCount(item.first, item.second)))}); } - for (const auto &item : info.text) { + for (const auto &item : info.text_indices) { results.push_back({TypedValue(text_index_mark), TypedValue(item.first), TypedValue(), TypedValue()}); } std::sort(results.begin(), results.end(), [&label_index_mark](const auto &record_1, const auto &record_2) { diff --git a/src/storage/v2/disk/storage.cpp b/src/storage/v2/disk/storage.cpp index 21fc33911..ccdccf1a1 100644 --- a/src/storage/v2/disk/storage.cpp +++ b/src/storage/v2/disk/storage.cpp @@ -847,7 +847,7 @@ StorageInfo DiskStorage::GetInfo(bool force_dir, const auto &lbl = access->ListAllIndices(); info.label_indices = lbl.label.size(); info.label_property_indices = lbl.label_property.size(); - info.text_indices = lbl.text.size(); + info.text_indices = lbl.text_indices.size(); const auto &con = access->ListAllConstraints(); info.existence_constraints = con.existence.size(); info.unique_constraints = con.unique.size(); @@ -1657,12 +1657,12 @@ utils::BasicResult DiskStorage::DiskAccessor::Co throw utils::NotYetImplemented("ClearIndexStats(stats) is not implemented for DiskStorage."); } break; case MetadataDelta::Action::TEXT_INDEX_CREATE: { - if (!disk_storage->durable_metadata_.PersistTextIndexCreation(md_delta.text.index_name)) { + if (!disk_storage->durable_metadata_.PersistTextIndexCreation(md_delta.text_indices.index_name)) { return StorageManipulationError{PersistenceError{}}; } } break; case MetadataDelta::Action::TEXT_INDEX_DROP: { - if (!disk_storage->durable_metadata_.PersistTextIndexDeletion(md_delta.text.index_name)) { + if (!disk_storage->durable_metadata_.PersistTextIndexDeletion(md_delta.text_indices.index_name)) { return StorageManipulationError{PersistenceError{}}; } } break; @@ -1765,7 +1765,7 @@ utils::BasicResult DiskStorage::DiskAccessor::Co } spdlog::trace("rocksdb: Commit successful"); if (flags::run_time::GetExperimentalTextSearchEnabled()) { - disk_storage->indices_.text_index_->Commit(); + disk_storage->indices_.text_index_.Commit(); } is_transaction_active_ = false; @@ -1886,7 +1886,7 @@ void DiskStorage::DiskAccessor::Abort() { transaction_.disk_transaction_->Rollback(); transaction_.disk_transaction_->ClearSnapshot(); if (flags::run_time::GetExperimentalTextSearchEnabled()) { - storage_->indices_.text_index_->Rollback(); + storage_->indices_.text_index_.Rollback(); } delete transaction_.disk_transaction_; transaction_.disk_transaction_ = nullptr; @@ -2077,8 +2077,8 @@ IndicesInfo DiskStorage::DiskAccessor::ListAllIndices() const { auto *disk_label_index = static_cast(on_disk->indices_.label_index_.get()); auto *disk_label_property_index = static_cast(on_disk->indices_.label_property_index_.get()); - auto *text_index = storage_->indices_.text_index_.get(); - return {disk_label_index->ListIndices(), disk_label_property_index->ListIndices(), text_index->ListIndices()}; + auto &text_index = storage_->indices_.text_index_; + return {disk_label_index->ListIndices(), disk_label_property_index->ListIndices(), text_index.ListIndices()}; } ConstraintsInfo DiskStorage::DiskAccessor::ListAllConstraints() const { auto *disk_storage = static_cast(storage_); diff --git a/src/storage/v2/durability/durability.cpp b/src/storage/v2/durability/durability.cpp index b8afc7364..ee20b1227 100644 --- a/src/storage/v2/durability/durability.cpp +++ b/src/storage/v2/durability/durability.cpp @@ -199,10 +199,10 @@ void RecoverIndicesAndStats(const RecoveredIndicesAndConstraints::IndicesMetadat if (flags::run_time::GetExperimentalTextSearchEnabled()) { // Recover text indices. - spdlog::info("Recreating {} text indices from metadata.", indices_metadata.text.size()); - auto *mem_text_index = static_cast(indices->text_index_.get()); - for (const auto &item : indices_metadata.text) { - if (!mem_text_index->RecoverIndex(item.first, item.second, vertices->access(), name_id_mapper)) + spdlog::info("Recreating {} text indices from metadata.", indices_metadata.text_indices.size()); + auto &mem_text_index = indices->text_index_; + for (const auto &item : indices_metadata.text_indices) { + if (!mem_text_index.RecoverIndex(item.first, item.second, vertices->access(), name_id_mapper)) throw RecoveryFailure("The text index must be created here!"); spdlog::info("Text index {} on :{} is recreated from metadata", item.first, name_id_mapper->IdToName(item.second.AsUint())); diff --git a/src/storage/v2/durability/metadata.hpp b/src/storage/v2/durability/metadata.hpp index 9c2e421ca..cb070a922 100644 --- a/src/storage/v2/durability/metadata.hpp +++ b/src/storage/v2/durability/metadata.hpp @@ -43,7 +43,7 @@ struct RecoveredIndicesAndConstraints { std::vector> label_property; std::vector> label_stats; std::vector>> label_property_stats; - std::vector> text; + std::vector> text_indices; } indices; struct ConstraintsMetadata { diff --git a/src/storage/v2/durability/snapshot.cpp b/src/storage/v2/durability/snapshot.cpp index 6d80cd81a..d15f5bf37 100644 --- a/src/storage/v2/durability/snapshot.cpp +++ b/src/storage/v2/durability/snapshot.cpp @@ -1640,8 +1640,8 @@ RecoveredSnapshot LoadSnapshot(const std::filesystem::path &path, utils::SkipLis if (!index_name.has_value()) throw RecoveryFailure("Couldn't read text index name!"); auto label = snapshot.ReadUint(); if (!label) throw RecoveryFailure("Couldn't read text index label!"); - AddRecoveredIndexConstraint(&indices_constraints.indices.text, {index_name.value(), get_label_from_id(*label)}, - "The text index already exists!"); + AddRecoveredIndexConstraint(&indices_constraints.indices.text_indices, + {index_name.value(), get_label_from_id(*label)}, "The text index already exists!"); SPDLOG_TRACE("Recovered metadata of text index {} for :{}", index_name.value(), name_id_mapper->IdToName(snapshot_id_map.at(*label))); } @@ -2127,7 +2127,7 @@ void CreateSnapshot(Storage *storage, Transaction *transaction, const std::files // Write text indices. if (flags::run_time::GetExperimentalTextSearchEnabled()) { - auto text = storage->indices_.text_index_->ListIndices(); + auto text = storage->indices_.text_index_.ListIndices(); snapshot.WriteUint(text.size()); for (const auto &item : text) { snapshot.WriteString(item.first); diff --git a/src/storage/v2/durability/wal.cpp b/src/storage/v2/durability/wal.cpp index 26c5493e0..0f6f2a72a 100644 --- a/src/storage/v2/durability/wal.cpp +++ b/src/storage/v2/durability/wal.cpp @@ -950,14 +950,14 @@ RecoveryInfo LoadWal(const std::filesystem::path &path, RecoveredIndicesAndConst case WalDeltaData::Type::TEXT_INDEX_CREATE: { auto index_name = delta.operation_text.index_name; auto label = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_text.label)); - AddRecoveredIndexConstraint(&indices_constraints->indices.text, {index_name, label}, + AddRecoveredIndexConstraint(&indices_constraints->indices.text_indices, {index_name, label}, "The text index already exists!"); break; } case WalDeltaData::Type::TEXT_INDEX_DROP: { auto index_name = delta.operation_text.index_name; auto label = LabelId::FromUint(name_id_mapper->NameToId(delta.operation_text.label)); - RemoveRecoveredIndexConstraint(&indices_constraints->indices.text, {index_name, label}, + RemoveRecoveredIndexConstraint(&indices_constraints->indices.text_indices, {index_name, label}, "The text index doesn't exist!"); break; } diff --git a/src/storage/v2/indices/indices.cpp b/src/storage/v2/indices/indices.cpp index b8fab3fb7..91a8e0c85 100644 --- a/src/storage/v2/indices/indices.cpp +++ b/src/storage/v2/indices/indices.cpp @@ -14,6 +14,7 @@ #include "storage/v2/disk/label_property_index.hpp" #include "storage/v2/inmemory/label_index.hpp" #include "storage/v2/inmemory/label_property_index.hpp" +#include "storage/v2/storage.hpp" namespace memgraph::storage { @@ -43,7 +44,7 @@ void Indices::UpdateOnAddLabel(LabelId label, Vertex *vertex, const Transaction label_index_->UpdateOnAddLabel(label, vertex, tx); label_property_index_->UpdateOnAddLabel(label, vertex, tx); if (update_text_index) { - text_index_->UpdateOnAddLabel(label, vertex, storage, tx.start_timestamp); + text_index_.UpdateOnAddLabel(label, vertex, storage->name_id_mapper_.get(), tx.start_timestamp); } } @@ -51,7 +52,7 @@ void Indices::UpdateOnRemoveLabel(LabelId label, Vertex *vertex, const Transacti label_index_->UpdateOnRemoveLabel(label, vertex, tx); label_property_index_->UpdateOnRemoveLabel(label, vertex, tx); if (update_text_index) { - text_index_->UpdateOnRemoveLabel(label, vertex, tx.start_timestamp); + text_index_.UpdateOnRemoveLabel(label, vertex, tx.start_timestamp); } } @@ -59,13 +60,12 @@ void Indices::UpdateOnSetProperty(PropertyId property, const PropertyValue &valu const Transaction &tx, Storage *storage, bool update_text_index) const { label_property_index_->UpdateOnSetProperty(property, value, vertex, tx); if (update_text_index) { - text_index_->UpdateOnSetProperty(vertex, storage, tx.start_timestamp); + text_index_.UpdateOnSetProperty(vertex, storage->name_id_mapper_.get(), tx.start_timestamp); } } Indices::Indices(const Config &config, StorageMode storage_mode) { std::invoke([this, config, storage_mode]() { - text_index_ = std::make_unique(); if (storage_mode == StorageMode::IN_MEMORY_TRANSACTIONAL || storage_mode == StorageMode::IN_MEMORY_ANALYTICAL) { label_index_ = std::make_unique(); label_property_index_ = std::make_unique(); diff --git a/src/storage/v2/indices/indices.hpp b/src/storage/v2/indices/indices.hpp index b61b9d337..6b64929aa 100644 --- a/src/storage/v2/indices/indices.hpp +++ b/src/storage/v2/indices/indices.hpp @@ -68,7 +68,7 @@ struct Indices { std::unique_ptr label_index_; std::unique_ptr label_property_index_; - std::unique_ptr text_index_; + mutable TextIndex text_index_; }; } // namespace memgraph::storage diff --git a/src/storage/v2/indices/text_index.cpp b/src/storage/v2/indices/text_index.cpp index 17abdbb2e..8a2833db2 100644 --- a/src/storage/v2/indices/text_index.cpp +++ b/src/storage/v2/indices/text_index.cpp @@ -17,8 +17,9 @@ namespace memgraph::storage { -void TextIndex::AddNode(Vertex *vertex_after_update, Storage *storage, const std::uint64_t transaction_start_timestamp, - const std::vector &applicable_text_indices, bool skip_commit) { +void TextIndex::AddNode(Vertex *vertex_after_update, NameIdMapper *name_id_mapper, + const std::uint64_t transaction_start_timestamp, + const std::vector &applicable_text_indices) { if (!flags::run_time::GetExperimentalTextSearchEnabled()) { throw query::QueryException("To use text indices, enable the text search feature."); } @@ -30,16 +31,16 @@ void TextIndex::AddNode(Vertex *vertex_after_update, Storage *storage, const std for (const auto &[prop_id, prop_value] : vertex_after_update->properties.Properties()) { switch (prop_value.type()) { case PropertyValue::Type::Bool: - properties[storage->PropertyToName(prop_id)] = prop_value.ValueBool(); + properties[name_id_mapper->IdToName(prop_id.AsUint())] = prop_value.ValueBool(); break; case PropertyValue::Type::Int: - properties[storage->PropertyToName(prop_id)] = prop_value.ValueInt(); + properties[name_id_mapper->IdToName(prop_id.AsUint())] = prop_value.ValueInt(); break; case PropertyValue::Type::Double: - properties[storage->PropertyToName(prop_id)] = prop_value.ValueDouble(); + properties[name_id_mapper->IdToName(prop_id.AsUint())] = prop_value.ValueDouble(); break; case PropertyValue::Type::String: - properties[storage->PropertyToName(prop_id)] = prop_value.ValueString(); + properties[name_id_mapper->IdToName(prop_id.AsUint())] = prop_value.ValueString(); break; case PropertyValue::Type::Null: case PropertyValue::Type::List: @@ -63,14 +64,14 @@ void TextIndex::AddNode(Vertex *vertex_after_update, Storage *storage, const std *index_context, mgcxx::text_search::DocumentInput{ .data = document.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace)}, - skip_commit); + kDoSkipCommit); } catch (const std::exception &e) { throw query::QueryException(fmt::format("Tantivy error: {}", e.what())); } } } -void TextIndex::AddNode(Vertex *vertex_after_update, Storage *storage, +void TextIndex::AddNode(Vertex *vertex_after_update, NameIdMapper *name_id_mapper, const std::uint64_t transaction_start_timestamp) { if (!flags::run_time::GetExperimentalTextSearchEnabled()) { throw query::QueryException("To use text indices, enable the text search feature."); @@ -78,10 +79,10 @@ void TextIndex::AddNode(Vertex *vertex_after_update, Storage *storage, auto applicable_text_indices = GetApplicableTextIndices(vertex_after_update); if (applicable_text_indices.empty()) return; - AddNode(vertex_after_update, storage, transaction_start_timestamp, applicable_text_indices); + AddNode(vertex_after_update, std::move(name_id_mapper), transaction_start_timestamp, applicable_text_indices); } -void TextIndex::UpdateNode(Vertex *vertex_after_update, Storage *storage, +void TextIndex::UpdateNode(Vertex *vertex_after_update, NameIdMapper *name_id_mapper, const std::uint64_t transaction_start_timestamp) { if (!flags::run_time::GetExperimentalTextSearchEnabled()) { throw query::QueryException("To use text indices, enable the text search feature."); @@ -90,10 +91,10 @@ void TextIndex::UpdateNode(Vertex *vertex_after_update, Storage *storage, auto applicable_text_indices = GetApplicableTextIndices(vertex_after_update); if (applicable_text_indices.empty()) return; RemoveNode(vertex_after_update, applicable_text_indices); - AddNode(vertex_after_update, storage, transaction_start_timestamp, applicable_text_indices); + AddNode(vertex_after_update, std::move(name_id_mapper), transaction_start_timestamp, applicable_text_indices); } -void TextIndex::UpdateNode(Vertex *vertex_after_update, Storage *storage, +void TextIndex::UpdateNode(Vertex *vertex_after_update, NameIdMapper *name_id_mapper, const std::uint64_t transaction_start_timestamp, const std::vector &removed_labels) { if (!flags::run_time::GetExperimentalTextSearchEnabled()) { @@ -106,7 +107,7 @@ void TextIndex::UpdateNode(Vertex *vertex_after_update, Storage *storage, auto indexes_to_update_node = GetApplicableTextIndices(vertex_after_update); if (indexes_to_update_node.empty()) return; RemoveNode(vertex_after_update, indexes_to_update_node); - AddNode(vertex_after_update, storage, transaction_start_timestamp, indexes_to_update_node); + AddNode(vertex_after_update, std::move(name_id_mapper), transaction_start_timestamp, indexes_to_update_node); } void TextIndex::RemoveNode(Vertex *vertex_after_update, @@ -137,7 +138,7 @@ void TextIndex::RemoveNode(Vertex *vertex_after_update) { RemoveNode(vertex_after_update, applicable_text_indices); } -void TextIndex::UpdateOnAddLabel(LabelId added_label, Vertex *vertex_after_update, Storage *storage, +void TextIndex::UpdateOnAddLabel(LabelId added_label, Vertex *vertex_after_update, NameIdMapper *name_id_mapper, const std::uint64_t transaction_start_timestamp) { if (!flags::run_time::GetExperimentalTextSearchEnabled()) { throw query::QueryException("To use text indices, enable the text search feature."); @@ -146,7 +147,7 @@ void TextIndex::UpdateOnAddLabel(LabelId added_label, Vertex *vertex_after_updat if (!label_to_index_.contains(added_label)) { return; } - AddNode(vertex_after_update, storage, transaction_start_timestamp, + AddNode(vertex_after_update, std::move(name_id_mapper), transaction_start_timestamp, std::vector{&index_.at(label_to_index_.at(added_label)).context_}); } @@ -162,13 +163,13 @@ void TextIndex::UpdateOnRemoveLabel(LabelId removed_label, Vertex *vertex_after_ RemoveNode(vertex_after_update, {&index_.at(label_to_index_.at(removed_label)).context_}); } -void TextIndex::UpdateOnSetProperty(Vertex *vertex_after_update, Storage *storage, +void TextIndex::UpdateOnSetProperty(Vertex *vertex_after_update, NameIdMapper *name_id_mapper, std::uint64_t transaction_start_timestamp) { if (!flags::run_time::GetExperimentalTextSearchEnabled()) { throw query::QueryException("To use text indices, enable the text search feature."); } - UpdateNode(vertex_after_update, storage, transaction_start_timestamp); + UpdateNode(vertex_after_update, std::move(name_id_mapper), transaction_start_timestamp); } std::vector TextIndex::GetApplicableTextIndices(const std::vector &labels) { @@ -222,6 +223,8 @@ bool TextIndex::CreateIndex(const std::string &index_name, LabelId label, memgra bool has_schema = false; std::vector> indexed_properties{}; auto &index_context = index_.at(index_name).context_; + + // TODO antepusic get nodes with label if there's an adequate label index for (const auto &v : db->Vertices(View::NEW)) { if (!v.HasLabel(View::NEW, label).GetValue()) { continue; @@ -431,13 +434,11 @@ std::vector TextIndex::Search(const std::string &index_name, const std::str } catch (const std::exception &e) { throw query::QueryException(fmt::format("Tantivy error: {}", e.what())); } - auto docs = search_results.docs; - for (const auto &doc : docs) { + for (const auto &doc : search_results.docs) { // The CXX .data() method (https://cxx.rs/binding/string.html) may overestimate string length, causing JSON parsing // errors downstream. We prevent this by resizing the converted string with the correctly-working .length() method. - auto doc_data = doc.data; - std::string doc_string = doc_data.data(); - doc_string.resize(doc_data.length()); + std::string doc_string = doc.data.data(); + doc_string.resize(doc.data.length()); auto doc_json = nlohmann::json::parse(doc_string); found_nodes.push_back(storage::Gid::FromString(doc_json["metadata"]["gid"].dump())); } @@ -460,7 +461,7 @@ std::vector> TextIndex::ListIndices() const { std::vector> ret; ret.reserve(index_.size()); for (const auto &[index_name, index_data] : index_) { - ret.push_back({index_name, index_data.scope_}); + ret.emplace_back(index_name, index_data.scope_); } return ret; } diff --git a/src/storage/v2/indices/text_index.hpp b/src/storage/v2/indices/text_index.hpp index ce1c194fe..1ee62ba00 100644 --- a/src/storage/v2/indices/text_index.hpp +++ b/src/storage/v2/indices/text_index.hpp @@ -33,9 +33,8 @@ struct TextIndexData { class TextIndex { private: - void AddNode(Vertex *vertex, Storage *storage, const std::uint64_t transaction_start_timestamp, - const std::vector &applicable_text_indices, - bool skip_commit = kDoSkipCommit); + void AddNode(Vertex *vertex, NameIdMapper *name_id_mapper, const std::uint64_t transaction_start_timestamp, + const std::vector &applicable_text_indices); std::vector GetApplicableTextIndices(const std::vector &labels); @@ -56,22 +55,22 @@ class TextIndex { std::map index_; std::map label_to_index_; - void AddNode(Vertex *vertex, Storage *storage, const std::uint64_t transaction_start_timestamp); + void AddNode(Vertex *vertex, NameIdMapper *name_id_mapper, const std::uint64_t transaction_start_timestamp); - void UpdateNode(Vertex *vertex, Storage *storage, const std::uint64_t transaction_start_timestamp); + void UpdateNode(Vertex *vertex, NameIdMapper *name_id_mapper, const std::uint64_t transaction_start_timestamp); - void UpdateNode(Vertex *vertex, Storage *storage, const std::uint64_t transaction_start_timestamp, + void UpdateNode(Vertex *vertex, NameIdMapper *name_id_mapper, const std::uint64_t transaction_start_timestamp, const std::vector &removed_labels); void RemoveNode(Vertex *vertex); - void UpdateOnAddLabel(LabelId added_label, Vertex *vertex_after_update, Storage *storage, + void UpdateOnAddLabel(LabelId added_label, Vertex *vertex_after_update, NameIdMapper *name_id_mapper, const std::uint64_t transaction_start_timestamp); void UpdateOnRemoveLabel(LabelId removed_label, Vertex *vertex_after_update, const std::uint64_t transaction_start_timestamp); - void UpdateOnSetProperty(Vertex *vertex_after_update, Storage *storage, + void UpdateOnSetProperty(Vertex *vertex_after_update, NameIdMapper *name_id_mapper, const std::uint64_t transaction_start_timestamp); bool CreateIndex(const std::string &index_name, LabelId label, memgraph::query::DbAccessor *db); diff --git a/src/storage/v2/inmemory/storage.cpp b/src/storage/v2/inmemory/storage.cpp index e642df89f..8c07b8f02 100644 --- a/src/storage/v2/inmemory/storage.cpp +++ b/src/storage/v2/inmemory/storage.cpp @@ -877,7 +877,7 @@ utils::BasicResult InMemoryStorage::InMemoryAcce } if (flags::run_time::GetExperimentalTextSearchEnabled()) { - mem_storage->indices_.text_index_->Commit(); + mem_storage->indices_.text_index_.Commit(); } } @@ -1202,7 +1202,7 @@ void InMemoryStorage::InMemoryAccessor::Abort() { storage_->indices_.AbortEntries(property, prop_vertices, transaction_.start_timestamp); } if (flags::run_time::GetExperimentalTextSearchEnabled()) { - storage_->indices_.text_index_->Rollback(); + storage_->indices_.text_index_.Rollback(); } // VERTICES @@ -1803,7 +1803,7 @@ StorageInfo InMemoryStorage::GetInfo(bool force_directory, const auto &lbl = access->ListAllIndices(); info.label_indices = lbl.label.size(); info.label_property_indices = lbl.label_property.size(); - info.text_indices = lbl.text.size(); + info.text_indices = lbl.text_indices.size(); const auto &con = access->ListAllConstraints(); info.existence_constraints = con.existence.size(); info.unique_constraints = con.unique.size(); @@ -2058,12 +2058,12 @@ bool InMemoryStorage::AppendToWal(const Transaction &transaction, uint64_t final final_commit_timestamp); } break; case MetadataDelta::Action::TEXT_INDEX_CREATE: { - const auto &info = md_delta.text; + const auto &info = md_delta.text_indices; AppendToWalDataDefinition(durability::StorageMetadataOperation::TEXT_INDEX_CREATE, info.index_name, info.label, final_commit_timestamp); } break; case MetadataDelta::Action::TEXT_INDEX_DROP: { - const auto &info = md_delta.text; + const auto &info = md_delta.text_indices; AppendToWalDataDefinition(durability::StorageMetadataOperation::TEXT_INDEX_DROP, info.index_name, info.label, final_commit_timestamp); } break; @@ -2274,8 +2274,8 @@ IndicesInfo InMemoryStorage::InMemoryAccessor::ListAllIndices() const { auto *mem_label_index = static_cast(in_memory->indices_.label_index_.get()); auto *mem_label_property_index = static_cast(in_memory->indices_.label_property_index_.get()); - auto *text_index = storage_->indices_.text_index_.get(); - return {mem_label_index->ListIndices(), mem_label_property_index->ListIndices(), text_index->ListIndices()}; + auto &text_index = storage_->indices_.text_index_; + return {mem_label_index->ListIndices(), mem_label_property_index->ListIndices(), text_index.ListIndices()}; } ConstraintsInfo InMemoryStorage::InMemoryAccessor::ListAllConstraints() const { const auto *mem_storage = static_cast(storage_); diff --git a/src/storage/v2/metadata_delta.hpp b/src/storage/v2/metadata_delta.hpp index c8b603dfc..cb80b1ea5 100644 --- a/src/storage/v2/metadata_delta.hpp +++ b/src/storage/v2/metadata_delta.hpp @@ -94,10 +94,10 @@ struct MetadataDelta { : action(Action::LABEL_PROPERTY_INDEX_STATS_CLEAR), label{label} {} MetadataDelta(TextIndexCreate /*tag*/, std::string index_name, LabelId label) - : action(Action::TEXT_INDEX_CREATE), text{index_name, label} {} + : action(Action::TEXT_INDEX_CREATE), text_indices{index_name, label} {} MetadataDelta(TextIndexDrop /*tag*/, std::string index_name, LabelId label) - : action(Action::TEXT_INDEX_DROP), text{index_name, label} {} + : action(Action::TEXT_INDEX_DROP), text_indices{index_name, label} {} MetadataDelta(ExistenceConstraintCreate /*tag*/, LabelId label, PropertyId property) : action(Action::EXISTENCE_CONSTRAINT_CREATE), label_property{label, property} {} @@ -167,7 +167,7 @@ struct MetadataDelta { struct { std::string index_name; LabelId label; - } text; + } text_indices; }; }; diff --git a/src/storage/v2/storage.cpp b/src/storage/v2/storage.cpp index 9bdf2eb6f..0accfbf9f 100644 --- a/src/storage/v2/storage.cpp +++ b/src/storage/v2/storage.cpp @@ -150,7 +150,7 @@ Result> Storage::Accessor::DeleteVertex(VertexAcce } if (flags::run_time::GetExperimentalTextSearchEnabled()) { - storage_->indices_.text_index_->RemoveNode(vertex->vertex_); + storage_->indices_.text_index_.RemoveNode(vertex->vertex_); } const auto &value = res.GetValue(); @@ -191,7 +191,7 @@ Result>>> Stor } if (flags::run_time::GetExperimentalTextSearchEnabled()) { - storage_->indices_.text_index_->RemoveNode(vertex->vertex_); + storage_->indices_.text_index_.RemoveNode(vertex->vertex_); } auto &value = res.GetValue(); @@ -284,7 +284,7 @@ Storage::Accessor::DetachDelete(std::vector nodes, std::vector if (flags::run_time::GetExperimentalTextSearchEnabled()) { for (auto *node : nodes_to_delete) { - storage_->indices_.text_index_->RemoveNode(node); + storage_->indices_.text_index_.RemoveNode(node); } } diff --git a/src/storage/v2/storage.hpp b/src/storage/v2/storage.hpp index 5202a4194..af64527f1 100644 --- a/src/storage/v2/storage.hpp +++ b/src/storage/v2/storage.hpp @@ -61,7 +61,7 @@ class EdgeAccessor; struct IndicesInfo { std::vector label; std::vector> label_property; - std::vector> text; + std::vector> text_indices; }; struct ConstraintsInfo { @@ -228,23 +228,24 @@ class Storage { virtual bool LabelPropertyIndexExists(LabelId label, PropertyId property) const = 0; bool TextIndexExists(const std::string &index_name) const { - return storage_->indices_.text_index_->IndexExists(index_name); + return storage_->indices_.text_index_.IndexExists(index_name); } void TextIndexAddVertex(VertexAccessor *vertex) { - storage_->indices_.text_index_->AddNode(vertex->vertex_, storage_, storage_->timestamp_); + storage_->indices_.text_index_.AddNode(vertex->vertex_, storage_->name_id_mapper_.get(), storage_->timestamp_); } void TextIndexUpdateVertex(VertexAccessor *vertex) { - storage_->indices_.text_index_->UpdateNode(vertex->vertex_, storage_, storage_->timestamp_); + storage_->indices_.text_index_.UpdateNode(vertex->vertex_, storage_->name_id_mapper_.get(), storage_->timestamp_); } void TextIndexUpdateVertex(VertexAccessor *vertex, std::vector removed_labels) { - storage_->indices_.text_index_->UpdateNode(vertex->vertex_, storage_, storage_->timestamp_, removed_labels); + storage_->indices_.text_index_.UpdateNode(vertex->vertex_, storage_->name_id_mapper_.get(), storage_->timestamp_, + removed_labels); } std::vector TextIndexSearch(const std::string &index_name, const std::string &search_query) const { - return storage_->indices_.text_index_->Search(index_name, search_query); + return storage_->indices_.text_index_.Search(index_name, search_query); } virtual IndicesInfo ListAllIndices() const = 0; @@ -294,12 +295,12 @@ class Storage { virtual utils::BasicResult CreateTextIndex(const std::string &index_name, LabelId label, query::DbAccessor *db) { - storage_->indices_.text_index_->CreateIndex(index_name, label, db); + storage_->indices_.text_index_.CreateIndex(index_name, label, db); return {}; } virtual utils::BasicResult DropTextIndex(const std::string &index_name) { - storage_->indices_.text_index_->DropIndex(index_name); + storage_->indices_.text_index_.DropIndex(index_name); return {}; }