mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2025-05-11 02:15:45 +02:00
refactor: remove CmdArgParser::ToUpper() (#3831)
This commit is contained in:
parent
14c94e41d7
commit
843adf85a6
4 changed files with 25 additions and 46 deletions
|
@ -39,9 +39,4 @@ CmdArgParser::~CmdArgParser() {
|
|||
// TODO DCHECK(!HasNext()) << "Not all args were processed";
|
||||
}
|
||||
|
||||
void CmdArgParser::ToUpper(size_t i) {
|
||||
for (auto& c : args_[i])
|
||||
c = absl::ascii_toupper(c);
|
||||
}
|
||||
|
||||
} // namespace facade
|
||||
|
|
|
@ -151,13 +151,6 @@ struct CmdArgParser {
|
|||
return !HasError();
|
||||
}
|
||||
|
||||
// In-place convert the next argument to uppercase
|
||||
CmdArgParser& ToUpper() {
|
||||
if (cur_i_ < args_.size())
|
||||
ToUpper(cur_i_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Return remaining arguments
|
||||
CmdArgList Tail() const {
|
||||
return args_.subspan(cur_i_);
|
||||
|
@ -261,8 +254,6 @@ struct CmdArgParser {
|
|||
return {};
|
||||
}
|
||||
|
||||
void ToUpper(size_t i);
|
||||
|
||||
private:
|
||||
size_t cur_i_ = 0;
|
||||
CmdArgList args_;
|
||||
|
|
|
@ -958,25 +958,24 @@ OpResult<std::vector<ResultType>> StateExecutor::Execute(const CommandList& comm
|
|||
return results;
|
||||
}
|
||||
|
||||
nonstd::expected<CommonAttributes, std::string> ParseCommonAttr(CmdArgParser* prser) {
|
||||
CmdArgParser& parser = *prser;
|
||||
nonstd::expected<CommonAttributes, std::string> ParseCommonAttr(CmdArgParser* parser) {
|
||||
CommonAttributes parsed;
|
||||
using nonstd::make_unexpected;
|
||||
if (!parser.HasAtLeast(2)) {
|
||||
|
||||
auto [encoding, offset_str] = parser->Next<string_view, string_view>();
|
||||
|
||||
if (encoding.empty()) {
|
||||
return make_unexpected(kSyntaxErr);
|
||||
}
|
||||
|
||||
auto encoding = parser.ToUpper().Next();
|
||||
if (absl::StartsWith(encoding, "U")) {
|
||||
if (encoding[0] == 'U' || encoding[0] == 'u') {
|
||||
parsed.type = EncodingType::UINT;
|
||||
} else if (absl::StartsWith(encoding, "I")) {
|
||||
} else if (encoding[0] == 'I' || encoding[0] == 'i') {
|
||||
parsed.type = EncodingType::INT;
|
||||
} else {
|
||||
return make_unexpected(kSyntaxErr);
|
||||
}
|
||||
|
||||
std::string_view bits = encoding;
|
||||
bits = bits.substr(1);
|
||||
std::string_view bits = encoding.substr(1);
|
||||
|
||||
if (!absl::SimpleAtoi(bits, &parsed.encoding_bit_size)) {
|
||||
return make_unexpected(kSyntaxErr);
|
||||
|
@ -994,7 +993,6 @@ nonstd::expected<CommonAttributes, std::string> ParseCommonAttr(CmdArgParser* pr
|
|||
"is.");
|
||||
}
|
||||
|
||||
std::string_view offset_str = parser.Next();
|
||||
bool is_proxy = false;
|
||||
if (absl::StartsWith(offset_str, "#")) {
|
||||
offset_str = offset_str.substr(1);
|
||||
|
@ -1013,20 +1011,20 @@ nonstd::expected<CommonAttributes, std::string> ParseCommonAttr(CmdArgParser* pr
|
|||
// Returns the CommandList if the parsing completed succefully or std::string
|
||||
// to indicate an error
|
||||
nonstd::expected<CommandList, std::string> ParseToCommandList(CmdArgList args, bool read_only) {
|
||||
enum class Cmds { OVERFLOW, GET, SET, INCRBY };
|
||||
CommandList result;
|
||||
|
||||
using nonstd::make_unexpected;
|
||||
|
||||
CmdArgParser parser(args);
|
||||
while (parser.HasNext()) {
|
||||
if (!parser.HasAtLeast(2)) {
|
||||
auto cmd = parser.MapNext("OVERFLOW", Cmds::OVERFLOW, "GET", Cmds::GET, "SET", Cmds::SET,
|
||||
"INCRBY", Cmds::INCRBY);
|
||||
if (parser.Error()) {
|
||||
return make_unexpected(kSyntaxErr);
|
||||
}
|
||||
|
||||
auto op = parser.ToUpper().Next();
|
||||
|
||||
using namespace std::string_view_literals;
|
||||
if (op == "OVERFLOW"sv) {
|
||||
if (cmd == Cmds::OVERFLOW) {
|
||||
if (read_only) {
|
||||
make_unexpected("BITFIELD_RO only supports the GET subcommand");
|
||||
}
|
||||
|
@ -1042,11 +1040,12 @@ nonstd::expected<CommandList, std::string> ParseToCommandList(CmdArgList args, b
|
|||
|
||||
auto maybe_attr = ParseCommonAttr(&parser);
|
||||
if (!maybe_attr.has_value()) {
|
||||
parser.Error();
|
||||
return make_unexpected(std::move(maybe_attr.error()));
|
||||
}
|
||||
|
||||
auto attr = maybe_attr.value();
|
||||
if (op == "GET"sv) {
|
||||
if (cmd == Cmds::GET) {
|
||||
result.push_back(Command(Get(attr)));
|
||||
continue;
|
||||
}
|
||||
|
@ -1055,21 +1054,20 @@ nonstd::expected<CommandList, std::string> ParseToCommandList(CmdArgList args, b
|
|||
return make_unexpected("BITFIELD_RO only supports the GET subcommand");
|
||||
}
|
||||
|
||||
auto value = parser.Next<int64_t>();
|
||||
if (parser.HasError()) {
|
||||
parser.Error();
|
||||
int64_t value = parser.Next<int64_t>();
|
||||
if (parser.Error()) {
|
||||
return make_unexpected(kSyntaxErr);
|
||||
}
|
||||
|
||||
if (op == "SET"sv) {
|
||||
if (cmd == Cmds::SET) {
|
||||
result.push_back(Command(Set(attr, value)));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (op == "INCRBY"sv) {
|
||||
if (cmd == Cmds::INCRBY) {
|
||||
result.push_back(Command(IncrBy(attr, value)));
|
||||
continue;
|
||||
}
|
||||
parser.Error();
|
||||
return make_unexpected(kSyntaxErr);
|
||||
}
|
||||
|
||||
|
|
|
@ -371,12 +371,7 @@ void MemoryCmd::Track(CmdArgList args) {
|
|||
|
||||
CmdArgParser parser(args);
|
||||
|
||||
string_view sub_cmd = parser.ToUpper().Next();
|
||||
if (parser.HasError()) {
|
||||
return cntx_->SendError(parser.Error()->MakeReply());
|
||||
}
|
||||
|
||||
if (sub_cmd == "ADD") {
|
||||
if (parser.Check("ADD")) {
|
||||
AllocationTracker::TrackingInfo tracking_info;
|
||||
std::tie(tracking_info.lower_bound, tracking_info.upper_bound, tracking_info.sample_odds) =
|
||||
parser.Next<size_t, size_t, double>();
|
||||
|
@ -398,7 +393,7 @@ void MemoryCmd::Track(CmdArgList args) {
|
|||
}
|
||||
}
|
||||
|
||||
if (sub_cmd == "REMOVE") {
|
||||
if (parser.Check("REMOVE")) {
|
||||
auto [lower_bound, upper_bound] = parser.Next<size_t, size_t>();
|
||||
if (parser.HasError()) {
|
||||
return cntx_->SendError(parser.Error()->MakeReply());
|
||||
|
@ -418,12 +413,12 @@ void MemoryCmd::Track(CmdArgList args) {
|
|||
}
|
||||
}
|
||||
|
||||
if (sub_cmd == "CLEAR") {
|
||||
if (parser.Check("CLEAR")) {
|
||||
shard_set->pool()->AwaitBrief([&](unsigned index, auto*) { AllocationTracker::Get().Clear(); });
|
||||
return cntx_->SendOk();
|
||||
}
|
||||
|
||||
if (sub_cmd == "GET") {
|
||||
if (parser.Check("GET")) {
|
||||
auto ranges = AllocationTracker::Get().GetRanges();
|
||||
auto* rb = static_cast<facade::RedisReplyBuilder*>(cntx_->reply_builder());
|
||||
rb->StartArray(ranges.size());
|
||||
|
@ -434,7 +429,7 @@ void MemoryCmd::Track(CmdArgList args) {
|
|||
return;
|
||||
}
|
||||
|
||||
if (sub_cmd == "ADDRESS") {
|
||||
if (parser.Check("ADDRESS")) {
|
||||
string_view ptr_str = parser.Next();
|
||||
if (parser.HasError()) {
|
||||
return cntx_->SendError(parser.Error()->MakeReply());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue