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
Fix cascade deletion bug. (#1742)
Harpreet Sangar8e808d4e29f6
diff --git a/include/collection.h b/include/collection.hindex cadfe47e..956d2d9c 100644--- a/include/collection.h+++ b/include/collection.h@@ -601,6 +601,8 @@ public:Option<nlohmann::json> get(const std::string & id) const;+ void remove_ref_docs(const uint32_t& count, uint32_t const* const docs, bool remove_from_store = true);+Option<std::string> remove(const std::string & id, bool remove_from_store = true);Option<bool> remove_if_found(uint32_t seq_id, bool remove_from_store = true);diff --git a/src/collection.cpp b/src/collection.cppindex 00290da5..c00b6522 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -4435,12 +4435,33 @@ void Collection::remove_document(const nlohmann::json & document, const uint32_tfor (const auto &item: referenced_in) {auto ref_coll = collectionManager.get_collection(item.first);if (ref_coll != nullptr) {+ // Delete all the docs where reference helper field has value `seq_id`.filter_result_t filter_result;- ref_coll->get_filter_ids(item.second + ":=" + id, filter_result);- for (uint32_t i = 0; i < filter_result.count; i++) {- ref_coll->remove(std::to_string(filter_result.docs[i]));+ ref_coll->get_filter_ids(item.second + ":" + std::to_string(seq_id), filter_result);++ ref_coll->remove_ref_docs(filter_result.count, filter_result.docs, remove_from_store);+ }+ }+}++void Collection::remove_ref_docs(const uint32_t& count, uint32_t const* const docs, bool remove_from_store) {+ for (uint32_t i = 0; i < count; i++) {+ auto const& seq_id = docs[i];++ nlohmann::json document;+ auto get_doc_op = get_document_from_store(get_seq_id_key(seq_id), document);++ if( !get_doc_op.ok()) {+ if (get_doc_op.code() == 404) {+ LOG(ERROR) << "`" << name << "` collection: Sequence ID `" << seq_id << "` exists, but document is missing.";+ continue;}++ LOG(ERROR) << "`" << name << "` collection: " << get_doc_op.error();+ continue;}++ remove_document(document, seq_id, remove_from_store);}}diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cppindex d77ce55f..f1c6dd04 100644--- a/test/collection_join_test.cpp+++ b/test/collection_join_test.cpp@@ -4280,6 +4280,176 @@ TEST_F(CollectionJoinTest, CascadeDeletion) {ASSERT_EQ(1, res_obj["found"].get<size_t>());ASSERT_EQ("product_b", res_obj["hits"][0]["document"].at("product_id"));ASSERT_EQ("user_a", res_obj["hits"][0]["document"].at("user_id"));++ schema_json =+ R"({+ "name": "document",+ "fields": [+ {"name": "name", "type": "string"}+ ]+ })"_json;+ documents = {+ R"({+ "id": "1",+ "name": "doc_1"+ })"_json,+ R"({+ "id": "2",+ "name": "doc_2"+ })"_json,+ R"({+ "id": "3",+ "name": "doc_3"+ })"_json,+ };+ collection_create_op = collectionManager.create_collection(schema_json);+ ASSERT_TRUE(collection_create_op.ok());+ for (auto const &json: documents) {+ auto add_op = collection_create_op.get()->add(json.dump());+ ASSERT_TRUE(add_op.ok());+ }++ schema_json =+ R"({+ "name": "lead",+ "fields": [+ {"name": "name", "type": "string"}+ ]+ })"_json;+ documents = {+ R"({+ "id": "1",+ "name": "lead_1"+ })"_json,+ R"({+ "id": "2",+ "name": "lead_2"+ })"_json,+ R"({+ "id": "3",+ "name": "lead_3"+ })"_json+ };+ collection_create_op = collectionManager.create_collection(schema_json);+ ASSERT_TRUE(collection_create_op.ok());+ for (auto const &json: documents) {+ auto add_op = collection_create_op.get()->add(json.dump());+ ASSERT_TRUE(add_op.ok());+ }++ schema_json =+ R"({+ "name": "lead_document",+ "fields": [+ {"name": "leadId", "type": "string", "reference":"lead.id"},+ {"name": "documentId", "type": "string", "reference":"document.id"}+ ]+ })"_json;+ documents = {+ R"({+ "id": "1",+ "leadId": "1",+ "documentId": "1"+ })"_json,+ R"({+ "id": "2",+ "leadId": "2",+ "documentId": "2"+ })"_json,+ R"({+ "id": "3",+ "leadId": "3",+ "documentId": "2"+ })"_json+ };+ collection_create_op = collectionManager.create_collection(schema_json);+ ASSERT_TRUE(collection_create_op.ok());+ for (auto const &json: documents) {+ auto add_op = collection_create_op.get()->add(json.dump());+ ASSERT_TRUE(add_op.ok());+ }++ req_params = {+ {"collection", "lead_document"},+ {"q", "*"},+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(3, res_obj["found"].get<size_t>());++ ASSERT_EQ("3", res_obj["hits"][0]["document"].at("leadId"));+ ASSERT_EQ("2", res_obj["hits"][0]["document"].at("documentId"));++ ASSERT_EQ("2", res_obj["hits"][1]["document"].at("leadId"));+ ASSERT_EQ("2", res_obj["hits"][1]["document"].at("documentId"));++ ASSERT_EQ("1", res_obj["hits"][2]["document"].at("leadId"));+ ASSERT_EQ("1", res_obj["hits"][2]["document"].at("documentId"));++ collectionManager.get_collection_unsafe("document")->remove("1");++ req_params = {+ {"collection", "lead_document"},+ {"q", "*"},+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(2, res_obj["found"].get<size_t>());++ ASSERT_EQ("3", res_obj["hits"][0]["document"].at("leadId"));+ ASSERT_EQ("2", res_obj["hits"][0]["document"].at("documentId"));++ ASSERT_EQ("2", res_obj["hits"][1]["document"].at("leadId"));+ ASSERT_EQ("2", res_obj["hits"][1]["document"].at("documentId"));++ auto doc = R"({+ "id": "1",+ "leadId": "1",+ "documentId": "3"+ })"_json;+ auto add_doc_op = collectionManager.get_collection_unsafe("lead_document")->add(doc.dump());+ ASSERT_TRUE(add_doc_op.ok());++ req_params = {+ {"collection", "lead_document"},+ {"q", "*"},+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(3, res_obj["found"].get<size_t>());++ ASSERT_EQ("1", res_obj["hits"][0]["document"].at("leadId"));+ ASSERT_EQ("3", res_obj["hits"][0]["document"].at("documentId"));++ ASSERT_EQ("3", res_obj["hits"][1]["document"].at("leadId"));+ ASSERT_EQ("2", res_obj["hits"][1]["document"].at("documentId"));++ ASSERT_EQ("2", res_obj["hits"][2]["document"].at("leadId"));+ ASSERT_EQ("2", res_obj["hits"][2]["document"].at("documentId"));++ collectionManager.get_collection_unsafe("lead")->remove("1");++ req_params = {+ {"collection", "lead_document"},+ {"q", "*"},+ };+ search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(2, res_obj["found"].get<size_t>());++ ASSERT_EQ("3", res_obj["hits"][0]["document"].at("leadId"));+ ASSERT_EQ("2", res_obj["hits"][0]["document"].at("documentId"));++ ASSERT_EQ("2", res_obj["hits"][1]["document"].at("leadId"));+ ASSERT_EQ("2", res_obj["hits"][1]["document"].at("documentId"));}TEST_F(CollectionJoinTest, SortByReference) {