From c0eeb0b129a76b930ccb881c4ea3ae7d9fc82321 Mon Sep 17 00:00:00 2001 From: Marcel Maltry Date: Mon, 12 Feb 2024 15:52:41 +0100 Subject: [PATCH] [Schema] Add `is_valid` flag for indexes We currently do not support automatic updates on indexes. To ensure correct query results, inserts on tables will now invalidate all indexes on that table such that the indexes are no longer used to answer queries. --- include/mutable/catalog/Schema.hpp | 21 ++++++++++++++++----- src/catalog/DatabaseCommand.cpp | 2 ++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/mutable/catalog/Schema.hpp b/include/mutable/catalog/Schema.hpp index 8c556341..63054ddd 100644 --- a/include/mutable/catalog/Schema.hpp +++ b/include/mutable/catalog/Schema.hpp @@ -873,6 +873,7 @@ struct M_EXPORT Database const Table &table; ///< the table of the index const Attribute &attribute; ///< the indexed attribute std::unique_ptr index; ///< the actual index + bool is_valid; ///< indicates if the index should be used to answer queries index_entry_type(const char *name, const Table &table, const Attribute &attribute, std::unique_ptr index) @@ -880,6 +881,7 @@ struct M_EXPORT Database , table(table) , attribute(attribute) , index(std::move(index)) + , is_valid(true) { } }; @@ -986,7 +988,7 @@ struct M_EXPORT Database if (it->name == index_name) return true; return false; } - /** Returns `true` iff there is an index using \p method on \p attribute_name of \p table_name. Throws + /** Returns `true` iff there is a valid index using \p method on \p attribute_name of \p table_name. Throws * `m::invalid_argument` if a `Table` with the given \p table_name does not exist. Throws `m::invalid_argument` if * an `Attribute` with \p attribute_name does not exist in `Table` \p table_name. */ bool has_index(const char *table_name, const char *attribute_name, idx::IndexMethod method) const { @@ -997,7 +999,7 @@ struct M_EXPORT Database if (not table->has_attribute(attribute_name)) throw m::invalid_argument("Attribute with that name does not exist."); for (auto &entry : indexes_) { - if (entry.table.name() == table_name and entry.attribute.name == attribute_name and + if (entry.is_valid and entry.table.name() == table_name and entry.attribute.name == attribute_name and entry.index->method() == method) { return true; @@ -1014,7 +1016,7 @@ struct M_EXPORT Database } throw m::invalid_argument("Index of that name does not exist."); } - /** Returns an index using \p method on \p attribute_name of \p table_name iff one exists. Throws + /** Returns a valid index using \p method on \p attribute_name of \p table_name iff one exists. Throws * `m::invalid_argument` if such an index does not exist. Throws `m::invalid_argument` if a `Table` with the given * \p table_name does not exist. Throws `m::invalid_argument` if an `Attribute` with \p attribute_name does not * exist in `Table` \p table_name. */ @@ -1026,7 +1028,7 @@ struct M_EXPORT Database if (not table->has_attribute(attribute_name)) throw m::invalid_argument("Attribute with that name does not exist."); for (auto &entry : indexes_) { - if (entry.table.name() == table_name and entry.attribute.name == attribute_name and + if (entry.is_valid and entry.table.name() == table_name and entry.attribute.name == attribute_name and entry.index->method() == method) { return *entry.index; @@ -1034,7 +1036,16 @@ struct M_EXPORT Database } throw m::invalid_argument("Index of that method on that attribute of that table does not exist."); } - + /** Invalidates all indexes on attributes of `Table` \p table_name s.t. they are no longer used to answer queries. + * Throws `m_invalid_argument` if a `Table` with the given \p table_name does not exist. */ + void invalidate_indexes(const char *table_name) { + if (not has_table(table_name)) + throw m::invalid_argument("Table with that name does not exist."); + for (auto &entry : indexes_) { + if (entry.table.name() == table_name) + entry.is_valid = false; + } + } }; } diff --git a/src/catalog/DatabaseCommand.cpp b/src/catalog/DatabaseCommand.cpp index a8f62d0d..b60d165c 100644 --- a/src/catalog/DatabaseCommand.cpp +++ b/src/catalog/DatabaseCommand.cpp @@ -170,6 +170,8 @@ void InsertRecords::execute(Diagnostic&) W.append(tup); } + /* Invalidate all indexes on the table. */ + DB.invalidate_indexes(T.name()); } void UpdateRecords::execute(Diagnostic&)