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

Fixed an edge case in collection deletion.

Kishore Nallan926a9105d761
diff --git a/src/collection.cpp b/src/collection.cpp
index 79f5901f..c80ef82c 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1999,7 +1999,8 @@ Option<bool> Collection::get_document_from_store(const std::string &seq_id_key,
StoreStatus json_doc_status = store->get(seq_id_key, json_doc_str);
if(json_doc_status != StoreStatus::FOUND) {
- return Option<bool>(500, "Could not locate the JSON document for sequence ID: " + seq_id_key);
+ const std::string& seq_id = std::to_string(get_seq_id_from_key(seq_id_key));
+ return Option<bool>(500, "Could not locate the JSON document for sequence ID: " + seq_id);
}
try {
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 16f4d2fd..dd64ef8d 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -342,11 +342,10 @@ Option<nlohmann::json> CollectionManager::drop_collection(const std::string& col
nlohmann::json collection_json = collection->get_summary_json();
if(remove_from_store) {
- const std::string &collection_id_str = std::to_string(collection->get_collection_id());
+ const std::string& del_key_prefix = std::to_string(collection->get_collection_id()) + "_";
- // Note: The order of dropping documents first before dropping collection meta is important for replication
- rocksdb::Iterator* iter = store->scan(collection_id_str);
- while(iter->Valid() && iter->key().starts_with(collection_id_str)) {
+ rocksdb::Iterator* iter = store->scan(del_key_prefix);
+ while(iter->Valid() && iter->key().starts_with(del_key_prefix)) {
store->remove(iter->key().ToString());
iter->Next();
}
diff --git a/src/core_api.cpp b/src/core_api.cpp
index c135ff35..5804af5b 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -117,7 +117,7 @@ bool post_create_collection(const std::shared_ptr<http_req>& req, const std::sha
bool del_drop_collection(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
std::string doc_id = req->params["id"];
CollectionManager & collectionManager = CollectionManager::get_instance();
- Option<nlohmann::json> drop_op = collectionManager.drop_collection(req->params["collection"]);
+ Option<nlohmann::json> drop_op = collectionManager.drop_collection(req->params["collection"], true);
if(!drop_op.ok()) {
res->set(drop_op.code(), drop_op.error());
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index b5570d70..015ba78b 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -142,4 +142,36 @@ TEST_F(CollectionSpecificTest, ExplicitHighlightFieldsConfig) {
ASSERT_EQ(0, results["hits"][0]["highlights"].size());
collectionManager.drop_collection("coll1");
-}
\ No newline at end of file
+}
+
+TEST_F(CollectionSpecificTest, CreateManyCollectionsAndDeleteOneOfThem) {
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ for(size_t i = 0; i <= 10; i++) {
+ const std::string& coll_name = "coll" + std::to_string(i);
+ collectionManager.drop_collection(coll_name);
+ ASSERT_TRUE(collectionManager.create_collection(coll_name, 1, fields, "points").ok());
+ }
+
+ auto coll1 = collectionManager.get_collection_unsafe("coll1");
+ auto coll10 = collectionManager.get_collection_unsafe("coll10");
+
+ nlohmann::json doc;
+ doc["id"] = "0";
+ doc["title"] = "The quick brown fox was too fast.";
+ doc["points"] = 100;
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+ ASSERT_TRUE(coll10->add(doc.dump()).ok());
+
+ collectionManager.drop_collection("coll1", true);
+
+ // Record with id "0" should exist in coll10
+ ASSERT_TRUE(coll10->get("0").ok());
+
+ for(size_t i = 0; i <= 10; i++) {
+ const std::string& coll_name = "coll" + std::to_string(i);
+ collectionManager.drop_collection(coll_name);
+ }
+}