From 8eb50c09757de9cb682a19b7ee270ddbba4967aa Mon Sep 17 00:00:00 2001 From: guojidan <1948535941@qq.com> Date: Wed, 22 Nov 2023 01:18:58 +0000 Subject: [PATCH] optimize append str && some logic err --- src/types/json.h | 15 +++++++-------- src/types/redis_json.cc | 4 ++-- tests/cppunit/types/json_test.cc | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/types/json.h b/src/types/json.h index 411b50cf3ab..08f0a0e96fd 100644 --- a/src/types/json.h +++ b/src/types/json.h @@ -155,21 +155,20 @@ struct JsonValue { Status StrAppend(std::string_view path, const std::string &append_value, std::vector &append_cnt) { try { - std::string append_trimmed; + std::string append_str; jsoncons::json append_json = jsoncons::json::parse(append_value); if (append_json.is_string()) { - append_trimmed = append_json.as_string(); + append_str = append_json.as_string(); } else { return {Status::NotOK, "STRAPPEND need input a string to append"}; } jsoncons::jsonpath::json_replace( - value, path, [&append_trimmed, &append_cnt](const std::string & /*path*/, jsoncons::json &origin) { + value, path, [&append_str, &append_cnt](const std::string & /*path*/, jsoncons::json &origin) { if (origin.is_string()) { - auto origin_trimmed = origin.as(); - append_cnt.push_back(origin_trimmed.length() + append_trimmed.length()); - jsoncons::json new_value(origin_trimmed + append_trimmed); - origin = new_value; + auto origin_trimmed = origin.as_string(); + append_cnt.push_back(origin_trimmed.length() + append_str.length()); + origin = origin_trimmed + append_str; } else { append_cnt.push_back(std::numeric_limits::max()); } @@ -186,7 +185,7 @@ struct JsonValue { jsoncons::jsonpath::json_query(value, path, [&str_lens](const std::string & /*path*/, const jsoncons::json &origin) { if (origin.is_string()) { - str_lens.push_back((int64_t)origin.as().length()); + str_lens.push_back(origin.as_string().length()); } else { str_lens.push_back(std::numeric_limits::max()); } diff --git a/src/types/redis_json.cc b/src/types/redis_json.cc index 16cc40892c6..840673dafea 100644 --- a/src/types/redis_json.cc +++ b/src/types/redis_json.cc @@ -400,12 +400,12 @@ rocksdb::Status Json::StrAppend(const std::string &user_key, const std::string & auto append_res = json_val.StrAppend(path, value, append_cnt); if (!append_res) return rocksdb::Status::InvalidArgument(append_res.Msg()); - // need Reverse order + // need reverse order std::reverse(append_cnt.begin(), append_cnt.end()); bool need_overwrite = false; for (auto append : append_cnt) { - if (append > 0) { + if (append != std::numeric_limits::max()) { need_overwrite = true; } } diff --git a/tests/cppunit/types/json_test.cc b/tests/cppunit/types/json_test.cc index 6c30afaac6c..a9ca515be62 100644 --- a/tests/cppunit/types/json_test.cc +++ b/tests/cppunit/types/json_test.cc @@ -518,4 +518,4 @@ TEST_F(RedisJsonTest, StrLen) { for (int i = 0; i < 3; ++i) { ASSERT_EQ(append_cnt_[i], result1[i]); } -} \ No newline at end of file +}