Add memcached version and delete commands

This commit is contained in:
Roman Gershman 2022-02-27 18:04:38 +02:00
parent edf74d2494
commit 3f7e3a5a0a
6 changed files with 30 additions and 11 deletions

View File

@ -130,11 +130,11 @@ a distributed log format.
- [X] stats (partial) - [X] stats (partial)
- [x] append - [x] append
- [x] prepend - [x] prepend
- [ ] delete - [x] delete
- [ ] flush_all - [ ] flush_all
- [x] incr - [x] incr
- [x] decr - [x] decr
- [ ] version - [x] version
- [x] quit - [x] quit
API 2.0 API 2.0

2
helio

@ -1 +1 @@
Subproject commit 1b01326ec8689e559dcf4c5be6f328055479e808 Subproject commit 775f321d8efe9b90fae3fee14b2fcf8364beae7b

View File

@ -160,6 +160,8 @@ void GenericFamily::Del(CmdArgList args, ConnectionContext* cntx) {
VLOG(1) << "Del " << ArgS(args, 1); VLOG(1) << "Del " << ArgS(args, 1);
atomic_uint32_t result{0}; atomic_uint32_t result{0};
bool is_mc = cntx->protocol() == Protocol::MEMCACHE;
auto cb = [&result](const Transaction* t, EngineShard* shard) { auto cb = [&result](const Transaction* t, EngineShard* shard) {
ArgSlice args = t->ShardArgsInShard(shard->shard_id()); ArgSlice args = t->ShardArgsInShard(shard->shard_id());
auto res = OpDel(OpArgs{shard, t->db_index()}, args); auto res = OpDel(OpArgs{shard, t->db_index()}, args);
@ -173,7 +175,18 @@ void GenericFamily::Del(CmdArgList args, ConnectionContext* cntx) {
DVLOG(2) << "Del ts " << transaction->txid(); DVLOG(2) << "Del ts " << transaction->txid();
(*cntx)->SendLong(result.load(memory_order_release)); uint32_t del_cnt = result.load(memory_order_relaxed);
if (is_mc) {
MCReplyBuilder* mc_builder = static_cast<MCReplyBuilder*>(cntx->reply_builder());
if (del_cnt == 0) {
mc_builder->SendNotFound();
} else {
mc_builder->SendDirect("DELETED\r\n");
}
} else {
(*cntx)->SendLong(del_cnt);
}
} }
void GenericFamily::Ping(CmdArgList args, ConnectionContext* cntx) { void GenericFamily::Ping(CmdArgList args, ConnectionContext* cntx) {

View File

@ -489,6 +489,9 @@ void Service::DispatchMC(const MemcacheParser::Command& cmd, std::string_view va
strcpy(cmd_name, "SET"); strcpy(cmd_name, "SET");
strcpy(store_opt, "NX"); strcpy(store_opt, "NX");
break; break;
case MemcacheParser::DELETE:
strcpy(cmd_name, "DEL");
break;
case MemcacheParser::INCR: case MemcacheParser::INCR:
strcpy(cmd_name, "INCRBY"); strcpy(cmd_name, "INCRBY");
absl::numbers_internal::FastIntToBuffer(cmd.delta, store_opt); absl::numbers_internal::FastIntToBuffer(cmd.delta, store_opt);
@ -512,7 +515,9 @@ void Service::DispatchMC(const MemcacheParser::Command& cmd, std::string_view va
case MemcacheParser::STATS: case MemcacheParser::STATS:
server_family_.StatsMC(cmd.key, cntx); server_family_.StatsMC(cmd.key, cntx);
return; return;
case MemcacheParser::VERSION:
mc_builder->SendDirect(absl::StrCat("VERSION ", gflags::VersionString(), "\r\n"));
return;
default: default:
mc_builder->SendClientError("bad command line format"); mc_builder->SendClientError("bad command line format");
return; return;

View File

@ -22,7 +22,7 @@ MP::CmdType From(string_view token) {
{"get", MP::GET}, {"gets", MP::GETS}, {"gat", MP::GAT}, {"get", MP::GET}, {"gets", MP::GETS}, {"gat", MP::GAT},
{"gats", MP::GATS}, {"stats", MP::STATS}, {"incr", MP::INCR}, {"gats", MP::GATS}, {"stats", MP::STATS}, {"incr", MP::INCR},
{"decr", MP::DECR}, {"delete", MP::DELETE}, {"flush_all", MP::FLUSHALL}, {"decr", MP::DECR}, {"delete", MP::DELETE}, {"flush_all", MP::FLUSHALL},
{"quit", MP::QUIT}, {"quit", MP::QUIT}, {"version", MP::VERSION},
}; };
auto it = cmd_map.find(token); auto it = cmd_map.find(token);
@ -165,7 +165,7 @@ auto MP::Parse(string_view str, uint32_t* consumed, Command* cmd) -> Result {
} }
if (num_tokens == 1) { if (num_tokens == 1) {
if (base::_in(cmd->type, {MP::STATS, MP::FLUSHALL, MP::QUIT})) if (base::_in(cmd->type, {MP::STATS, MP::FLUSHALL, MP::QUIT, MP::VERSION}))
return MP::OK; return MP::OK;
return MP::PARSE_ERROR; return MP::PARSE_ERROR;
} }

View File

@ -31,12 +31,13 @@ class MemcacheParser {
STATS = 14, STATS = 14,
QUIT = 20, QUIT = 20,
VERSION = 21,
// The rest of write commands. // The rest of write commands.
DELETE = 21, DELETE = 31,
INCR = 22, INCR = 32,
DECR = 23, DECR = 33,
FLUSHALL = 24, FLUSHALL = 34,
}; };
// According to https://github.com/memcached/memcached/wiki/Commands#standard-protocol // According to https://github.com/memcached/memcached/wiki/Commands#standard-protocol