From a5d849b5e94dc91eb4162b8dee206717959ad710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Pu=C5=A1i=C4=87?= Date: Fri, 5 Jan 2024 00:18:46 +0100 Subject: [PATCH] Add text index creation and deletion --- src/query/db_accessor.hpp | 10 ++++++- src/storage/v2/indices/indices.cpp | 3 +- src/storage/v2/indices/text_index.hpp | 40 +++++++++++++++++++++------ src/storage/v2/storage.hpp | 13 ++++++++- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/query/db_accessor.hpp b/src/query/db_accessor.hpp index ed7dde409..2aef3bb1b 100644 --- a/src/query/db_accessor.hpp +++ b/src/query/db_accessor.hpp @@ -1,4 +1,4 @@ -// Copyright 2023 Memgraph Ltd. +// Copyright 2024 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -628,6 +628,14 @@ class DbAccessor final { return accessor_->DropIndex(label, property); } + utils::BasicResult CreateTextIndex(storage::LabelId label) { + return accessor_->CreateTextIndex(label); + } + + utils::BasicResult DropTextIndex(storage::LabelId label) { + return accessor_->DropTextIndex(label); + } + utils::BasicResult CreateExistenceConstraint( storage::LabelId label, storage::PropertyId property) { return accessor_->CreateExistenceConstraint(label, property); diff --git a/src/storage/v2/indices/indices.cpp b/src/storage/v2/indices/indices.cpp index e0b194ad4..ccf92ea30 100644 --- a/src/storage/v2/indices/indices.cpp +++ b/src/storage/v2/indices/indices.cpp @@ -1,4 +1,4 @@ -// Copyright 2023 Memgraph Ltd. +// Copyright 2024 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -55,6 +55,7 @@ void Indices::UpdateOnSetProperty(PropertyId property, const PropertyValue &valu 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/text_index.hpp b/src/storage/v2/indices/text_index.hpp index 223f48709..dee42b576 100644 --- a/src/storage/v2/indices/text_index.hpp +++ b/src/storage/v2/indices/text_index.hpp @@ -9,6 +9,8 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. +#include "utils/memcxx.hpp" + namespace memgraph::storage { class TextIndex { @@ -20,20 +22,42 @@ class TextIndex { TextIndex &operator=(const TextIndex &) = delete; TextIndex &operator=(TextIndex &&) = delete; - virtual ~TextIndex() = default; + ~TextIndex() = default; - virtual void UpdateOnAddLabel(LabelId added_label, Vertex *vertex_after_update, const Transaction &tx) = 0; + void UpdateOnAddLabel(LabelId added_label, Vertex *vertex_after_update, const Transaction &tx) {} - // Not used for in-memory - virtual void UpdateOnRemoveLabel(LabelId removed_label, Vertex *vertex_after_update, const Transaction &tx) = 0; + void UpdateOnRemoveLabel(LabelId removed_label, Vertex *vertex_after_update, const Transaction &tx) {} - virtual bool DropIndex(LabelId label) = 0; + /// @throw std::bad_alloc + bool CreateIndex(LabelId label, const std::optional ¶llel_exec_info) { + auto index_config = memcxx_mock::text_search::IndexConfig{.mappings = "TODO antepusic"}; + auto new_index = memcxx_mock::text_search::Mock::create_index(label.ToString(), index_config); + index_[label] = new_index; + return true; - virtual bool IndexExists(LabelId label) const = 0; + // TODO add documents to index + } - virtual std::vector ListIndices() const = 0; + bool DropIndex(LabelId label) { + memcxx_mock::text_search::Mock::drop_index(label.ToString()); + index_.erase(label); + return true; + } - virtual uint64_t ApproximateVertexCount(LabelId label) const = 0; + bool IndexExists(LabelId label) { return index_.contains(label); } + + std::vector ListIndices() { + std::vector ret; + ret.reserve(index_.size()); + for (const auto &item : index_) { + ret.push_back(item.first); + } + return ret; + } + + uint64_t ApproximateVertexCount(LabelId label) { return 10; } + + std::map index_; }; } // namespace memgraph::storage diff --git a/src/storage/v2/storage.hpp b/src/storage/v2/storage.hpp index bf1973bcd..58289f74c 100644 --- a/src/storage/v2/storage.hpp +++ b/src/storage/v2/storage.hpp @@ -1,4 +1,4 @@ -// Copyright 2023 Memgraph Ltd. +// Copyright 2024 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -255,6 +255,17 @@ class Storage { virtual utils::BasicResult DropIndex(LabelId label, PropertyId property) = 0; + virtual utils::BasicResult CreateTextIndex(LabelId label) { + // TODO: pass vertices to CreateIndex + storage_->indices_.text_index_->CreateIndex(label, std::nullopt); + return {}; + } + + virtual utils::BasicResult DropTextIndex(LabelId label) { + storage_->indices_.text_index_->DropIndex(label); + return {}; + } + virtual utils::BasicResult CreateExistenceConstraint( LabelId label, PropertyId property) = 0;