Skip to content
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

fix: Better checking for prefix matches #2599

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/commands/cmd_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,15 @@ class CommandNamespace : public Commander {
class CommandKeys : public Commander {
public:
Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override {
std::string prefix = args_[1];
const std::string &prefix = args_[1];
std::vector<std::string> keys;
redis::Database redis(srv->storage, conn->GetNamespace());

rocksdb::Status s;
if (prefix == "*") {
s = redis.Keys(ctx, std::string(), &keys);
} else {
if (prefix[prefix.size() - 1] != '*') {
return {Status::RedisExecErr, "only keys prefix match was supported"};
}

s = redis.Keys(ctx, prefix.substr(0, prefix.size() - 1), &keys);
if (prefix.empty() || prefix.find('*') != prefix.size() - 1) {
return {Status::RedisExecErr, "only keys prefix match was supported"};
}

const rocksdb::Status s = redis.Keys(ctx, prefix.substr(0, prefix.size() - 1), &keys);
if (!s.ok()) {
return {Status::RedisExecErr, s.ToString()};
}
Expand Down
6 changes: 4 additions & 2 deletions src/commands/scan_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class CommandScanBase : public Commander {
while (parser.Good()) {
if (parser.EatEqICase("match")) {
prefix_ = GET_OR_RET(parser.TakeStr());
if (!prefix_.empty() && prefix_.back() == '*') {
prefix_ = prefix_.substr(0, prefix_.size() - 1);
// The match pattern should contain exactly one '*' at the end; remove the * to
// get the prefix to match.
if (!prefix_.empty() && prefix_.find('*') == prefix_.size() - 1) {
prefix_.pop_back();
} else {
return {Status::RedisParseErr, "currently only key prefix matching is supported"};
}
Expand Down
4 changes: 4 additions & 0 deletions tests/gocase/unit/keyspace/keyspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func TestKeyspace(t *testing.T) {
require.Equal(t, []string{"foo_a", "foo_b", "foo_c"}, keys)
})

t.Run("KEYS with invalid pattern", func(t *testing.T) {
require.Error(t, rdb.Keys(ctx, "*ab*").Err())
})

t.Run("KEYS to get all keys", func(t *testing.T) {
keys := rdb.Keys(ctx, "*").Val()
sort.Slice(keys, func(i, j int) bool {
Expand Down
7 changes: 7 additions & 0 deletions tests/gocase/unit/scan/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func ScanTest(t *testing.T, rdb *redis.Client, ctx context.Context) {
require.Len(t, keys, 1000)
})

t.Run("SCAN MATCH invalid pattern", func(t *testing.T) {
require.NoError(t, rdb.FlushDB(ctx).Err())
util.Populate(t, rdb, "*ab", 1000, 10)
// SCAN MATCH with invalid pattern should return an error
require.Error(t, rdb.Do(context.Background(), "SCAN", "match", "*ab*").Err())
})

t.Run("SCAN guarantees check under write load", func(t *testing.T) {
require.NoError(t, rdb.FlushDB(ctx).Err())
util.Populate(t, rdb, "", 100, 10)
Expand Down
Loading