-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 issue #1517: scan 命令不支持 type 参数 #1582
Changes from all commits
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 |
---|---|---|
|
@@ -600,10 +600,12 @@ class ScanCmd : public Cmd { | |
int64_t cursor_ = 0; | ||
std::string pattern_ = "*"; | ||
int64_t count_ = 10; | ||
storage::DataType type_ = storage::DataType::kAll; | ||
void DoInitial() override; | ||
void Clear() override { | ||
pattern_ = "*"; | ||
count_ = 10; | ||
type_ = storage::DataType::kAll; | ||
} | ||
}; | ||
|
||
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. Based on the code patch, it seems that a new member variable Without seeing the implementation details of the class and associated functions, it is difficult to identify any bugs or potential improvements in the code. However, adding this new variable appears to be a reasonable and potentially useful enhancement to the 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. Based on the provided code patch, it appears that a new member variable Without any additional information about the implementation of |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1090,14 +1090,30 @@ void ScanCmd::DoInitial() { | |
|
||
while (index < argc) { | ||
std::string opt = argv_[index]; | ||
if ((strcasecmp(opt.data(), "match") == 0) || (strcasecmp(opt.data(), "count") == 0)) { | ||
if ((strcasecmp(opt.data(), "match") == 0) || (strcasecmp(opt.data(), "count") == 0) || | ||
(strcasecmp(opt.data(), "type") == 0)) { | ||
index++; | ||
if (index >= argc) { | ||
res_.SetRes(CmdRes::kSyntaxErr); | ||
return; | ||
} | ||
if (strcasecmp(opt.data(), "match") == 0) { | ||
pattern_ = argv_[index]; | ||
} else if (strcasecmp(opt.data(), "type") == 0) { | ||
std::string str_type = argv_[index]; | ||
if (strcasecmp(str_type.data(), "string") == 0) { | ||
type_ = storage::DataType::kStrings; | ||
} else if (strcasecmp(str_type.data(), "zset") == 0) { | ||
type_ = storage::DataType::kZSets; | ||
} else if (strcasecmp(str_type.data(), "set") == 0) { | ||
type_ = storage::DataType::kSets; | ||
} else if (strcasecmp(str_type.data(), "list") == 0) { | ||
type_ = storage::DataType::kLists; | ||
} else if (strcasecmp(str_type.data(), "hash") == 0) { | ||
type_ = storage::DataType::kHashes; | ||
} else { | ||
res_.SetRes(CmdRes::kSyntaxErr); | ||
} | ||
} else if ((pstd::string2int(argv_[index].data(), argv_[index].size(), &count_) == 0) || count_ <= 0) { | ||
res_.SetRes(CmdRes::kInvalidInt); | ||
return; | ||
|
@@ -1123,7 +1139,7 @@ void ScanCmd::Do(std::shared_ptr<Slot> slot) { | |
keys.clear(); | ||
batch_count = left < PIKA_SCAN_STEP_LENGTH ? left : PIKA_SCAN_STEP_LENGTH; | ||
left = left > PIKA_SCAN_STEP_LENGTH ? left - PIKA_SCAN_STEP_LENGTH : 0; | ||
cursor_ret = slot->db()->Scan(storage::DataType::kAll, cursor_ret, pattern_, batch_count, &keys); | ||
cursor_ret = slot->db()->Scan(type_, cursor_ret, pattern_, batch_count, &keys); | ||
for (const auto& key : keys) { | ||
RedisAppendLen(raw, key.size(), "$"); | ||
RedisAppendContent(raw, key); | ||
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. It seems that the code patch is adding functionality to the existing code. It introduces a new option "-type" to filter results by key type, and assigns it to the variable "type_". The types will be mapped to enumerated values from storage::DataType. An improvement would be to add proper error messages to make it easier to understand why the command has failed. Another suggestion would be to handle exceptions in case of errors within 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. The code patch appears to add functionality related to scanning Redis data. Here are some potential issues and improvements that could be made:
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. The code patch looks good overall, as it introduces a new option One minor suggestion for improvement would be to add some error handling in the Also, there might be room for improvement in terms of code readability by extracting the code for parsing the data type options into a separate function to reduce the complexity of the |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,51 @@ start_server {tags {"scan"}} { | |
assert_equal 100 [llength $keys] | ||
} | ||
|
||
test "SCAN TYPE" { | ||
r flushdb | ||
# populate only creates strings | ||
r debug populate 1000 | ||
|
||
# Check non-strings are excluded | ||
set cur 0 | ||
set keys {} | ||
while 1 { | ||
set res [r scan $cur type "list"] | ||
set cur [lindex $res 0] | ||
set k [lindex $res 1] | ||
lappend keys {*}$k | ||
if {$cur == 0} break | ||
} | ||
|
||
assert_equal 0 [llength $keys] | ||
|
||
# Check strings are included | ||
set cur 0 | ||
set keys {} | ||
while 1 { | ||
set res [r scan $cur type "string"] | ||
set cur [lindex $res 0] | ||
set k [lindex $res 1] | ||
lappend keys {*}$k | ||
if {$cur == 0} break | ||
} | ||
|
||
assert_equal 1000 [llength $keys] | ||
|
||
# Check all three args work together | ||
set cur 0 | ||
set keys {} | ||
while 1 { | ||
set res [r scan $cur type "string" match "key:*" count 10] | ||
set cur [lindex $res 0] | ||
set k [lindex $res 1] | ||
lappend keys {*}$k | ||
if {$cur == 0} break | ||
} | ||
|
||
assert_equal 1000 [llength $keys] | ||
} | ||
|
||
foreach enc {intset hashtable} { | ||
test "SSCAN with encoding $enc" { | ||
# Create the Set | ||
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. The code patch seems fine, and I don't see any bug risks. However, here are some suggestions to improve the code:
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. The code patch seems to be a new test added to an existing implementation of Redis Command Line Interface (CLI) module, specifically scanning keys with a specific type. The test seems to check if the implemented command The code looks fine in terms of structure and syntax. However, here are my suggestions:
|
||
|
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.
Based on the provided code patch, a brief code review and possible suggestions are: