-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys_commands.c
156 lines (130 loc) · 3.4 KB
/
keys_commands.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "server.h"
#include <stdlib.h>
#include <string.h>
static inline bool delete_if_expired(HashTable *htable, HashTableNode *node) {
Object *obj = node->value;
if(!obj->ttl_idx)
return false;
mstime when = state.ttl_heap->elems[obj->ttl_idx - 1].expires_at;
mstime now = state.cmd_start_time;
if(now < when)
return false;
hash_table_remove(htable, node);
return true;
}
HashTableNode* lookup_key(HashTable *htable, const char *key) {
HashTableNode *node = hash_table_get(htable, key);
if(!node)
return NULL;
if(delete_if_expired(htable, node))
return NULL;
return node;
}
static inline bool assert_type_get(
HashTable *htable,
ObjectType type,
const char *key,
void **value
) {
HashTableNode *node = lookup_key(htable, key);
if(!node) {
*value = NULL;
return true;
}
Object *obj = node->value;
if(obj->type != type) {
send_err(ERR_TYPE_MISMATCH);
return false;
}
*value = obj->ptr;
return true;
}
bool get_string_by_key(HashTable *htable, const char *key, char **string) {
return assert_type_get(htable, OBJ_STRING, key, (void**)string);
}
bool get_zset_by_key(HashTable *htable, const char *key, ZSet **zset) {
return assert_type_get(htable, OBJ_ZSET, key, (void**)zset);
}
// GET key
void get_handler(void) {
char *key;
CmdArgState arg_state = INIT_CMD_ARG_STATE;
if(!next_string_arg(&arg_state, &key))
return;
char *value;
if(!get_string_by_key(state.keys, key, &value))
goto end;
if(value)
send_str(value, strlen(value));
else
send_nil();
end:
cmd_restore(&arg_state);
}
// SET key value
void set_handler(void) {
CmdArgState arg_state = INIT_CMD_ARG_STATE;
char *key = NULL, *value = NULL;
Object *value_obj = NULL;
bool should_send_error = true;
if(!next_string_arg(&arg_state, &key)) {
should_send_error = false;
goto error;
}
key = strdup(key);
if(!key)
goto error;
if(!next_string_arg(&arg_state, &value)) {
should_send_error = false;
goto error;
}
value = strdup(value);
if(!value)
goto error;
value_obj = createStringObject(value);
if(!value_obj)
goto error;
if(!hash_table_set(state.keys, key, value_obj))
goto error;
cmd_restore(&arg_state);
send_nil();
return;
error:
if(key) free(key);
if(value) free(value);
if(value_obj) free(value_obj);
cmd_restore(&arg_state);
if(should_send_error)
send_err(ERR_OUT_OF_MEMORY);
}
// DEL key
void del_handler(void) {
CmdArgState arg_state = INIT_CMD_ARG_STATE;
char *key;
if(!next_string_arg(&arg_state, &key))
return;
HashTableNode *value_node = lookup_key(state.keys, key);
uint32_t result;
if(value_node) {
hash_table_remove(state.keys, value_node);
result = 1;
} else {
result = 0;
}
cmd_restore(&arg_state);
send_uint(result);
}
// KEYS
// TODO: call delete_if_expired on each node after implement background hash resize
void keys_handler(void) {
if(!send_arr())
return;
for(HashTableIterator it = hash_table_begin(state.keys);
hash_table_has_next(&it);
hash_table_next(&it))
{
if(!send_str(it.node->key, strlen(it.node->key)))
return;
}
end_arr(state.keys->size);
}