mirror of
https://github.com/typesense/typesense.git
synced 2025-05-18 12:42:50 +08:00
Fixed an edge case in collection deletion.
This commit is contained in:
parent
10efdea611
commit
ba212a4c4e
@ -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 {
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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());
|
||||
|
@ -142,4 +142,36 @@ TEST_F(CollectionSpecificTest, ExplicitHighlightFieldsConfig) {
|
||||
ASSERT_EQ(0, results["hits"][0]["highlights"].size());
|
||||
|
||||
collectionManager.drop_collection("coll1");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user