Skip to content

Conversation

@chakkk309
Copy link

Fixes #3309

require.NoError(t, rdb.Set(ctx, "key1", "Hello world", 0).Err())

// DIGEST should return hex hash
digest := rdb.Do(ctx, "DIGEST", "key1").Val()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use the String to avoid the typecast. The same as other places.

Suggested change
digest := rdb.Do(ctx, "DIGEST", "key1").Val()
digest := rdb.Do(ctx, "DIGEST", "key1").String()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use the String to avoid the typecast. The same as other places.

Thanks for the suggestion, I will update other places.

@git-hulk git-hulk requested a review from PragmaTwice December 27, 2025 14:13
@git-hulk
Copy link
Member

@chakkk309 The CI failure is due to the missing ASF license header in the new file; you could also fix it. https://github.com/apache/kvrocks/actions/runs/20540153574/job/59003405075?pr=3313#step:4:139

@chakkk309
Copy link
Author

@chakkk309 The CI failure is due to the missing ASF license header in the new file; you could also fix it. https://github.com/apache/kvrocks/actions/runs/20540153574/job/59003405075?pr=3313#step:4:139

Thanks for the reminder, I have already added the license header~

@chakkk309 chakkk309 requested a review from git-hulk December 27, 2025 14:28
Copy link
Member

@git-hulk git-hulk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good to me, one test case suggestion inline.

@chakkk309, Could you please remove the meaningless comments inside test cases?

@Valay17
Copy link

Valay17 commented Dec 28, 2025

I was going through your commits and observed that you have implemented the digest function directly in the cmd_string.cc itself, making it noncallable as other commands depend on it and making it difficult to adapt changes further down the line. Type checking for key is not implemented as well, key must be of type string for it to be hashed

@git-hulk
Copy link
Member

git-hulk commented Dec 28, 2025

@chakkk309 XXH3_64bits haven't been implemented in Kvrocks.

Comment on lines 662 to 665
std::string String::ComputeXXH3Hash(const std::string &data) {
uint64_t hash = XXH3_64bits(data.data(), data.size());
return fmt::format("{:016x}", hash);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method seems unnecessary.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no, because the DELEX and SET command relies on this command so keeping a method is the right way to do it. If we do it this way it will be easier to add more commands in the future that requires DIGEST and also change the hashing algorithm by just changing this 1 function instead of changing it multiple times in various places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this utility. But it should not be a method of redis::String since it does nothing related to rocksdb IO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be put into string_util.h.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be put into string_util.h.

Sure, i will move this method later.

Comment on lines 731 to 738
Config *config = srv->GetConfig();
uint32_t max_btos_size = static_cast<uint32_t>(config->max_bitmap_to_string_mb) * MiB;
redis::Bitmap bitmap_db(srv->storage, conn->GetNamespace());
std::string value;
s = bitmap_db.GetString(ctx, args_[1], max_btos_size, &value);
if (s.ok()) {
digest = redis::String::ComputeXXH3Hash(value);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put them in redis_bitmap.cc? @git-hulk

Comment on lines 667 to 677
rocksdb::Status String::Digest(engine::Context &ctx, const std::string &user_key, std::string *digest) {
std::string value;
auto s = Get(ctx, user_key, &value);
if (!s.ok()) {
return s;
}

*digest = ComputeXXH3Hash(value);
return rocksdb::Status::OK();
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we can unify the bitmap and string part into one place. Or maybe we can just drop the support of bitmap? @git-hulk

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree to drop the support of bitmap in string, because it might introduce the unexpected performance issue if users incorrectly read the bitmap as a string.

#include <string>

#include "commander.h"
#include <fmt/format.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this pls.

Comment on lines +731 to +740
if (s.IsInvalidArgument()) {
Config *config = srv->GetConfig();
uint32_t max_btos_size = static_cast<uint32_t>(config->max_bitmap_to_string_mb) * MiB;
redis::Bitmap bitmap_db(srv->storage, conn->GetNamespace());
std::string value;
s = bitmap_db.GetString(ctx, args_[1], max_btos_size, &value);
if (s.ok()) {
digest = util::ComputeXXH3Hash(value);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

#include "commander.h"
#include <fmt/format.h>
#include "commands/command_parser.h"
#include "common/string_util.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this.

return s;
}

std::string ComputeXXH3Hash(const std::string &data) {
Copy link
Member

@PragmaTwice PragmaTwice Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::string ComputeXXH3Hash(const std::string &data) {
std::string StringDigest(std::string_view data) {

}

std::string ComputeXXH3Hash(const std::string &data) {
uint64_t hash = XXH3_64bits(data.data(), data.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint64_t hash = XXH3_64bits(data.data(), data.size());
XXH64_hash_t hash = XXH3_64bits(data.data(), data.size());

Comment on lines +131 to +134
{"simple string", "hello", ""},
{"number as string", "123", ""},
{"special chars", "!@#$%^&*()", ""},
{"unicode", "こんにちは", ""},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fill these expcted value..

Copy link
Member

@PragmaTwice PragmaTwice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support of the DIGEST command

4 participants