-
Notifications
You must be signed in to change notification settings - Fork 581
feat: Add DELPREFIX to delete keys by prefix #2828
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
Draft
frHimanshu
wants to merge
27
commits into
apache:unstable
Choose a base branch
from
frHimanshu: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.
Draft
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4514d5f
Update cmd_key.cc
frHimanshu efa8911
Update commander.cc
frHimanshu a144d25
ommander.cc
frHimanshu 513d33f
cmd_key.cc
frHimanshu a503e27
Fixes
frHimanshu f3604d3
Update cmd_key.cc
frHimanshu 6215660
Delete tests/persistence directory
frHimanshu 8a9d566
Delete tests/test_helper.tcl
frHimanshu 82e19a0
Create cmd_delprefix_test.go
frHimanshu 67e54f9
Attempt 3
frHimanshu a095125
Fixes
frHimanshu ebeb166
Update cmd_delprefix_test.go
frHimanshu 5da5b6a
Update cmd_key.cc
frHimanshu a24af6f
Original go.mod
frHimanshu 42fcd7f
Update cmd_key.cc
frHimanshu f413a5b
Update cmd_delprefix_test.go
frHimanshu 83c2394
Update cmd_delprefix_test.go
frHimanshu d8fa88d
Update cmd_key.cc
frHimanshu 5ff609d
Update cmd_key.cc
frHimanshu f979cbd
Update cmd_key.cc
frHimanshu 8ca8b57
Update redis_db.cc
frHimanshu 49e2222
Update redis_db.h
frHimanshu 5dd9fdd
Delete tests/gocase/integration/cli/cmd_delprefix_test.go
frHimanshu 23d0f3c
Create cmd_delprefix_test.go
frHimanshu 804922b
Update cmd_delprefix_test.go
frHimanshu 57cb21d
Merge branch 'unstable' into unstable
aleksraiden 0c71db3
Merge branch 'unstable' into unstable
frHimanshu 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * 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 cli | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/apache/kvrocks/tests/gocase/util" // Utilize common utilities | ||
| "github.com/redis/go-redis/v9" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDelPrefix(t *testing.T) { | ||
| ctx := context.Background() | ||
| client := redis.NewClient(&redis.Options{ | ||
| Addr: "localhost:6666", // Ensure this matches the Kvrocks port | ||
| }) | ||
| defer client.Close() | ||
|
|
||
| // Set up test keys | ||
| client.Set(ctx, "test:1", "value1", 0) | ||
| client.Set(ctx, "test:2", "value2", 0) | ||
| client.Set(ctx, "other:1", "value3", 0) | ||
|
|
||
| // Run the DELPREFIX command | ||
| res, err := client.Do(ctx, "DELPREFIX", "test:").Int() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, 2, res, "Expected to delete 2 keys") | ||
|
|
||
| // Ensure the prefixed keys are deleted, but others remain | ||
| existsTest1, _ := client.Exists(ctx, "test:1").Result() | ||
| existsTest2, _ := client.Exists(ctx, "test:2").Result() | ||
| existsOther1, _ := client.Exists(ctx, "other:1").Result() | ||
|
|
||
| assert.Equal(t, int64(0), existsTest1, "test:1 should be deleted") | ||
| assert.Equal(t, int64(0), existsTest2, "test:2 should be deleted") | ||
| assert.Equal(t, int64(1), existsOther1, "other:1 should still exist") | ||
| } | ||
|
|
||
| func TestDelPrefixClusterMode(t *testing.T) { | ||
| t.Parallel() | ||
| // Start server with cluster mode enabled | ||
| srv := util.StartServer(t, map[string]string{"cluster-enabled": "yes"}) | ||
| defer srv.Close() | ||
|
|
||
| ctx := context.Background() | ||
| client := srv.NewClient() | ||
| defer func() { require.NoError(t, client.Close()) }() | ||
|
|
||
| // Simulate cluster mode by adding keys with hash prefixes | ||
| client.Set(ctx, "{hash1}test:1", "value1", 0) | ||
| client.Set(ctx, "{hash1}test:2", "value2", 0) | ||
| client.Set(ctx, "{hash2}other:1", "value3", 0) | ||
|
|
||
| // Run the DELPREFIX command (expect it to be disabled) | ||
| _, err := client.Do(ctx, "DELPREFIX", "{hash1}test:").Result() | ||
| require.ErrorContains(t, err, "disabled in cluster mode") | ||
|
|
||
| // Ensure no keys are deleted | ||
| existsTest1, _ := client.Exists(ctx, "{hash1}test:1").Result() | ||
| existsTest2, _ := client.Exists(ctx, "{hash1}test:2").Result() | ||
| existsOther1, _ := client.Exists(ctx, "{hash2}other:1").Result() | ||
|
|
||
| assert.Equal(t, int64(1), existsTest1, "{hash1}test:1 should still exist") | ||
| assert.Equal(t, int64(1), existsTest2, "{hash1}test:2 should still exist") | ||
| assert.Equal(t, int64(1), existsOther1, "{hash2}other:1 should still exist") | ||
| } | ||
|
|
||
| func TestDelPrefixDisabledInClusterMode(t *testing.T) { | ||
| t.Parallel() | ||
| // Start server with cluster mode enabled | ||
| srv := util.StartServer(t, map[string]string{"cluster-enabled": "yes"}) | ||
| defer srv.Close() | ||
|
|
||
| ctx := context.Background() | ||
| client := srv.NewClient() | ||
| defer func() { require.NoError(t, client.Close()) }() | ||
|
|
||
| // Simulate cluster mode by adding keys with hash prefixes | ||
| client.Set(ctx, "{hash1}test:1", "value1", 0) | ||
| client.Set(ctx, "{hash1}test:2", "value2", 0) | ||
|
|
||
| // Assume DELPREFIX command is disabled in cluster mode, expect an error | ||
| _, err := client.Do(ctx, "DELPREFIX", "{hash1}test:").Result() | ||
| require.ErrorContains(t, err, "disabled in cluster mode") | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.