Address some warnings related to update doc scrubbing.

This commit is contained in:
kishorenc 2020-12-27 17:32:27 +05:30
parent 59f95dbf67
commit b2fba69a73
3 changed files with 17 additions and 12 deletions

View File

@ -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;

View File

@ -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)

View File

@ -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);
}
}