-
Notifications
You must be signed in to change notification settings - Fork 63
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
feat: add sort commands #357
Changes from 17 commits
08ce98c
106a216
a08635d
1349df0
c0f59f9
cd274a9
9b9dc71
748d795
f6a753a
f086ab3
bbe6624
b064f70
990d4bd
789bbac
ea46866
af9a8ce
ccbf94c
648c6bd
a01c008
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 |
---|---|---|
|
@@ -6,9 +6,15 @@ | |
*/ | ||
|
||
#include "cmd_admin.h" | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
#include "db.h" | ||
|
||
#include "braft/raft.h" | ||
#include "pstd_string.h" | ||
#include "rocksdb/version.h" | ||
|
||
#include "pikiwidb.h" | ||
|
@@ -258,4 +264,205 @@ void CmdDebugSegfault::DoCmd(PClient* client) { | |
*ptr = 0; | ||
} | ||
|
||
SortCmd::SortCmd(const std::string& name, int16_t arity) | ||
: BaseCmd(name, arity, kCmdFlagsAdmin | kCmdFlagsWrite, kAclCategoryAdmin) {} | ||
|
||
bool SortCmd::DoInitial(PClient* client) { | ||
InitialArgument(); | ||
client->SetKey(client->argv_[1]); | ||
size_t argc = client->argv_.size(); | ||
for (int i = 2; i < argc; ++i) { | ||
int leftargs = argc - i - 1; | ||
if (strcasecmp(client->argv_[i].data(), "asc") == 0) { | ||
desc = 0; | ||
} else if (strcasecmp(client->argv_[i].data(), "desc") == 0) { | ||
desc = 1; | ||
} else if (strcasecmp(client->argv_[i].data(), "alpha") == 0) { | ||
alpha = 1; | ||
} else if (strcasecmp(client->argv_[i].data(), "limit") == 0 && leftargs >= 2) { | ||
if (pstd::String2int(client->argv_[i + 1], &offset) == 0 || pstd::String2int(client->argv_[i + 2], &count) == 0) { | ||
client->SetRes(CmdRes::kSyntaxErr); | ||
return false; | ||
} | ||
i += 2; | ||
} else if (strcasecmp(client->argv_[i].data(), "store") == 0 && leftargs >= 1) { | ||
store_key = client->argv_[i + 1]; | ||
i++; | ||
} else if (strcasecmp(client->argv_[i].data(), "by") == 0 && leftargs >= 1) { | ||
sortby = client->argv_[i + 1]; | ||
if (sortby.find('*') == std::string::npos) { | ||
dontsort = 1; | ||
} | ||
i++; | ||
} else if (strcasecmp(client->argv_[i].data(), "get") == 0 && leftargs >= 1) { | ||
get_patterns.push_back(client->argv_[i + 1]); | ||
i++; | ||
} else { | ||
client->SetRes(CmdRes::kSyntaxErr); | ||
return false; | ||
} | ||
} | ||
|
||
Status s; | ||
s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LRange(client->Key(), 0, -1, &ret); | ||
if (s.ok()) { | ||
return true; | ||
} else if (!s.IsNotFound()) { | ||
client->SetRes(CmdRes::kErrOther, s.ToString()); | ||
return false; | ||
} | ||
|
||
s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SMembers(client->Key(), &ret); | ||
if (s.ok()) { | ||
return true; | ||
} else if (!s.IsNotFound()) { | ||
client->SetRes(CmdRes::kErrOther, s.ToString()); | ||
return false; | ||
} | ||
|
||
std::vector<storage::ScoreMember> score_members; | ||
s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZRange(client->Key(), 0, -1, &score_members); | ||
if (s.ok()) { | ||
for (auto& c : score_members) { | ||
ret.emplace_back(c.member); | ||
} | ||
return true; | ||
} else if (!s.IsNotFound()) { | ||
client->SetRes(CmdRes::kErrOther, s.ToString()); | ||
return false; | ||
} | ||
client->SetRes(CmdRes::kErrOther, "Unknown Type"); | ||
return false; | ||
} | ||
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. 方法 该方法实现了 - void SortCmd::DoInitial(PClient* client) {
+ // 建议拆分此方法,将参数解析逻辑分别封装到独立的私有方法中。
|
||
|
||
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. 要是获取数据失败了怎么办,这里可以根据status加一个错误处理 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.
嗯嗯,感谢,我再修改一下 |
||
void SortCmd::DoCmd(PClient* client) { | ||
std::vector<RedisSortObject> sort_ret(ret.size()); | ||
for (size_t i = 0; i < ret.size(); ++i) { | ||
sort_ret[i].obj = ret[i]; | ||
} | ||
|
||
if (!dontsort) { | ||
for (size_t i = 0; i < ret.size(); ++i) { | ||
std::string byval; | ||
if (!sortby.empty()) { | ||
auto lookup = lookupKeyByPattern(client, sortby, ret[i]); | ||
if (!lookup.has_value()) { | ||
byval = ret[i]; | ||
} else { | ||
byval = std::move(lookup.value()); | ||
} | ||
} else { | ||
byval = ret[i]; | ||
} | ||
|
||
if (alpha) { | ||
sort_ret[i].u = byval; | ||
} else { | ||
double double_byval; | ||
if (pstd::String2d(byval, &double_byval)) { | ||
sort_ret[i].u = double_byval; | ||
} else { | ||
client->SetRes(CmdRes::kErrOther, "One or more scores can't be converted into double"); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
std::sort(sort_ret.begin(), sort_ret.end(), [this](const RedisSortObject& a, const RedisSortObject& b) { | ||
if (this->alpha) { | ||
std::string score_a = std::get<std::string>(a.u); | ||
std::string score_b = std::get<std::string>(b.u); | ||
return !this->desc ? score_a < score_b : score_a > score_b; | ||
} else { | ||
double score_a = std::get<double>(a.u); | ||
double score_b = std::get<double>(b.u); | ||
return !this->desc ? score_a < score_b : score_a > score_b; | ||
} | ||
}); | ||
|
||
size_t sort_size = sort_ret.size(); | ||
|
||
count = count >= 0 ? count : sort_size; | ||
offset = (offset >= 0 && offset < sort_size) ? offset : sort_size; | ||
count = (offset + count < sort_size) ? count : sort_size - offset; | ||
|
||
size_t m_start = offset; | ||
size_t m_end = offset + count; | ||
|
||
ret.clear(); | ||
if (get_patterns.empty()) { | ||
get_patterns.emplace_back("#"); | ||
} | ||
|
||
for (; m_start < m_end; m_start++) { | ||
for (const std::string& pattern : get_patterns) { | ||
std::optional<std::string> val = lookupKeyByPattern(client, pattern, sort_ret[m_start].obj); | ||
if (val.has_value()) { | ||
ret.push_back(val.value()); | ||
} else { | ||
ret.emplace_back(""); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (store_key.empty()) { | ||
client->AppendStringVector(ret); | ||
} else { | ||
uint64_t reply_num = 0; | ||
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPush(store_key, ret, &reply_num); | ||
if (s.ok()) { | ||
client->AppendInteger(reply_num); | ||
} else { | ||
client->SetRes(CmdRes::kErrOther, s.ToString()); | ||
} | ||
} | ||
} | ||
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. 方法 该方法实现了 - void SortCmd::DoCmd(PClient* client) {
+ // 建议拆分此方法,将排序逻辑和结果存储分别封装到独立的私有方法中。
|
||
|
||
std::optional<std::string> SortCmd::lookupKeyByPattern(PClient* client, const std::string& pattern, | ||
const std::string& subst) { | ||
if (pattern == "#") { | ||
return subst; | ||
} | ||
|
||
auto match_pos = pattern.find('*'); | ||
if (match_pos == std::string::npos) { | ||
return std::nullopt; | ||
} | ||
|
||
std::string field; | ||
auto arrow_pos = pattern.find("->", match_pos + 1); | ||
if (arrow_pos != std::string::npos && arrow_pos + 2 < pattern.size()) { | ||
field = pattern.substr(arrow_pos + 2); | ||
} | ||
|
||
std::string key = pattern.substr(0, match_pos + 1); | ||
key.replace(match_pos, 1, subst); | ||
|
||
std::string value; | ||
storage::Status s; | ||
if (!field.empty()) { | ||
s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HGet(key, field, &value); | ||
} else { | ||
s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Get(key, &value); | ||
} | ||
|
||
if (!s.ok()) { | ||
return std::nullopt; | ||
} | ||
|
||
return value; | ||
} | ||
|
||
void SortCmd::InitialArgument() { | ||
desc = 0; | ||
alpha = 0; | ||
offset = 0; | ||
count = -1; | ||
dontsort = 0; | ||
store_key.clear(); | ||
sortby.clear(); | ||
get_patterns.clear(); | ||
ret.clear(); | ||
} | ||
} // namespace pikiwidb |
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.
293行到324行的代码主要是做命令行的解析,挪到doinitial会不会好一些