infdahai commented on code in PR #1414:
URL:
https://github.com/apache/incubator-kvrocks/pull/1414#discussion_r1182174260
##########
src/cluster/cluster.cc:
##########
@@ -641,6 +648,56 @@ 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::parseSlotRanges(const std::string &slots_str,
std::vector<std::pair<int, int>> &slots) {
+ if (slots_str.empty()) {
+ return {Status::NotOK, "No slots to parse."};
+ }
+ 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 valid_range = NumericRange<int>{0, kClusterSlots - 1};
+ // Parse all slots(include slot ranges)
+ for (auto &slot_range : slot_ranges) {
+ if (slot_range.find('-') == std::string::npos) {
+ auto parse_result = ParseInt<int>(slot_range, valid_range, 10);
+ if (!parse_result) {
+ return {Status::NotOK, errInvalidSlotID};
+ }
+ int slot_start = *parse_result;
+ slots.emplace_back(std::make_pair(slot_start, slot_start));
+ continue;
+ }
+
+ // parse slot range: "int1-int2" (satisfy: int1 <= int2 )
+ if (slot_range.back() == '-') {
+ return {Status::NotOK,
+ fmt::format("Invalid slot range: {}. The character '-' can't
appear in the last position.", slot_range)};
+ }
Review Comment:
in util/Split(),this process is below.
```cpp
size_t begin = 0, end = in.find_first_of(delim);
do {
std::string elem = in.substr(begin, end - begin);
if (!elem.empty()) out.push_back(std::move(elem));
if (end == std::string::npos) break;
begin = end + 1;
end = in.find_first_of(delim, begin);
} while (true);
```
when I use the range such as `3-4-`, I also get a valid result. Because the
last `-` just ignores and breaks in this func.
--
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]