-
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
Conversation
void DoInitial() override; | ||
void Clear() override { | ||
pattern_ = "*"; | ||
count_ = 10; | ||
type_ = storage::kAll; | ||
} | ||
}; | ||
|
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:
- The added "storage::DataType type_ = storage::kAll;" seems fine if it's going to be used in the implementation of this class later.
- It is not clear what the purpose of the "DoInitial()" function is or how it is being used. Therefore, reviewing the implementation for this function could be beneficial.
- It would be better to define the initial value of "type_" at its declaration point instead of initializing it into the "Clear()" method.
- There doesn't seem to be any bug risk in the given code.
@@ -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 comment
The 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 std::string2int
.
|
||
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 comment
The 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:
-
The test case "SCAN TYPE" could be split into multiple tests for better readability and maintainability.
-
Consider adding more test cases to cover different scenarios and edge cases.
-
Use meaningful variable names instead of short ones such as "cur," "res," and "k."
-
Consider documenting the purpose of each test case.
-
Consider using the "incr" command instead of "*}$k" to append elements to a list.
-
Identify specific Redis versions that the code is tested on.
-
Consider using a consistent style throughout the code.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the code patch, it seems that a new member variable type_
of type storage::DataType
has been added to class ScanCmd
. This variable is initialized with the value kAll
in the Clear()
function.
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 ScanCmd
class.
@@ -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 comment
The 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:
- The current code may have an issue with error handling when a type other than string, zset, set, list and hash is specified. If an incorrect type is entered, the
SyntaxErr
is returned without a clear message about what went wrong. It would be better to provide a more specific error message to help the user identify the problem. - The use of
strcasecmp
can be a security risk due to its vulnerability to timing attacks, which can be mitigated using constant-time comparison or modern secure comparison functions likesecure_compare()
provided by libsodium. - It is unclear what
PIKA_SCAN_STEP_LENGTH
represents, which makes it difficult to understand the purpose ofbatch_count
. - Adding comments would be helpful to make it easy for developers who unfamiliar with the project to quickly understand what each function does, why it was added, and its important details.
- Another possible improvement suggestion would be to add unit tests for the new functionality to ensure proper functionality over time.
include/module.h
Outdated
void PikaModuleInit(void); | ||
int moduleLoad(const char *path, void **module_argv, int module_argc, int is_loadex); | ||
|
||
#endif No newline at end of file |
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.
This code patch appears to be defining a C header file "module.h" which provides some macro definitions, defines two empty data structures, and declares two functions "PikaModuleInit" and "moduleLoad". It also includes the header file "pika_server.h" which may contain additional functionality. However, without further context, it is difficult to provide specific suggestions for improvement or identify any potential bugs.
include/pika_command.h
Outdated
@@ -219,6 +222,7 @@ enum CmdFlags { | |||
kCmdFlagsHyperLogLog = 14, | |||
kCmdFlagsGeo = 16, | |||
kCmdFlagsPubSub = 18, | |||
kCmdFlagsModule = 20, | |||
kCmdFlagsNoLocal = 0, // default nolocal | |||
kCmdFlagsLocal = 32, | |||
kCmdFlagsNoSuspend = 0, // default nosuspend |
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.
The code patch adds a new command for modules (kCmdNameModule) and defines a corresponding flag (kCmdFlagsModule). The change looks safe as it only introduces a new command and doesn't modify any existing functionality. However, it is hard to give more specific advice without seeing the implementation of the module command.
include/pika_module.h
Outdated
virtual void Clear() { arguments_.clear(); } | ||
}; | ||
|
||
#endif No newline at end of file |
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.
This code patch is a header file implementing a ModuleCmd
class for a Redis-compatible database system called Pika. Based on its content, it seems to be complete and doesn't contain any obvious bugs or errors. However, without the entirety of the system's code, it's difficult to provide a comprehensive review and improvements.
The only suggestion I have is to consider adding a newline character at the end of the file to conform to common coding practices and avoid compilation warnings.
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 comment
The 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 type_
of type storage::DataType
has been added to the ScanCmd
class with an initial value of kAll
. Additionally, the Clear()
method has been updated to reset type_
to kAll
.
Without any additional information about the implementation of storage::DataType
or the purpose of the ScanCmd
class and its methods, it is difficult to identify any potential bug risks or improvement suggestions from this code patch alone.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
The code patch looks good overall, as it introduces a new option type
to allow scanning specific data types and handles it appropriately in the Do
function by passing the chosen type to the Scan
method.
One minor suggestion for improvement would be to add some error handling in the DoInitial
function to check if an invalid data type is provided after the type
option and set the response accordingly.
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 DoInitial
function.
|
||
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 comment
The 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 SCAN
filters by key type as expected.
The code looks fine in terms of structure and syntax. However, here are my suggestions:
-
One improvement suggestion is to introduce fixtures or helper methods that generate predefined keys of different types, instead of using
populate
of Redis debug commands to add test data. -
There could have been some comments added under each of the test cases defining what they are trying to test which would help future maintainers of this code.
-
Another good practice that can always be considered is adding additional edge test cases like empty values being evaluated, non-existing types, etc.
-
Lastly, it's unclear from the provided snippet whether other important details such as dependencies, imports, and system level requirements, etc. are present or not.
* feat: supported 'ptype' command * Simplified command `type` and `ptype` code * Remove the capacity judgment * fix slavaof serialize response bug (#1583) Co-authored-by: liuyuecai <liuyuecai@360.cn> * fix (#1587) (#1588) * fix unit test:type/set (#1577) * Fix memory leaks in HandleBGWorkerWriteDB and HandleMetaSyncResponse (#1590) * fix memory leaks in PikaReplBgWorker::HandleBGWorkerWriteDB(void* arg) and PikaReplClientConn::HandleMetaSyncResponse(void* arg). * add address/thread sanitizer to CMakeLists --------- Co-authored-by: cjh <1271435567@qq.com> * refactor: replace pstd/env with std::filesystem (#1470) * fix bug issue 1554: using unique_ptr.reset to fix the SIGABRT error (#1595) using unique_ptr.reset to fix the SIGABRT error && using time-wait lock to notify quickly * fix: incorrect manner of terminating the process (#1596) * fix issue#1597: add rocksdb dependency to pstd (#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <J1senn@outlook.com> * fix_info_command * fix: gcc13 compile failed (#1601) * ci: add unit test to github action (#1604) * ci: add unit test in github workflow * chore:change `PLATFORM` field logic (#1615) * fix issue 1517: scan 命令不支持 type 参数 (#1582) * fix: the unit test of type/set (#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * [fix issue1621] fix deadlock (#1620) * [fix] fix deadlock * [fix] fix * command `type` and `ptype` add unit test * fix member variable initialization * Update issue templates * feat: supported 'ptype' command * fix unit test:type/set (#1577) * fix issue#1597: add rocksdb dependency to pstd (#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <J1senn@outlook.com> * fix: gcc13 compile failed (#1601) * fix: the unit test of type/set (#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * Modify other modules to use the new `type` function * fix function repeat --------- Co-authored-by: Yuecai Liu <38887641+luky116@users.noreply.github.com> Co-authored-by: liuyuecai <liuyuecai@360.cn> Co-authored-by: Peter Chan <luckygoose@foxmail.com> Co-authored-by: chenbt <34958405+chenbt-hz@users.noreply.github.com> Co-authored-by: Junhua Chen <41671101+cheniujh@users.noreply.github.com> Co-authored-by: cjh <1271435567@qq.com> Co-authored-by: kang jinci <jincikang@gmail.com> Co-authored-by: Xin.Zh <dragoncharlie@foxmail.com> Co-authored-by: machinly <machinlyg@gmail.com> Co-authored-by: A2ureStone <73770413+A2ureStone@users.noreply.github.com> Co-authored-by: J1senn <J1senn@outlook.com> Co-authored-by: chejinge <945997690@qq.com> Co-authored-by: baerwang <52104949+baerwang@users.noreply.github.com> Co-authored-by: ptbxzrt <89020404+ptbxzrt@users.noreply.github.com> Co-authored-by: 你不要过来啊 <73388438+iiiuwioajdks@users.noreply.github.com>
* feat: supported 'ptype' command * Simplified command `type` and `ptype` code * Remove the capacity judgment * fix slavaof serialize response bug (OpenAtomFoundation#1583) Co-authored-by: liuyuecai <liuyuecai@360.cn> * fix (OpenAtomFoundation#1587) (OpenAtomFoundation#1588) * fix unit test:type/set (OpenAtomFoundation#1577) * Fix memory leaks in HandleBGWorkerWriteDB and HandleMetaSyncResponse (OpenAtomFoundation#1590) * fix memory leaks in PikaReplBgWorker::HandleBGWorkerWriteDB(void* arg) and PikaReplClientConn::HandleMetaSyncResponse(void* arg). * add address/thread sanitizer to CMakeLists --------- Co-authored-by: cjh <1271435567@qq.com> * refactor: replace pstd/env with std::filesystem (OpenAtomFoundation#1470) * fix bug issue 1554: using unique_ptr.reset to fix the SIGABRT error (OpenAtomFoundation#1595) using unique_ptr.reset to fix the SIGABRT error && using time-wait lock to notify quickly * fix: incorrect manner of terminating the process (OpenAtomFoundation#1596) * fix issue#1597: add rocksdb dependency to pstd (OpenAtomFoundation#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <J1senn@outlook.com> * fix_info_command * fix: gcc13 compile failed (OpenAtomFoundation#1601) * ci: add unit test to github action (OpenAtomFoundation#1604) * ci: add unit test in github workflow * chore:change `PLATFORM` field logic (OpenAtomFoundation#1615) * fix issue 1517: scan 命令不支持 type 参数 (OpenAtomFoundation#1582) * fix: the unit test of type/set (OpenAtomFoundation#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * [fix issue1621] fix deadlock (OpenAtomFoundation#1620) * [fix] fix deadlock * [fix] fix * command `type` and `ptype` add unit test * fix member variable initialization * Update issue templates * feat: supported 'ptype' command * fix unit test:type/set (OpenAtomFoundation#1577) * fix issue#1597: add rocksdb dependency to pstd (OpenAtomFoundation#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <J1senn@outlook.com> * fix: gcc13 compile failed (OpenAtomFoundation#1601) * fix: the unit test of type/set (OpenAtomFoundation#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * Modify other modules to use the new `type` function * fix function repeat --------- Co-authored-by: Yuecai Liu <38887641+luky116@users.noreply.github.com> Co-authored-by: liuyuecai <liuyuecai@360.cn> Co-authored-by: Peter Chan <luckygoose@foxmail.com> Co-authored-by: chenbt <34958405+chenbt-hz@users.noreply.github.com> Co-authored-by: Junhua Chen <41671101+cheniujh@users.noreply.github.com> Co-authored-by: cjh <1271435567@qq.com> Co-authored-by: kang jinci <jincikang@gmail.com> Co-authored-by: Xin.Zh <dragoncharlie@foxmail.com> Co-authored-by: machinly <machinlyg@gmail.com> Co-authored-by: A2ureStone <73770413+A2ureStone@users.noreply.github.com> Co-authored-by: J1senn <J1senn@outlook.com> Co-authored-by: chejinge <945997690@qq.com> Co-authored-by: baerwang <52104949+baerwang@users.noreply.github.com> Co-authored-by: ptbxzrt <89020404+ptbxzrt@users.noreply.github.com> Co-authored-by: 你不要过来啊 <73388438+iiiuwioajdks@users.noreply.github.com>
* feat: supported 'ptype' command * Simplified command `type` and `ptype` code * Remove the capacity judgment * fix slavaof serialize response bug (OpenAtomFoundation#1583) Co-authored-by: liuyuecai <liuyuecai@360.cn> * fix (OpenAtomFoundation#1587) (OpenAtomFoundation#1588) * fix unit test:type/set (OpenAtomFoundation#1577) * Fix memory leaks in HandleBGWorkerWriteDB and HandleMetaSyncResponse (OpenAtomFoundation#1590) * fix memory leaks in PikaReplBgWorker::HandleBGWorkerWriteDB(void* arg) and PikaReplClientConn::HandleMetaSyncResponse(void* arg). * add address/thread sanitizer to CMakeLists --------- Co-authored-by: cjh <1271435567@qq.com> * refactor: replace pstd/env with std::filesystem (OpenAtomFoundation#1470) * fix bug issue 1554: using unique_ptr.reset to fix the SIGABRT error (OpenAtomFoundation#1595) using unique_ptr.reset to fix the SIGABRT error && using time-wait lock to notify quickly * fix: incorrect manner of terminating the process (OpenAtomFoundation#1596) * fix issue#1597: add rocksdb dependency to pstd (OpenAtomFoundation#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <J1senn@outlook.com> * fix_info_command * fix: gcc13 compile failed (OpenAtomFoundation#1601) * ci: add unit test to github action (OpenAtomFoundation#1604) * ci: add unit test in github workflow * chore:change `PLATFORM` field logic (OpenAtomFoundation#1615) * fix issue 1517: scan 命令不支持 type 参数 (OpenAtomFoundation#1582) * fix: the unit test of type/set (OpenAtomFoundation#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * [fix issue1621] fix deadlock (OpenAtomFoundation#1620) * [fix] fix deadlock * [fix] fix * command `type` and `ptype` add unit test * fix member variable initialization * Update issue templates * feat: supported 'ptype' command * fix unit test:type/set (OpenAtomFoundation#1577) * fix issue#1597: add rocksdb dependency to pstd (OpenAtomFoundation#1599) * fix g++15 compile failure * add rocksdb dependency to pstd --------- Co-authored-by: J1senn <J1senn@outlook.com> * fix: gcc13 compile failed (OpenAtomFoundation#1601) * fix: the unit test of type/set (OpenAtomFoundation#1617) * test: optimize the return results of srandmember to avoid approximate results * fix: use last_seed for random engine * Modify other modules to use the new `type` function * fix function repeat --------- Co-authored-by: Yuecai Liu <38887641+luky116@users.noreply.github.com> Co-authored-by: liuyuecai <liuyuecai@360.cn> Co-authored-by: Peter Chan <luckygoose@foxmail.com> Co-authored-by: chenbt <34958405+chenbt-hz@users.noreply.github.com> Co-authored-by: Junhua Chen <41671101+cheniujh@users.noreply.github.com> Co-authored-by: cjh <1271435567@qq.com> Co-authored-by: kang jinci <jincikang@gmail.com> Co-authored-by: Xin.Zh <dragoncharlie@foxmail.com> Co-authored-by: machinly <machinlyg@gmail.com> Co-authored-by: A2ureStone <73770413+A2ureStone@users.noreply.github.com> Co-authored-by: J1senn <J1senn@outlook.com> Co-authored-by: chejinge <945997690@qq.com> Co-authored-by: baerwang <52104949+baerwang@users.noreply.github.com> Co-authored-by: ptbxzrt <89020404+ptbxzrt@users.noreply.github.com> Co-authored-by: 你不要过来啊 <73388438+iiiuwioajdks@users.noreply.github.com>
Fixes: #1517