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

Finer locking during collection creation.

Kishore Nallanbb5d6a308dbd
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 48011ed2..8b293309 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -292,6 +292,7 @@ Option<Collection*> CollectionManager::create_collection(const std::string& name
const std::string& fallback_field_type,
const std::vector<std::string>& symbols_to_index,
const std::vector<std::string>& token_separators) {
+ 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.");
@@ -340,6 +341,7 @@ Option<Collection*> CollectionManager::create_collection(const std::string& name
return Option<Collection*>(500, "Could not write to on-disk storage.");
}
+ lock.unlock();
add_to_collections(new_collection);
return Option<Collection*>(new_collection);
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 06cd86e2..b8faad9e 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -104,6 +104,35 @@ TEST_F(CollectionManagerTest, CollectionCreation) {
ASSERT_EQ("1", next_collection_id);
}
+TEST_F(CollectionManagerTest, ParallelCollectionCreation) {
+ std::vector<std::thread> threads;
+ for(size_t i = 0; i < 10; i++) {
+ threads.emplace_back([i, &collectionManager = collectionManager]() {
+ nlohmann::json coll_json = R"({
+ "name": "parcoll",
+ "fields": [
+ {"name": "title", "type": "string"}
+ ]
+ })"_json;
+ coll_json["name"] = coll_json["name"].get<std::string>() + std::to_string(i+1);
+ auto coll_op = collectionManager.create_collection(coll_json);
+ ASSERT_TRUE(coll_op.ok());
+ });
+ }
+
+ for(auto& thread : threads){
+ thread.join();
+ }
+
+ int64_t prev_id = INT32_MAX;
+
+ for(auto coll: collectionManager.get_collections()) {
+ // collections are sorted by ID, in descending order
+ ASSERT_TRUE(coll->get_collection_id() < prev_id);
+ prev_id = coll->get_collection_id();
+ }
+}
+
TEST_F(CollectionManagerTest, ShouldInitCollection) {
nlohmann::json collection_meta1 =
nlohmann::json::parse("{\"name\": \"foobar\", \"id\": 100, \"fields\": [{\"name\": \"org\", \"type\": "