-
Notifications
You must be signed in to change notification settings - Fork 564
feat: add deleterange command and test #2839
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
Open
baobaomaomeng
wants to merge
1
commit into
apache:unstable
Choose a base branch
from
baobaomaomeng:unstable
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -111,6 +111,7 @@ class Database { | |||||
[[nodiscard]] rocksdb::Status Expire(engine::Context &ctx, const Slice &user_key, uint64_t timestamp); | ||||||
[[nodiscard]] rocksdb::Status Del(engine::Context &ctx, const Slice &user_key); | ||||||
[[nodiscard]] rocksdb::Status MDel(engine::Context &ctx, const std::vector<Slice> &keys, uint64_t *deleted_cnt); | ||||||
[[nodiscard]] rocksdb::Status DeleteRange(engine::Context &ctx, const Slice &start, const Slice &end); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
[[nodiscard]] rocksdb::Status Exists(engine::Context &ctx, const std::vector<Slice> &keys, int *ret); | ||||||
[[nodiscard]] rocksdb::Status TTL(engine::Context &ctx, const Slice &user_key, int64_t *ttl); | ||||||
[[nodiscard]] rocksdb::Status GetExpireTime(engine::Context &ctx, const Slice &user_key, uint64_t *timestamp); | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* 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 deleterange | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/apache/kvrocks/tests/gocase/util" | ||
"github.com/redis/go-redis/v9" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
func TestDeleteRange(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()) }() | ||
DeleteRangeTest(t, rdb, ctx) | ||
} | ||
|
||
func DeleteRangeTest(t *testing.T, rdb *redis.Client, ctx context.Context) { | ||
t.Run("DELETERANGE ALL", func(t *testing.T) { | ||
require.NoError(t, rdb.FlushDB(ctx).Err()) | ||
util.Populate(t, rdb, "key:", 1000, 10) | ||
require.NoError(t, rdb.Do(ctx, "deleterange", "*").Err()) | ||
keys := scanAll(t, rdb) | ||
require.Len(t, keys, 0) | ||
}) | ||
|
||
t.Run("DELETERANGE BY PERFERIX", func(t *testing.T) { | ||
require.NoError(t, rdb.FlushDB(ctx).Err()) | ||
|
||
for _, key := range []string{"aa", "aab", "aabb", "ab", "abb", "ba", "cc", "cd", "dd"} { | ||
require.NoError(t, rdb.Set(ctx, key, "hello", 0).Err()) | ||
} | ||
deleterange(t, rdb, "aa*") | ||
keys := scanAll(t, rdb) | ||
require.Equal(t, []string{"ab", "abb", "ba", "cc", "cd", "dd"}, keys) | ||
|
||
deleterange(t, rdb, "c*") | ||
keys = scanAll(t, rdb) | ||
require.Equal(t, []string{"ab", "abb", "ba", "dd"}, keys) | ||
|
||
deleterange(t, rdb, "d*") | ||
keys = scanAll(t, rdb) | ||
require.Equal(t, []string{"ab", "abb", "ba"}, keys) | ||
|
||
deleterange(t, rdb, "a*") | ||
keys = scanAll(t, rdb) | ||
require.Equal(t, []string{"ba"}, keys) | ||
|
||
deleterange(t, rdb, "*") | ||
keys = scanAll(t, rdb) | ||
require.Equal(t, []string(nil), keys) | ||
}) | ||
|
||
t.Run("DELETERANGE with multi namespace", func(t *testing.T) { | ||
require.NoError(t, rdb.FlushDB(ctx).Err()) | ||
require.NoError(t, rdb.ConfigSet(ctx, "requirepass", "foobared").Err()) | ||
|
||
tokens := []string{"test_ns_token1", "test_ns_token2"} | ||
keyPrefixes := []string{"key1*", "key2*"} | ||
namespaces := []string{"test_ns1", "test_ns2"} | ||
|
||
for i := 0; i < 2; i++ { | ||
require.NoError(t, rdb.Do(ctx, "AUTH", "foobared").Err()) | ||
require.NoError(t, rdb.Do(ctx, "NAMESPACE", "ADD", namespaces[i], tokens[i]).Err()) | ||
require.NoError(t, rdb.Do(ctx, "AUTH", tokens[i]).Err()) | ||
|
||
for k := 0; k < 1000; k++ { | ||
require.NoError(t, rdb.Set(ctx, fmt.Sprintf("%s:%d", keyPrefixes[i], k), "hello", 0).Err()) | ||
} | ||
for k := 0; k < 100; k++ { | ||
require.NoError(t, rdb.Set(ctx, strconv.Itoa(k), "hello", 0).Err()) | ||
} | ||
} | ||
|
||
for i := 0; i < 2; i++ { | ||
require.NoError(t, rdb.Do(ctx, "AUTH", tokens[i]).Err()) | ||
require.NoError(t, rdb.Do(ctx, "deleterange", keyPrefixes[i]).Err()) | ||
|
||
keys := scanAll(t, rdb, "match", keyPrefixes[i]) | ||
require.Len(t, keys, 0) | ||
|
||
keys = scanAll(t, rdb) | ||
require.Len(t, keys, 100) | ||
} | ||
}) | ||
|
||
t.Run("Deleterange reject invalid input", func(t *testing.T) { | ||
util.ErrorRegexp(t, rdb.Do(ctx, "DELETERANGE", "hello").Err(), ".*syntax error.*") | ||
util.ErrorRegexp(t, rdb.Do(ctx, "DELETERANGE", "hel*o").Err(), ".*syntax error.*") | ||
util.ErrorRegexp(t, rdb.Do(ctx, "DELETERANGE", "*hello").Err(), ".*syntax error.*") | ||
util.ErrorRegexp(t, rdb.Do(ctx, "DELETERANGE", "[").Err(), ".*syntax error.*") | ||
util.ErrorRegexp(t, rdb.Do(ctx, "DELETERANGE", "\\").Err(), ".*syntax error.*") | ||
util.ErrorRegexp(t, rdb.Do(ctx, "DELETERANGE", "[a").Err(), ".*syntax error.*") | ||
util.ErrorRegexp(t, rdb.Do(ctx, "DELETERANGE", "[a-]").Err(), ".*syntax error.*") | ||
}) | ||
} | ||
|
||
func scan(t testing.TB, rdb *redis.Client, c string, args ...interface{}) (cursor string, keys []string) { | ||
args = append([]interface{}{"SCAN", c}, args...) | ||
r := rdb.Do(context.Background(), args...) | ||
require.NoError(t, r.Err()) | ||
require.Len(t, r.Val(), 2) | ||
|
||
rs := r.Val().([]interface{}) | ||
cursor = rs[0].(string) | ||
|
||
for _, key := range rs[1].([]interface{}) { | ||
keys = append(keys, key.(string)) | ||
} | ||
|
||
return | ||
} | ||
|
||
func deleterange(t testing.TB, rdb *redis.Client, c string, args ...interface{}) { | ||
args = append([]interface{}{"DELETERANGE", c}, args...) | ||
r := rdb.Do(context.Background(), args...) | ||
require.NoError(t, r.Err()) | ||
} | ||
|
||
func scanAll(t testing.TB, rdb *redis.Client, args ...interface{}) (keys []string) { | ||
c := "0" | ||
for { | ||
cursor, keyList := scan(t, rdb, c, args...) | ||
|
||
c = cursor | ||
keys = append(keys, keyList...) | ||
|
||
if c == "0" { | ||
slices.Sort(keys) | ||
keys = slices.Compact(keys) | ||
return | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So this is a regex rule?