diff --git a/src/search/index_info.h b/src/search/index_info.h index 3badc372adc..6728041298e 100644 --- a/src/search/index_info.h +++ b/src/search/index_info.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -52,10 +53,12 @@ struct FieldInfo { struct IndexInfo { using FieldMap = std::map; + using MutexMap = std::map; std::string name; redis::IndexMetadata metadata; FieldMap fields; + mutable MutexMap field_mutexes; redis::IndexPrefixes prefixes; std::string ns; @@ -63,10 +66,15 @@ struct IndexInfo { : name(std::move(name)), metadata(std::move(metadata)), ns(std::move(ns)) {} void Add(FieldInfo &&field) { - const auto &name = field.name; + auto name = field.name; field.index = this; fields.emplace(name, std::move(field)); + field_mutexes.emplace(std::piecewise_construct, std::make_tuple(name), std::make_tuple()); } + + void LockField(const std::string &field_name) const { field_mutexes.at(field_name).lock(); } + + void UnLockField(const std::string &field_name) const { field_mutexes.at(field_name).unlock(); } }; struct IndexMap : std::map> { diff --git a/src/search/indexer.cc b/src/search/indexer.cc index 576de07343a..8294dc9eb6b 100644 --- a/src/search/indexer.cc +++ b/src/search/indexer.cc @@ -304,19 +304,23 @@ Status IndexUpdater::UpdateIndex(const std::string &field, std::string_view key, return {Status::NotOK, "No such field to do index updating"}; } + info->LockField(field); + auto *metadata = iter->second.metadata.get(); SearchKey search_key(info->ns, info->name, field); + Status status; if (auto tag = dynamic_cast(metadata)) { - GET_OR_RET(UpdateTagIndex(key, original, current, search_key, tag)); + status = UpdateTagIndex(key, original, current, search_key, tag); } else if (auto numeric [[maybe_unused]] = dynamic_cast(metadata)) { - GET_OR_RET(UpdateNumericIndex(key, original, current, search_key, numeric)); + status = UpdateNumericIndex(key, original, current, search_key, numeric); } else if (auto vector = dynamic_cast(metadata)) { - GET_OR_RET(UpdateHnswVectorIndex(key, original, current, search_key, vector)); + status = UpdateHnswVectorIndex(key, original, current, search_key, vector); } else { - return {Status::NotOK, "Unexpected field type"}; + status = {Status::NotOK, "Unexpected field type"}; } - return Status::OK(); + info->UnLockField(field); + return status; } Status IndexUpdater::Update(const FieldValues &original, std::string_view key) const {