infdahai commented on code in PR #1414:
URL:
https://github.com/apache/incubator-kvrocks/pull/1414#discussion_r1182004201
##########
src/cluster/cluster.cc:
##########
@@ -641,6 +649,54 @@ Status Cluster::LoadClusterNodes(const std::string
&file_path) {
return SetClusterNodes(nodes_info, version, false);
}
+// TODO: maybe it needs to use a more precise error type to represent `NotOk`.
+Status Cluster::parseSlotRange(const std::string &slots_str,
std::vector<std::pair<int, int>> &slots) {
+ if (slots_str.empty()) {
+ return {Status::NotOK, "Don't use empty slots."};
+ }
+ std::vector<std::string> slot_ranges = util::Split(slots_str, " ");
+
+ if (slot_ranges.empty()) {
+ return {Status::NotOK, fmt::format("Invalid slots: {}. Please use ' ' to
space slots.", slots_str)};
+ }
+
+ auto is_number = [&](const std::string &s) {
+#if __cplusplus >= 202002L
+ return std::ranges::all_of(s.begin(), s.end(), [](char c) { return
isdigit(c) != 0; });
+#else
+ for (char c : s) {
+ if (isdigit(c) == 0) {
+ return false;
+ }
+ }
+ return true;
+#endif
+ };
+
+ // Parse all slots(include slot ranges)
+ for (const auto &slot_range : slot_ranges) {
+ if (is_number(slot_range)) {
Review Comment:
yeah, I change the logic.
##########
src/commands/cmd_cluster.cc:
##########
@@ -219,7 +210,7 @@ class CommandClusterX : public Commander {
*output = redis::Error(s.Msg());
}
} else if (subcommand_ == "setslot") {
- Status s = svr->cluster->SetSlot(slot_id_, args_[4], set_version_);
+ Status s = svr->cluster->SetSlot(slots_str_, args_[4], set_version_);
Review Comment:
I'm not sure to do this. `CommandClusterX::Parse` is only used to find some
keywords and do the simple check.
If necessary, I can migrate `parse` functions in `cluster.h` to
`CommandClusterX`.
##########
src/cluster/cluster.cc:
##########
@@ -641,6 +649,54 @@ Status Cluster::LoadClusterNodes(const std::string
&file_path) {
return SetClusterNodes(nodes_info, version, false);
}
+// TODO: maybe it needs to use a more precise error type to represent `NotOk`.
+Status Cluster::parseSlotRange(const std::string &slots_str,
std::vector<std::pair<int, int>> &slots) {
+ if (slots_str.empty()) {
+ return {Status::NotOK, "Don't use empty slots."};
+ }
+ std::vector<std::string> slot_ranges = util::Split(slots_str, " ");
+
+ if (slot_ranges.empty()) {
+ return {Status::NotOK, fmt::format("Invalid slots: {}. Please use ' ' to
space slots.", slots_str)};
+ }
+
+ auto is_number = [&](const std::string &s) {
+#if __cplusplus >= 202002L
+ return std::ranges::all_of(s.begin(), s.end(), [](char c) { return
isdigit(c) != 0; });
+#else
+ for (char c : s) {
+ if (isdigit(c) == 0) {
+ return false;
+ }
+ }
+ return true;
+#endif
+ };
+
+ // Parse all slots(include slot ranges)
+ for (const auto &slot_range : slot_ranges) {
+ if (is_number(slot_range)) {
+ int s_start = stoi(slot_range);
+ assert(IsValidSlot(s_start));
+ slots.emplace_back(std::make_pair(s_start, s_start));
+ continue;
+ }
+
+ // parse slot range: "int1-int2" (satisfy: int1 <= int2 )
+ assert(slot_range.back() != '-');
+ std::vector<std::string> fields = util::Split(slot_range, "-");
+ assert(fields.size() == 2);
Review Comment:
you're right. I will remove `assert`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]