-
Notifications
You must be signed in to change notification settings - Fork 591
feat(digest): implement DIGEST command #3313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: unstable
Are you sure you want to change the base?
Changes from 14 commits
fdad4ab
e03b477
624ce78
db10ddd
ec7052e
b9e603c
302b8d0
02670d7
928dcf6
20974ca
694b53a
d1120f8
2226341
8623728
c9e1744
0c93d42
0c3f318
d5da219
d2618f3
c7a99dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,12 +18,13 @@ | |
| * | ||
| */ | ||
|
|
||
| #include "commander.h" | ||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <string> | ||
|
|
||
| #include "commander.h" | ||
| #include <fmt/format.h> | ||
| #include "commands/command_parser.h" | ||
| #include "common/string_util.h" | ||
|
||
| #include "error_constants.h" | ||
| #include "server/redis_reply.h" | ||
| #include "server/redis_request.h" | ||
|
|
@@ -721,10 +722,39 @@ class CommandLCS : public Commander { | |
| int64_t min_match_len_ = 0; | ||
| }; | ||
|
|
||
| class CommandDigest : public Commander { | ||
| public: | ||
| Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override { | ||
| redis::String string_db(srv->storage, conn->GetNamespace()); | ||
| std::string digest; | ||
| auto s = string_db.Digest(ctx, args_[1], &digest); | ||
| 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); | ||
| } | ||
| } | ||
|
||
| if (!s.ok() && !s.IsNotFound()) { | ||
| return {Status::RedisExecErr, s.ToString()}; | ||
| } | ||
| if (s.IsNotFound()) { | ||
| *output = conn->NilString(); | ||
| return Status::OK(); | ||
| } | ||
| *output = redis::BulkString(digest); | ||
| return Status::OK(); | ||
| } | ||
| }; | ||
|
|
||
| REDIS_REGISTER_COMMANDS( | ||
| String, MakeCmdAttr<CommandGet>("get", 2, "read-only", 1, 1, 1), | ||
| MakeCmdAttr<CommandGetEx>("getex", -2, "write", 1, 1, 1), | ||
| MakeCmdAttr<CommandStrlen>("strlen", 2, "read-only", 1, 1, 1), | ||
| MakeCmdAttr<CommandDigest>("digest", 2, "read-only", 1, 1, 1), | ||
| MakeCmdAttr<CommandGetSet>("getset", 3, "write", 1, 1, 1), | ||
| MakeCmdAttr<CommandGetRange>("getrange", 4, "read-only", 1, 1, 1), | ||
| MakeCmdAttr<CommandSubStr>("substr", 4, "read-only", 1, 1, 1), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package string | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/apache/kvrocks/tests/gocase/util" | ||
| ) | ||
|
|
||
| func TestDigest(t *testing.T) { | ||
| srv := util.StartServer(t, map[string]string{}) | ||
| defer srv.Close() | ||
| ctx := context.Background() | ||
| rdb := srv.NewClient() | ||
| defer func() { require.NoError(t, rdb.Close()) }() | ||
|
|
||
| t.Run("DIGEST with existing string key", func(t *testing.T) { | ||
| require.NoError(t, rdb.Set(ctx, "key1", "Hello world", 0).Err()) | ||
|
|
||
| digest := rdb.Do(ctx, "DIGEST", "key1").Val().(string) | ||
| require.Equal(t, "b6acb9d84a38ff74", digest) | ||
| }) | ||
|
|
||
| t.Run("DIGEST with non-existent key", func(t *testing.T) { | ||
| digest := rdb.Do(ctx, "DIGEST", "nonexistent").Val() | ||
| require.Nil(t, digest) | ||
| }) | ||
|
|
||
| t.Run("DIGEST with different string values produces different hashes", func(t *testing.T) { | ||
| require.NoError(t, rdb.Set(ctx, "key1", "Hello", 0).Err()) | ||
| require.NoError(t, rdb.Set(ctx, "key2", "World", 0).Err()) | ||
|
|
||
| digest1 := rdb.Do(ctx, "DIGEST", "key1").Val().(string) | ||
| digest2 := rdb.Do(ctx, "DIGEST", "key2").Val().(string) | ||
|
|
||
| require.NotEqual(t, digest1, digest2) | ||
| }) | ||
|
|
||
| t.Run("DIGEST with same string value produces same hash", func(t *testing.T) { | ||
| require.NoError(t, rdb.Set(ctx, "key1", "consistent", 0).Err()) | ||
| require.NoError(t, rdb.Set(ctx, "key2", "consistent", 0).Err()) | ||
|
|
||
| digest1 := rdb.Do(ctx, "DIGEST", "key1").Val().(string) | ||
| digest2 := rdb.Do(ctx, "DIGEST", "key2").Val().(string) | ||
|
|
||
| require.Equal(t, digest1, digest2) | ||
| }) | ||
|
|
||
| t.Run("DIGEST with empty string", func(t *testing.T) { | ||
| require.NoError(t, rdb.Set(ctx, "empty", "", 0).Err()) | ||
|
|
||
| digest := rdb.Do(ctx, "DIGEST", "empty").Val().(string) | ||
| require.NotEmpty(t, digest) | ||
| require.Len(t, digest, 16) | ||
| }) | ||
|
|
||
| t.Run("DIGEST with binary data", func(t *testing.T) { | ||
| binaryData := "\x00\x01\x02\xff\xfe\xfd" | ||
| require.NoError(t, rdb.Set(ctx, "binary", binaryData, 0).Err()) | ||
|
|
||
| digest := rdb.Do(ctx, "DIGEST", "binary").Val().(string) | ||
| require.NotEmpty(t, digest) | ||
| require.Len(t, digest, 16) | ||
| }) | ||
|
|
||
| t.Run("DIGEST with large string", func(t *testing.T) { | ||
| largeString := make([]byte, 10240) | ||
| for i := range largeString { | ||
| largeString[i] = byte(i % 256) | ||
| } | ||
|
|
||
| require.NoError(t, rdb.Set(ctx, "large", string(largeString), 0).Err()) | ||
|
|
||
| digest := rdb.Do(ctx, "DIGEST", "large").Val().(string) | ||
| require.NotEmpty(t, digest) | ||
| require.Len(t, digest, 16) | ||
| }) | ||
|
|
||
| t.Run("DIGEST wrong number of arguments", func(t *testing.T) { | ||
| err := rdb.Do(ctx, "DIGEST").Err() | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "wrong number of arguments") | ||
|
|
||
| err = rdb.Do(ctx, "DIGEST", "key1", "extra").Err() | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "wrong number of arguments") | ||
| }) | ||
|
|
||
| t.Run("DIGEST with wrong key type should fail", func(t *testing.T) { | ||
| require.NoError(t, rdb.LPush(ctx, "list_key", "value").Err()) | ||
|
|
||
| err := rdb.Do(ctx, "DIGEST", "list_key").Err() | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "WRONGTYPE") | ||
| }) | ||
| } | ||
|
|
||
| func TestDigestCompatibility(t *testing.T) { | ||
| srv := util.StartServer(t, map[string]string{}) | ||
| defer srv.Close() | ||
| ctx := context.Background() | ||
| rdb := srv.NewClient() | ||
| defer func() { require.NoError(t, rdb.Close()) }() | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| value string | ||
| expected string | ||
| }{ | ||
| {"simple string", "hello", ""}, | ||
| {"number as string", "123", ""}, | ||
| {"special chars", "!@#$%^&*()", ""}, | ||
| {"unicode", "こんにちは", ""}, | ||
|
||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| require.NoError(t, rdb.Set(ctx, "test_key", tc.value, 0).Err()) | ||
|
|
||
| digest := rdb.Do(ctx, "DIGEST", "test_key").Val().(string) | ||
| require.NotEmpty(t, digest) | ||
| require.Len(t, digest, 16) | ||
|
|
||
| if tc.expected != "" { | ||
| require.Equal(t, tc.expected, digest) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this pls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done