Skip to content

Commit 6453149

Browse files
fix(json_family): Fix memory tracking for JSON mutate operations
Signed-off-by: Stepan Bagritsevich <[email protected]>
1 parent 8960ee1 commit 6453149

File tree

1 file changed

+8
-36
lines changed

1 file changed

+8
-36
lines changed

src/server/json_family.cc

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -748,47 +748,19 @@ OpResult<JsonCallbackResult<T>> JsonReadOnlyOperation(const OpArgs& op_args, std
748748
return json_path.ExecuteReadOnlyCallback<T>(json_val, cb, options.cb_result_options);
749749
}
750750

751-
struct MutateOperationOptions {
752-
using PostMutateCallback = absl::FunctionRef<OpStatus(JsonType*)>;
753-
754-
std::optional<PostMutateCallback> post_mutate_cb;
755-
CallbackResultOptions cb_result_options = CallbackResultOptions::DefaultMutateOptions();
756-
};
757-
758751
template <typename T>
759-
OpResult<JsonCallbackResult<optional<T>>> JsonMutateOperation(const OpArgs& op_args,
760-
std::string_view key,
761-
const WrappedJsonPath& json_path,
762-
JsonPathMutateCallback<T> cb,
763-
MutateOperationOptions options = {}) {
752+
OpResult<JsonCallbackResult<optional<T>>> JsonMutateOperation(
753+
const OpArgs& op_args, std::string_view key, const WrappedJsonPath& json_path,
754+
JsonPathMutateCallback<T> cb,
755+
CallbackResultOptions cb_result_options = CallbackResultOptions::DefaultMutateOptions()) {
764756
auto it_res = op_args.GetDbSlice().FindMutable(op_args.db_cntx, key, OBJ_JSON);
765757
RETURN_ON_BAD_STATUS(it_res);
766758

767-
JsonMemTracker mem_tracker;
768-
769-
PrimeValue& pv = it_res->it->second;
770-
771-
JsonType* json_val = pv.GetJson();
772-
DCHECK(json_val) << "should have a valid JSON object for key '" << key << "' the type for it is '"
773-
<< pv.ObjType() << "'";
774-
775-
op_args.shard->search_indices()->RemoveDoc(key, op_args.db_cntx, pv);
776-
777-
auto mutate_res = json_path.ExecuteMutateCallback(json_val, cb, options.cb_result_options);
759+
JsonAutoUpdater updater(op_args, key, *std::move(it_res));
778760

779-
// Call post mutate callback
780-
if (mutate_res && options.post_mutate_cb) {
781-
auto res = options.post_mutate_cb.value()(json_val);
782-
// We can not return result here, because we need to update the size
783-
if (res != OpStatus::OK) {
784-
mutate_res = res;
785-
}
786-
}
761+
auto mutate_res = json_path.ExecuteMutateCallback(updater.GetJson(), cb, cb_result_options);
787762

788-
// we need to manually run this before the PostUpdater run
789-
mem_tracker.SetJsonSize(pv, false);
790-
it_res->post_updater.Run();
791-
op_args.shard->search_indices()->AddDoc(key, op_args.db_cntx, pv);
763+
updater.SetJsonSize();
792764

793765
return mutate_res;
794766
}
@@ -1223,7 +1195,7 @@ auto OpArrPop(const OpArgs& op_args, string_view key, WrappedJsonPath& path, int
12231195
return {false, std::move(str)};
12241196
};
12251197
return JsonMutateOperation<std::string>(op_args, key, path, std::move(cb),
1226-
{{}, CallbackResultOptions{OnEmpty::kSendNil}});
1198+
CallbackResultOptions{OnEmpty::kSendNil});
12271199
}
12281200

12291201
// Returns numeric vector that represents the new length of the array at each path.

0 commit comments

Comments
 (0)