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

Address some warnings related to update doc scrubbing.

kishorencb2fba69a739d
diff --git a/include/index.h b/include/index.h
index 5c2e1524..d7ab0a9e 100644
--- a/include/index.h
+++ b/include/index.h
@@ -108,10 +108,10 @@ struct index_record {
size_t position; // position of record in the original request
uint32_t seq_id;
- nlohmann::json doc;
- nlohmann::json old_doc;
- nlohmann::json new_doc;
- nlohmann::json del_doc;
+ nlohmann::json doc; // actual document sent in request (could be partial)
+ nlohmann::json old_doc; // previously stored *full* document from disk
+ nlohmann::json new_doc; // new *full* document to be stored into disk
+ nlohmann::json del_doc; // document containing the fields that should be deleted
index_operation_t operation;
bool is_update;
diff --git a/src/collection.cpp b/src/collection.cpp
index 5e6060d1..faa0aa8f 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -224,6 +224,7 @@ void Collection::get_doc_changes(const nlohmann::json &document, nlohmann::json
}
for(auto it = document.begin(); it != document.end(); ++it) {
+ // adds new key or overrides existing key from `old_doc`
new_doc[it.key()] = it.value();
// if the update document contains a field that exists in old, we record that (for delete + reindex)
diff --git a/src/index.cpp b/src/index.cpp
index c9520e59..6a41f59f 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -352,18 +352,21 @@ Option<uint32_t> Index::validate_index_in_memory(const nlohmann::json &document,
}
void Index::scrub_reindex_doc(nlohmann::json& update_doc, nlohmann::json& del_doc, nlohmann::json& old_doc) {
- auto it = del_doc.cbegin();
- while(it != del_doc.cend()) {
+ std::vector<std::string> del_keys;
+
+ for(auto it = del_doc.cbegin(); it != del_doc.cend(); it++) {
const std::string& field_name = it.key();
const auto& search_field_it = search_schema.find(field_name);
if(search_field_it == search_schema.end()) {
- ++it;
continue;
}
const auto& search_field = search_field_it->second;
bool arrays_match = false;
+ // compare values between old and update docs:
+ // if they match, we will remove them from both del and update docs
+
if(search_field.is_string()) {
// Go through all the field names and find the keys+values so that they can be removed from in-memory index
std::vector<std::string> reindex_vals;
@@ -412,13 +415,14 @@ void Index::scrub_reindex_doc(nlohmann::json& update_doc, nlohmann::json& del_do
arrays_match = _arrays_match<bool>(reindex_vals, old_vals);
}
- if(!arrays_match) {
- ++it;
- } else {
- it = del_doc.erase(it);
- update_doc.erase(field_name);
+ if(arrays_match) {
+ del_keys.push_back(field_name);
}
+ }
+ for(const auto& del_key: del_keys) {
+ del_doc.erase(del_key);
+ update_doc.erase(del_key);
}
}