-
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
Merged
+338
−5
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
08ce98c
fix: fix a bug of hmget and Zset
haiyang426 106a216
fix: fix a bug of hmget and Zset
haiyang426 a08635d
fix: fix a bug of hmget and Zset
haiyang426 1349df0
Merge branch 'OpenAtomFoundation:unstable' into unstable
haiyang426 c0f59f9
Merge branch 'OpenAtomFoundation:unstable' into unstable
haiyang426 cd274a9
feat: add sort command
haiyang426 9b9dc71
make format
haiyang426 748d795
Merge branch 'OpenAtomFoundation:unstable' into unstable
haiyang426 f6a753a
add GO test and fix
haiyang426 f086ab3
add GO test and fix
haiyang426 bbe6624
change timeout
haiyang426 b064f70
Delete unnecessary comments
haiyang426 990d4bd
change max open files
haiyang426 789bbac
change max open files
haiyang426 ea46866
remove unused variables and move parser func to Doinitial
haiyang426 af9a8ce
remove unused variables and move parser func to Doinitial
haiyang426 ccbf94c
make format
haiyang426 648c6bd
fix style privte variable name
haiyang426 a01c008
make format
haiyang426 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,206 @@ 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. 要是获取数据失败了怎么办,这里可以根据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()); | ||
} | ||
} | ||
} | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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会不会好一些