Add mutate method to jsonpath (#2606)

* Add mutate method to jsonpath

Plug it in into OpToggle op.

---------

Signed-off-by: Roman Gershman <roman@dragonflydb.io>
This commit is contained in:
Roman Gershman 2024-02-18 16:06:33 +02:00 committed by GitHub
parent 1b51e82e55
commit 750f039316
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 186 additions and 14 deletions

View file

@ -578,23 +578,49 @@ OpResult<vector<OptSizeT>> OpArrLen(const OpArgs& op_args, string_view key, Json
return vec;
}
OpResult<vector<OptBool>> OpToggle(const OpArgs& op_args, string_view key, string_view path) {
OpResult<vector<OptBool>> OpToggle(const OpArgs& op_args, string_view key, string_view path,
JsonPathV2 expression) {
vector<OptBool> vec;
auto cb = [&vec](const auto&, JsonType& val) {
if (val.is_bool()) {
bool current_val = val.as_bool() ^ true;
val = current_val;
vec.emplace_back(current_val);
} else {
vec.emplace_back(nullopt);
if (std::holds_alternative<json::Path>(expression)) {
auto it_res = op_args.shard->db_slice().FindMutable(op_args.db_cntx, key, OBJ_JSON);
if (!it_res.ok()) {
return it_res.status();
}
};
OpStatus status = UpdateEntry(op_args, key, path, cb);
if (status != OpStatus::OK) {
return status;
PrimeValue& pv = it_res->it->second;
op_args.shard->search_indices()->RemoveDoc(key, op_args.db_cntx, pv);
const json::Path& expr = std::get<json::Path>(expression);
auto cb = [&vec](optional<string_view>, JsonType* val) {
if (val->is_bool()) {
bool next_val = val->as_bool() ^ true;
*val = next_val;
vec.emplace_back(next_val);
} else {
vec.emplace_back(nullopt);
}
return false;
};
json::MutatePath(expr, std::move(cb), pv.GetJson());
it_res->post_updater.Run();
op_args.shard->search_indices()->AddDoc(key, op_args.db_cntx, pv);
} else {
auto cb = [&vec](const auto&, JsonType& val) {
if (val.is_bool()) {
bool current_val = val.as_bool() ^ true;
val = current_val;
vec.emplace_back(current_val);
} else {
vec.emplace_back(nullopt);
}
};
OpStatus status = UpdateEntry(op_args, key, path, cb);
if (status != OpStatus::OK) {
return status;
}
}
return vec;
}
@ -1698,8 +1724,10 @@ void JsonFamily::Toggle(CmdArgList args, ConnectionContext* cntx) {
string_view key = ArgS(args, 0);
string_view path = ArgS(args, 1);
JsonPathV2 expression = PARSE_PATHV2(path);
auto cb = [&](Transaction* t, EngineShard* shard) {
return OpToggle(t->GetOpArgs(shard), key, path);
return OpToggle(t->GetOpArgs(shard), key, path, std::move(expression));
};
Transaction* trans = cntx->transaction;