Skip to content

Commit

Permalink
optimize append str && some logic err
Browse files Browse the repository at this point in the history
  • Loading branch information
guojidan committed Nov 22, 2023
1 parent 6eca3af commit 8eb50c0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
15 changes: 7 additions & 8 deletions src/types/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,20 @@ struct JsonValue {

Status StrAppend(std::string_view path, const std::string &append_value, std::vector<uint64_t> &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<std::string>();
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<uint64_t>::max());
}
Expand All @@ -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<std::string>().length());
str_lens.push_back(origin.as_string().length());
} else {
str_lens.push_back(std::numeric_limits<uint64_t>::max());
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/redis_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>::max()) {
need_overwrite = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cppunit/types/json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,4 @@ TEST_F(RedisJsonTest, StrLen) {
for (int i = 0; i < 3; ++i) {
ASSERT_EQ(append_cnt_[i], result1[i]);
}
}
}

0 comments on commit 8eb50c0

Please sign in to comment.