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 issue #1517: scan 命令不支持 type 参数 #1582

Merged
merged 1 commit into from
Jun 14, 2023

Conversation

ptbxzrt
Copy link
Contributor

@ptbxzrt ptbxzrt commented Jun 3, 2023

image

Fixes: #1517

void DoInitial() override;
void Clear() override {
pattern_ = "*";
count_ = 10;
type_ = storage::kAll;
}
};

Copy link

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);
Copy link

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
Copy link

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:

  1. The test case "SCAN TYPE" could be split into multiple tests for better readability and maintainability.

  2. Consider adding more test cases to cover different scenarios and edge cases.

  3. Use meaningful variable names instead of short ones such as "cur," "res," and "k."

  4. Consider documenting the purpose of each test case.

  5. Consider using the "incr" command instead of "*}$k" to append elements to a list.

  6. Identify specific Redis versions that the code is tested on.

  7. Consider using a consistent style throughout the code.

void DoInitial() override;
void Clear() override {
pattern_ = "*";
count_ = 10;
type_ = storage::DataType::kAll;
}
};

Copy link

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);
Copy link

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 like secure_compare() provided by libsodium.
  • It is unclear what PIKA_SCAN_STEP_LENGTH represents, which makes it difficult to understand the purpose of batch_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.

@ptbxzrt ptbxzrt changed the title fix issue 1517: scan 命令不支持 type 参数 fix issue #1517: scan 命令不支持 type 参数 Jun 3, 2023
@ptbxzrt
Copy link
Contributor Author

ptbxzrt commented Jun 3, 2023

#1517

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
Copy link

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.

@@ -219,6 +222,7 @@ enum CmdFlags {
kCmdFlagsHyperLogLog = 14,
kCmdFlagsGeo = 16,
kCmdFlagsPubSub = 18,
kCmdFlagsModule = 20,
kCmdFlagsNoLocal = 0, // default nolocal
kCmdFlagsLocal = 32,
kCmdFlagsNoSuspend = 0, // default nosuspend
Copy link

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.

virtual void Clear() { arguments_.clear(); }
};

#endif No newline at end of file
Copy link

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;
}
};

Copy link

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);
Copy link

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
Copy link

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:

  1. 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.

  2. 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.

  3. Another good practice that can always be considered is adding additional edge test cases like empty values being evaluated, non-existing types, etc.

  4. 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.

@AlexStocks AlexStocks merged commit 59b1193 into OpenAtomFoundation:unstable Jun 14, 2023
AlexStocks added a commit that referenced this pull request Jul 2, 2023
* 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>
bigdaronlee163 pushed a commit to bigdaronlee163/pika that referenced this pull request Jun 8, 2024
bigdaronlee163 pushed a commit to bigdaronlee163/pika that referenced this pull request Jun 8, 2024
* 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>
cheniujh added a commit to cheniujh/pika that referenced this pull request Sep 24, 2024
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

scan 命令不支持 type 参数
3 participants