Repository navigation
Expand file tree
/
Copy pathhandler.cc
More file actions
108 lines (99 loc) · 3.04 KB
/
Copy pathhandler.cc
File metadata and controls
108 lines (99 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <cstdio>
#include <cstring>
#include "handler.hh"
static void ping(Keyspace& keyspace, Cmd *cmd, char *resp, int resp_len);
static void get(Keyspace& keyspace, Cmd *cmd, char *resp, int resp_len);
static void set(Keyspace& keyspace, Cmd *cmd, char *resp, int resp_len);
static void del(Keyspace& keyspace, Cmd *cmd, char *resp, int resp_len);
/************************/
/*** PUBLIC FUNCTIONS ***/
/************************/
void handle_cmd(Keyspace& keyspace, Cmd *cmd, char* resp, int resp_len) {
if (cmd->type == CmdTypePing) {
ping(keyspace, cmd, resp, resp_len);
} else if (cmd->type == CmdTypeGet) {
get(keyspace, cmd, resp, resp_len);
} else if (cmd->type == CmdTypeSet) {
set(keyspace, cmd, resp, resp_len);
} else if (cmd->type == CmdTypeDel) {
del(keyspace, cmd, resp, resp_len);
} else if (cmd->type == CmdTypeCommand) {
sprintf(resp, "+OK\r\n");
} else {
sprintf(resp, "-ERR: Unsupported command\r\n");
}
}
/*************************/
/*** PRIVATE FUNCTIONS ***/
/*************************/
static void ping(Keyspace& keyspace, Cmd *cmd, char *resp, int resp_len) {
if (cmd->argc == 1) {
sprintf(resp, "+PONG\r\n");
} else {
std::string pong{std::move(cmd->argv[1])};
sprintf(resp, "$%lu\r\n%s\r\n", pong.length(), pong.c_str());
}
}
static void get(Keyspace& keyspace, Cmd *cmd, char *resp, int resp_len) {
if (cmd->argc < 2) {
sprintf(resp, "-ERR: Not enough arguments\r\n");
} else {
std::string key{std::move(cmd->argv[1])};
auto it = keyspace.find(key);
if (it == keyspace.end()) {
sprintf(resp, "$-1\r\n");
return;
}
RadahnObject obj = it->second;
if (obj.exp != -1) {
time_t now = time(NULL);
if (now >= obj.exp) {
keyspace.erase(it);
sprintf(resp, "$-1\r\n");
return;
}
}
sprintf(resp, "$%lu\r\n%s\r\n", obj.val.length(), obj.val.c_str());
}
}
static void set(Keyspace& keyspace, Cmd *cmd, char *resp, int resp_len) {
if (cmd->argc < 3) {
sprintf(resp, "-ERR: Not enough arguments\r\n");
} else {
time_t exp = -1;
if (cmd->argc == 5) {
if (cmd->argv[3] != "EX") {
sprintf(resp, "-ERR: Unsupported command option\r\n");
return;
};
int secs = atoi(cmd->argv[4].c_str());
if (secs <= 0) {
sprintf(resp, "-ERR: Invalid EX seconds value\r\n");
return;
}
exp = time(NULL) + secs;
}
std::string key{std::move(cmd->argv[1])};
RadahnObject obj;
obj.val = std::move(cmd->argv[2]);
obj.exp = exp;
keyspace.insert_or_assign(key, obj);
sprintf(resp, "+OK\r\n");
}
}
static void del(Keyspace& keyspace, Cmd *cmd, char *resp, int resp_len) {
if (cmd->argc < 2) {
sprintf(resp, "-ERR: Not enough arguments\r\n");
} else {
int n_del = 0;
for (unsigned int i = 1; i < cmd->argc; i++) {
std::string key{std::move(cmd->argv[1])};
auto found = keyspace.find(key);
if (found != keyspace.end()) {
keyspace.erase(found);
n_del++;
}
}
sprintf(resp, ":%d\r\n", n_del);
}
}