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

Unique lock before dropping a collection.

Kishore Nallan68e25fac0916
diff --git a/src/collection.cpp b/src/collection.cpp
index 05268e26..5b83ab6a 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -54,6 +54,8 @@ Collection::Collection(const std::string& name, const uint32_t collection_id, co
}
Collection::~Collection() {
+ std::unique_lock lock(mutex);
+
for(size_t i = 0; i < indices.size(); i++) {
delete indices[i];
}
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index bf88c86d..5d1ea24b 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -328,7 +328,9 @@ std::vector<Collection*> CollectionManager::get_collections() const {
}
Option<nlohmann::json> CollectionManager::drop_collection(const std::string& collection_name, const bool remove_from_store) {
- auto collection = get_collection(collection_name); // locked_resource_view_t
+ std::unique_lock lock(mutex);
+
+ auto collection = get_collection_unsafe(collection_name);
if(collection == nullptr) {
return Option<nlohmann::json>(404, "No collection with name `" + collection_name + "` found.");
@@ -357,7 +359,7 @@ Option<nlohmann::json> CollectionManager::drop_collection(const std::string& col
collections.erase(actual_coll_name);
collection_id_names.erase(collection->get_collection_id());
- delete collection.get();
+ delete collection;
return Option<nlohmann::json>(collection_json);
}
diff --git a/src/index.cpp b/src/index.cpp
index 203ace2c..83278f55 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -48,6 +48,8 @@ Index::Index(const std::string name, const std::unordered_map<std::string, field
}
Index::~Index() {
+ std::unique_lock lock(mutex);
+
for(auto & name_tree: search_index) {
art_tree_destroy(name_tree.second);
delete name_tree.second;
@@ -388,6 +390,7 @@ Option<uint32_t> Index::validate_index_in_memory(nlohmann::json& document, uint3
void Index::scrub_reindex_doc(nlohmann::json& update_doc, nlohmann::json& del_doc, nlohmann::json& old_doc) {
std::vector<std::string> del_keys;
+ std::shared_lock lock(mutex);
for(auto it = del_doc.cbegin(); it != del_doc.cend(); it++) {
const std::string& field_name = it.key();