Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Improve locking for synonym index.

Kishore Nallan525b75268c0d
diff --git a/src/synonym_index.cpp b/src/synonym_index.cpp
index cfbde766..537423d3 100644
--- a/src/synonym_index.cpp
+++ b/src/synonym_index.cpp
@@ -94,6 +94,7 @@ void SynonymIndex::synonym_reduction_internal(const std::vector<std::string>& to
void SynonymIndex::synonym_reduction(const std::vector<std::string>& tokens,
std::vector<std::vector<std::string>>& results) const {
+ std::shared_lock lock(mutex);
if(synonym_definitions.empty()) {
return;
}
@@ -104,7 +105,9 @@ void SynonymIndex::synonym_reduction(const std::vector<std::string>& tokens,
Option<bool> SynonymIndex::add_synonym(const std::string & collection_name, const synonym_t& synonym,
bool write_to_store) {
+ std::unique_lock write_lock(mutex);
if(synonym_definitions.count(synonym.id) != 0) {
+ write_lock.unlock();
// first we have to delete existing entries so we can upsert
Option<bool> rem_op = remove_synonym(collection_name, synonym.id);
if(!rem_op.ok()) {
@@ -112,7 +115,7 @@ Option<bool> SynonymIndex::add_synonym(const std::string & collection_name, cons
}
}
- std::unique_lock write_lock(mutex);
+ write_lock.lock();
synonym_definitions[synonym.id] = synonym;
if(!synonym.root.empty()) {