Skip to content

Commit c0eeb0b

Browse files
committed
[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.
1 parent bed06aa commit c0eeb0b

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

include/mutable/catalog/Schema.hpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -873,13 +873,15 @@ struct M_EXPORT Database
873873
const Table &table; ///< the table of the index
874874
const Attribute &attribute; ///< the indexed attribute
875875
std::unique_ptr<idx::IndexBase> index; ///< the actual index
876+
bool is_valid; ///< indicates if the index should be used to answer queries
876877

877878
index_entry_type(const char *name, const Table &table, const Attribute &attribute,
878879
std::unique_ptr<idx::IndexBase> index)
879880
: name(name)
880881
, table(table)
881882
, attribute(attribute)
882883
, index(std::move(index))
884+
, is_valid(true)
883885
{ }
884886
};
885887

@@ -986,7 +988,7 @@ struct M_EXPORT Database
986988
if (it->name == index_name) return true;
987989
return false;
988990
}
989-
/** Returns `true` iff there is an index using \p method on \p attribute_name of \p table_name. Throws
991+
/** Returns `true` iff there is a valid index using \p method on \p attribute_name of \p table_name. Throws
990992
* `m::invalid_argument` if a `Table` with the given \p table_name does not exist. Throws `m::invalid_argument` if
991993
* an `Attribute` with \p attribute_name does not exist in `Table` \p table_name. */
992994
bool has_index(const char *table_name, const char *attribute_name, idx::IndexMethod method) const {
@@ -997,7 +999,7 @@ struct M_EXPORT Database
997999
if (not table->has_attribute(attribute_name))
9981000
throw m::invalid_argument("Attribute with that name does not exist.");
9991001
for (auto &entry : indexes_) {
1000-
if (entry.table.name() == table_name and entry.attribute.name == attribute_name and
1002+
if (entry.is_valid and entry.table.name() == table_name and entry.attribute.name == attribute_name and
10011003
entry.index->method() == method)
10021004
{
10031005
return true;
@@ -1014,7 +1016,7 @@ struct M_EXPORT Database
10141016
}
10151017
throw m::invalid_argument("Index of that name does not exist.");
10161018
}
1017-
/** Returns an index using \p method on \p attribute_name of \p table_name iff one exists. Throws
1019+
/** Returns a valid index using \p method on \p attribute_name of \p table_name iff one exists. Throws
10181020
* `m::invalid_argument` if such an index does not exist. Throws `m::invalid_argument` if a `Table` with the given
10191021
* \p table_name does not exist. Throws `m::invalid_argument` if an `Attribute` with \p attribute_name does not
10201022
* exist in `Table` \p table_name. */
@@ -1026,15 +1028,24 @@ struct M_EXPORT Database
10261028
if (not table->has_attribute(attribute_name))
10271029
throw m::invalid_argument("Attribute with that name does not exist.");
10281030
for (auto &entry : indexes_) {
1029-
if (entry.table.name() == table_name and entry.attribute.name == attribute_name and
1031+
if (entry.is_valid and entry.table.name() == table_name and entry.attribute.name == attribute_name and
10301032
entry.index->method() == method)
10311033
{
10321034
return *entry.index;
10331035
}
10341036
}
10351037
throw m::invalid_argument("Index of that method on that attribute of that table does not exist.");
10361038
}
1037-
1039+
/** Invalidates all indexes on attributes of `Table` \p table_name s.t. they are no longer used to answer queries.
1040+
* Throws `m_invalid_argument` if a `Table` with the given \p table_name does not exist. */
1041+
void invalidate_indexes(const char *table_name) {
1042+
if (not has_table(table_name))
1043+
throw m::invalid_argument("Table with that name does not exist.");
1044+
for (auto &entry : indexes_) {
1045+
if (entry.table.name() == table_name)
1046+
entry.is_valid = false;
1047+
}
1048+
}
10381049
};
10391050

10401051
}

src/catalog/DatabaseCommand.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ void InsertRecords::execute(Diagnostic&)
170170

171171
W.append(tup);
172172
}
173+
/* Invalidate all indexes on the table. */
174+
DB.invalidate_indexes(T.name());
173175
}
174176

175177
void UpdateRecords::execute(Diagnostic&)

0 commit comments

Comments
 (0)