2021-11-28 15:29:26 +08:00
|
|
|
// Copyright 2021, Roman Gershman. All rights reserved.
|
|
|
|
// See LICENSE for licensing terms.
|
2021-11-22 15:43:43 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace dfly {
|
|
|
|
|
|
|
|
// Memcache parser does not parse value blobs, only the commands.
|
|
|
|
// The expectation is that the caller will parse the command and
|
|
|
|
// then will follow up with reading the blob data directly from source.
|
|
|
|
class MemcacheParser {
|
|
|
|
public:
|
|
|
|
enum CmdType {
|
|
|
|
INVALID = 0,
|
|
|
|
SET = 1,
|
|
|
|
ADD = 2,
|
|
|
|
REPLACE = 3,
|
|
|
|
APPEND = 4,
|
|
|
|
PREPEND = 5,
|
|
|
|
CAS = 6,
|
|
|
|
|
|
|
|
// Retrieval
|
|
|
|
GET = 10,
|
|
|
|
GETS = 11,
|
|
|
|
GAT = 12,
|
|
|
|
GATS = 13,
|
2022-02-22 04:39:59 +08:00
|
|
|
STATS = 14,
|
2021-11-22 15:43:43 +08:00
|
|
|
|
2022-02-24 05:54:56 +08:00
|
|
|
QUIT = 20,
|
|
|
|
|
|
|
|
// The rest of write commands.
|
2021-11-22 15:43:43 +08:00
|
|
|
DELETE = 21,
|
|
|
|
INCR = 22,
|
|
|
|
DECR = 23,
|
2022-02-24 05:54:56 +08:00
|
|
|
FLUSHALL = 24,
|
2021-11-22 15:43:43 +08:00
|
|
|
};
|
|
|
|
|
2022-02-21 04:07:33 +08:00
|
|
|
// According to https://github.com/memcached/memcached/wiki/Commands#standard-protocol
|
2021-11-22 15:43:43 +08:00
|
|
|
struct Command {
|
|
|
|
CmdType type = INVALID;
|
|
|
|
std::string_view key;
|
|
|
|
std::vector<std::string_view> keys_ext;
|
|
|
|
|
2022-02-24 05:54:56 +08:00
|
|
|
union {
|
|
|
|
uint64_t cas_unique = 0; // for CAS COMMAND
|
|
|
|
uint64_t delta; // for DECR/INCR commands.
|
|
|
|
};
|
|
|
|
|
2022-02-21 04:07:33 +08:00
|
|
|
uint32_t expire_ts = 0; // relative time in seconds.
|
2021-11-22 15:43:43 +08:00
|
|
|
uint32_t bytes_len = 0;
|
2022-02-21 04:07:33 +08:00
|
|
|
uint32_t flags = 0;
|
2021-11-22 15:43:43 +08:00
|
|
|
bool no_reply = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Result {
|
|
|
|
OK,
|
|
|
|
INPUT_PENDING,
|
|
|
|
UNKNOWN_CMD,
|
|
|
|
BAD_INT,
|
|
|
|
PARSE_ERROR,
|
2022-02-24 05:54:56 +08:00
|
|
|
BAD_DELTA,
|
2021-11-22 15:43:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool IsStoreCmd(CmdType type) {
|
|
|
|
return type >= SET && type <= CAS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Parse(std::string_view str, uint32_t* consumed, Command* res);
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dfly
|