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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Refactor collection resource locking.
Kishore Nallan816c56fadac7
diff --git a/include/collection.h b/include/collection.hindex a48200a7..cb816e25 100644--- a/include/collection.h+++ b/include/collection.h@@ -51,7 +51,8 @@ private:mutable std::shared_mutex mutex;- mutable std::shared_mutex index_repair_lock;+ // ensures that a Collection* is not destructed while in use by multiple threads+ mutable std::shared_mutex lifecycle_mutex;const uint8_t CURATED_RECORD_IDENTIFIER = 100;@@ -578,6 +579,8 @@ public:std::vector<search_field_t>& weighted_search_fields) const;Option<bool> truncate_after_top_k(const std::string& field_name, size_t k);++ std::shared_mutex& get_lifecycle_mutex();};template<class T>diff --git a/include/collection_manager.h b/include/collection_manager.hindex b11d4c1e..1ef88ee9 100644--- a/include/collection_manager.h+++ b/include/collection_manager.h@@ -53,7 +53,7 @@ class CollectionManager {private:mutable std::shared_mutex mutex;- mutable std::mutex coll_create_mutex;+ mutable std::shared_mutex noop_coll_mutex;Store *store;ThreadPool* thread_pool;diff --git a/src/collection.cpp b/src/collection.cppindex 29a17688..040a1310 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -57,8 +57,8 @@ Collection::Collection(const std::string& name, const uint32_t collection_id, co}Collection::~Collection() {+ std::unique_lock lifecycle_lock(lifecycle_mutex);std::unique_lock lock(mutex);- std::unique_lock repair_lock(index_repair_lock);delete index;delete synonym_index;}@@ -5195,6 +5195,10 @@ Option<bool> Collection::truncate_after_top_k(const string &field_name, size_t kreturn Option<bool>(true);}+std::shared_mutex& Collection::get_lifecycle_mutex() {+ return lifecycle_mutex;+}+void Collection::remove_embedding_field(const std::string& field_name) {if(embedding_fields.find(field_name) == embedding_fields.end()) {return;@@ -5211,6 +5215,6 @@ tsl::htrie_map<char, field> Collection::get_embedding_fields_unsafe() {}void Collection::do_housekeeping() {- std::unique_lock lock(index_repair_lock);+ std::unique_lock lock(lifecycle_mutex);index->repair_hnsw_index();}diff --git a/src/collection_manager.cpp b/src/collection_manager.cppindex 236dd22a..7b8510cf 100644--- a/src/collection_manager.cpp+++ b/src/collection_manager.cpp@@ -369,7 +369,7 @@ Option<Collection*> CollectionManager::create_collection(const std::string& nameconst std::vector<std::string>& symbols_to_index,const std::vector<std::string>& token_separators,const bool enable_nested_fields) {- std::unique_lock lock(coll_create_mutex);+ std::unique_lock lock(mutex);if(store->contains(Collection::get_meta_key(name))) {return Option<Collection*>(409, std::string("A collection with name `") + name + "` already exists.");@@ -420,6 +420,7 @@ Option<Collection*> CollectionManager::create_collection(const std::string& namereturn Option<Collection*>(500, "Could not write to on-disk storage.");}+ lock.unlock();add_to_collections(new_collection);return Option<Collection*>(new_collection);@@ -444,7 +445,8 @@ Collection* CollectionManager::get_collection_unsafe(const std::string & collectlocked_resource_view_t<Collection> CollectionManager::get_collection(const std::string & collection_name) const {std::shared_lock lock(mutex);Collection* coll = get_collection_unsafe(collection_name);- return locked_resource_view_t<Collection>(mutex, coll);+ return coll != nullptr ? locked_resource_view_t<Collection>(coll->get_lifecycle_mutex(), coll) :+ locked_resource_view_t<Collection>(noop_coll_mutex, coll);}locked_resource_view_t<Collection> CollectionManager::get_collection_with_id(uint32_t collection_id) const {