-
Notifications
You must be signed in to change notification settings - Fork 11
/
commands.c
372 lines (344 loc) · 12.4 KB
/
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Commands for OMcache
*
* Copyright (c) 2013-2014, Oskari Saarenmaa <[email protected]>
* All rights reserved.
*
* This file is under the Apache License, Version 2.0.
* See the file `LICENSE` for details.
*
*/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include "compat.h"
// Note: commands.c only uses the "public" omcache api
#include "omcache.h"
#include "memcached_protocol_binary.h"
#define QCMD(k) (timeout_msec ? k : k ## Q)
int omcache_noop(omcache_t *mc, int server_index, int32_t timeout_msec)
{
omcache_req_t req[1] = {{
.server_index = server_index,
.header = {
.opcode = PROTOCOL_BINARY_CMD_NOOP,
},
}};
return omcache_command_status(mc, req, timeout_msec);
}
int omcache_stat(omcache_t *mc, const char *command,
omcache_value_t *values, size_t *value_count,
int server_index, int32_t timeout_msec)
{
size_t key_len = command ? strlen(command) : 0;
size_t req_count = 1;
omcache_req_t req[1] = {{
.server_index = server_index,
.header = {
.opcode = PROTOCOL_BINARY_CMD_STAT,
.keylen = htobe16(key_len),
.bodylen = htobe32(key_len),
},
.key = (const unsigned char *) command,
}};
return omcache_command(mc, req, &req_count, values, value_count, timeout_msec);
}
int omcache_flush_all(omcache_t *mc, time_t expiration, int server_index, int32_t timeout_msec)
{
uint32_t body_exp = htobe32(expiration);
omcache_req_t req[1] = {{
.server_index = server_index,
.header = {
.opcode = PROTOCOL_BINARY_CMD_FLUSH,
.extlen = sizeof(body_exp),
.bodylen = htobe32(sizeof(body_exp)),
},
.extra = &body_exp,
}};
return omcache_command_status(mc, req, timeout_msec);
}
struct protocol_binary_set_request_body_s {
uint32_t flags;
uint32_t expiration;
} __attribute__((packed));
static int omc_set_cmd(omcache_t *mc, protocol_binary_command opcode,
const unsigned char *key, size_t key_len,
const unsigned char *value, size_t value_len,
time_t expiration, uint32_t flags,
uint64_t cas, int32_t timeout_msec)
{
struct protocol_binary_set_request_body_s extra = {
.flags = htobe32(flags),
.expiration = htobe32(expiration),
};
size_t extra_len = sizeof(extra);
if (opcode == PROTOCOL_BINARY_CMD_APPEND || opcode == PROTOCOL_BINARY_CMD_APPENDQ ||
opcode == PROTOCOL_BINARY_CMD_PREPEND || opcode == PROTOCOL_BINARY_CMD_PREPENDQ)
extra_len = 0;
omcache_req_t req[1] = {{
.server_index = -1,
.header = {
.opcode = opcode,
.extlen = extra_len,
.keylen = htobe16(key_len),
.bodylen = htobe32(key_len + value_len + extra_len),
.cas = htobe64(cas),
},
.extra = &extra,
.key = key,
.data = value,
}};
return omcache_command_status(mc, req, timeout_msec);
}
int omcache_set(omcache_t *mc,
const unsigned char *key, size_t key_len,
const unsigned char *value, size_t value_len,
time_t expiration, uint32_t flags,
uint64_t cas, int32_t timeout_msec)
{
return omc_set_cmd(mc, QCMD(PROTOCOL_BINARY_CMD_SET),
key, key_len, value, value_len,
expiration, flags, cas, timeout_msec);
}
int omcache_add(omcache_t *mc,
const unsigned char *key, size_t key_len,
const unsigned char *value, size_t value_len,
time_t expiration, uint32_t flags,
int32_t timeout_msec)
{
return omc_set_cmd(mc, QCMD(PROTOCOL_BINARY_CMD_ADD),
key, key_len, value, value_len,
expiration, flags, 0, timeout_msec);
}
int omcache_replace(omcache_t *mc,
const unsigned char *key, size_t key_len,
const unsigned char *value, size_t value_len,
time_t expiration, uint32_t flags,
int32_t timeout_msec)
{
return omc_set_cmd(mc, QCMD(PROTOCOL_BINARY_CMD_REPLACE),
key, key_len, value, value_len,
expiration, flags, 0, timeout_msec);
}
int omcache_append(omcache_t *mc,
const unsigned char *key, size_t key_len,
const unsigned char *value, size_t value_len,
uint64_t cas, int32_t timeout_msec)
{
return omc_set_cmd(mc, QCMD(PROTOCOL_BINARY_CMD_APPEND),
key, key_len, value, value_len,
0, 0, cas, timeout_msec);
}
int omcache_prepend(omcache_t *mc,
const unsigned char *key, size_t key_len,
const unsigned char *value, size_t value_len,
uint64_t cas, int32_t timeout_msec)
{
return omc_set_cmd(mc, QCMD(PROTOCOL_BINARY_CMD_PREPEND),
key, key_len, value, value_len,
0, 0, cas, timeout_msec);
}
struct protocol_binary_delta_request_body_s {
uint64_t delta;
uint64_t initial;
uint32_t expiration;
} __attribute__((packed));
static int omc_ctr_cmd(omcache_t *mc, protocol_binary_command opcode,
const unsigned char *key, size_t key_len,
uint64_t delta, uint64_t initial,
time_t expiration, uint64_t *valuep,
int32_t timeout_msec)
{
struct protocol_binary_delta_request_body_s body = {
.delta = htobe64(delta),
.initial = htobe64(initial),
.expiration = htobe32(expiration),
};
omcache_req_t req[1] = {{
.server_index = -1,
.header = {
.opcode = opcode,
.extlen = sizeof(body),
.keylen = htobe16(key_len),
.bodylen = htobe32(key_len + sizeof(body)),
},
.extra = &body,
.key = key,
}};
omcache_value_t value = {0};
size_t req_count = 1, value_count = 1;
int ret = omcache_command(mc, req, &req_count, &value, &value_count, timeout_msec);
if (ret == OMCACHE_OK && value_count)
ret = value.status;
if (valuep)
*valuep = (ret == OMCACHE_OK) ? value.delta_value : 0;
return ret;
}
int omcache_increment(omcache_t *mc,
const unsigned char *key, size_t key_len,
uint64_t delta, uint64_t initial,
time_t expiration, uint64_t *value,
int32_t timeout_msec)
{
return omc_ctr_cmd(mc, QCMD(PROTOCOL_BINARY_CMD_INCREMENT),
key, key_len, delta, initial, expiration,
value, timeout_msec);
}
int omcache_decrement(omcache_t *mc,
const unsigned char *key, size_t key_len,
uint64_t delta, uint64_t initial,
time_t expiration, uint64_t *value,
int32_t timeout_msec)
{
return omc_ctr_cmd(mc, QCMD(PROTOCOL_BINARY_CMD_DECREMENT),
key, key_len, delta, initial, expiration,
value, timeout_msec);
}
int omcache_delete(omcache_t *mc,
const unsigned char *key, size_t key_len,
int32_t timeout_msec)
{
omcache_req_t req[1] = {{
.server_index = -1,
.header = {
.opcode = QCMD(PROTOCOL_BINARY_CMD_DELETE),
.keylen = htobe16(key_len),
.bodylen = htobe32(key_len),
},
.key = key,
}};
return omcache_command_status(mc, req, timeout_msec);
}
int omcache_touch(omcache_t *mc,
const unsigned char *key, size_t key_len,
time_t expiration, int32_t timeout_msec)
{
uint32_t body_exp = htobe32(expiration);
omcache_req_t req[1] = {{
.server_index = -1,
.header = {
.opcode = PROTOCOL_BINARY_CMD_TOUCH,
.extlen = sizeof(body_exp),
.keylen = htobe16(key_len),
.bodylen = htobe32(key_len + sizeof(body_exp)),
},
.extra = &body_exp,
.key = key,
}};
return omcache_command_status(mc, req, timeout_msec);
}
static int omc_get_multi_cmd(omcache_t *mc,
protocol_binary_command opcode,
const unsigned char **keys,
size_t *key_lens,
uint32_t *be_expirations,
size_t key_count,
omcache_req_t *requests,
size_t *req_count,
omcache_value_t *values,
size_t *value_count,
int32_t timeout_msec)
{
if (req_count == NULL || *req_count < key_count)
return OMCACHE_INVALID;
memset(requests, 0, sizeof(*requests) * *req_count);
if (values && value_count)
memset(values, 0, sizeof(*values) * *value_count);
for (size_t i = 0; i < key_count; i ++)
{
requests[i].server_index = -1;
requests[i].header.opcode = opcode;
requests[i].header.keylen = htobe16(key_lens[i]);
if (opcode == PROTOCOL_BINARY_CMD_GAT || opcode == PROTOCOL_BINARY_CMD_GATQ ||
opcode == PROTOCOL_BINARY_CMD_GATK || opcode == PROTOCOL_BINARY_CMD_GATKQ)
{
requests[i].header.extlen = sizeof(uint32_t);
requests[i].extra = &be_expirations[i];
}
requests[i].header.bodylen = htobe32(key_lens[i] + requests[i].header.extlen);
requests[i].key = keys[i];
}
return omcache_command(mc, requests, req_count, values, value_count, timeout_msec);
}
int omcache_get_multi(omcache_t *mc,
const unsigned char **keys,
size_t *key_lens,
size_t key_count,
omcache_req_t *requests,
size_t *req_count,
omcache_value_t *values,
size_t *value_count,
int32_t timeout_msec)
{
return omc_get_multi_cmd(mc, PROTOCOL_BINARY_CMD_GETKQ,
keys, key_lens, NULL, key_count,
requests, req_count, values, value_count,
timeout_msec);
}
int omcache_gat_multi(omcache_t *mc,
const unsigned char **keys,
size_t *key_lens,
time_t *expirations,
size_t key_count,
omcache_req_t *requests,
size_t *req_count,
omcache_value_t *values,
size_t *value_count,
int32_t timeout_msec)
{
uint32_t be_expirations[key_count];
for (size_t i = 0; i < key_count; i ++)
be_expirations[i] = htobe32(expirations[i]);
return omc_get_multi_cmd(mc, PROTOCOL_BINARY_CMD_GATKQ,
keys, key_lens, be_expirations, key_count,
requests, req_count, values, value_count,
timeout_msec);
}
static int omc_get_cmd(omcache_t *mc, protocol_binary_command opcode,
const unsigned char *key, size_t key_len,
const unsigned char **valuep, size_t *value_len,
uint32_t be_expiration, uint32_t *flags, uint64_t *cas,
int32_t timeout_msec)
{
omcache_req_t req;
omcache_value_t value = {0};
size_t req_count = 1, value_count = 1;
int ret = omc_get_multi_cmd(mc, opcode, &key, &key_len, &be_expiration, 1, &req, &req_count,
&value, &value_count, timeout_msec);
if (value_count)
ret = value.status;
if (value_count == 0 && ret == OMCACHE_OK)
ret = OMCACHE_NOT_FOUND;
else if (ret == OMCACHE_AGAIN && valuep == NULL)
ret = OMCACHE_OK;
if (valuep)
*valuep = value.data;
if (value_len)
*value_len = value.data_len;
if (flags)
*flags = value.flags;
if (cas)
*cas = value.cas;
return ret;
}
int omcache_get(omcache_t *mc,
const unsigned char *key, size_t key_len,
const unsigned char **value, size_t *value_len,
uint32_t *flags, uint64_t *cas,
int32_t timeout_msec)
{
return omc_get_cmd(mc, QCMD(PROTOCOL_BINARY_CMD_GETK), key, key_len,
value, value_len, 0, flags, cas, timeout_msec);
}
int omcache_gat(omcache_t *mc,
const unsigned char *key, size_t key_len,
const unsigned char **value, size_t *value_len,
time_t expiration,
uint32_t *flags, uint64_t *cas,
int32_t timeout_msec)
{
return omc_get_cmd(mc, QCMD(PROTOCOL_BINARY_CMD_GATK), key, key_len,
value, value_len, htobe32(expiration),
flags, cas, timeout_msec);
}