2dbcb28071b624786a2f9e1c585f731ae90d7a30
[m6w6/libmemcached] / libmemcached / memcached_storage.c
1 /*
2 Memcached library
3
4 memcached_set()
5 memcached_replace()
6 memcached_add()
7
8 */
9 #include "common.h"
10 #include "memcached_io.h"
11
12 typedef enum {
13 SET_OP,
14 REPLACE_OP,
15 ADD_OP,
16 PREPEND_OP,
17 APPEND_OP,
18 CAS_OP,
19 } memcached_storage_action_t;
20
21 /* Inline this */
22 static inline const char *storage_op_string(memcached_storage_action_t verb)
23 {
24 switch (verb)
25 {
26 case SET_OP:
27 return "set ";
28 case REPLACE_OP:
29 return "replace ";
30 case ADD_OP:
31 return "add ";
32 case PREPEND_OP:
33 return "prepend ";
34 case APPEND_OP:
35 return "append ";
36 case CAS_OP:
37 return "cas ";
38 default:
39 return "tosserror"; /* This is impossible, fixes issue for compiler warning in VisualStudio */
40 }
41
42 /* NOTREACHED */
43 }
44
45 static memcached_return_t memcached_send_binary(memcached_st *ptr,
46 const char *master_key,
47 size_t master_key_length,
48 const char *key,
49 size_t key_length,
50 const char *value,
51 size_t value_length,
52 time_t expiration,
53 uint32_t flags,
54 uint64_t cas,
55 memcached_storage_action_t verb);
56
57 static inline memcached_return_t memcached_send(memcached_st *ptr,
58 const char *master_key, size_t master_key_length,
59 const char *key, size_t key_length,
60 const char *value, size_t value_length,
61 time_t expiration,
62 uint32_t flags,
63 uint64_t cas,
64 memcached_storage_action_t verb)
65 {
66 char to_write;
67 size_t write_length;
68 ssize_t sent_length;
69 memcached_return_t rc;
70 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
71 unsigned int server_key;
72
73 WATCHPOINT_ASSERT(!(value == NULL && value_length > 0));
74
75 rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
76 unlikely (rc != MEMCACHED_SUCCESS)
77 return rc;
78
79 unlikely (ptr->number_of_hosts == 0)
80 return MEMCACHED_NO_SERVERS;
81
82 if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
83 return MEMCACHED_BAD_KEY_PROVIDED;
84
85 if (ptr->flags.binary_protocol)
86 {
87 return memcached_send_binary(ptr, master_key, master_key_length,
88 key, key_length,
89 value, value_length, expiration,
90 flags, cas, verb);
91 }
92
93 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
94
95 if (cas)
96 {
97 write_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
98 "%s %s%.*s %u %llu %zu %llu%s\r\n",
99 storage_op_string(verb),
100 ptr->prefix_key,
101 (int)key_length, key, flags,
102 (unsigned long long)expiration, value_length,
103 (unsigned long long)cas,
104 (ptr->flags.no_reply) ? " noreply" : "");
105 }
106 else
107 {
108 char *buffer_ptr= buffer;
109 const char *command= storage_op_string(verb);
110
111 /* Copy in the command, no space needed, we handle that in the command function*/
112 memcpy(buffer_ptr, command, strlen(command));
113
114 /* Copy in the key prefix, switch to the buffer_ptr */
115 buffer_ptr= memcpy(buffer_ptr + strlen(command) , ptr->prefix_key, strlen(ptr->prefix_key));
116
117 /* Copy in the key, adjust point if a key prefix was used. */
118 buffer_ptr= memcpy(buffer_ptr + (ptr->prefix_key ? strlen(ptr->prefix_key) : 0),
119 key, key_length);
120 buffer_ptr+= key_length;
121 buffer_ptr[0]= ' ';
122 buffer_ptr++;
123
124 write_length= (size_t)(buffer_ptr - buffer);
125 write_length+= (size_t) snprintf(buffer_ptr, MEMCACHED_DEFAULT_COMMAND_SIZE,
126 "%u %llu %zu%s\r\n",
127 flags,
128 (unsigned long long)expiration, value_length,
129 ptr->flags.no_reply ? " noreply" : "");
130 }
131
132 if (ptr->flags.use_udp && ptr->flags.buffer_requests)
133 {
134 size_t cmd_size= write_length + value_length + 2;
135 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
136 return MEMCACHED_WRITE_FAILURE;
137 if (cmd_size + ptr->hosts[server_key].write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
138 memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1);
139 }
140
141 if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
142 {
143 rc= MEMCACHED_WRITE_FAILURE;
144 goto error;
145 }
146
147 /* Send command header */
148 rc= memcached_do(&ptr->hosts[server_key], buffer, write_length, 0);
149 if (rc != MEMCACHED_SUCCESS)
150 goto error;
151
152 /* Send command body */
153 if ((sent_length= memcached_io_write(&ptr->hosts[server_key], value, value_length, 0)) == -1)
154 {
155 rc= MEMCACHED_WRITE_FAILURE;
156 goto error;
157 }
158
159 if (ptr->flags.buffer_requests && verb == SET_OP)
160 {
161 to_write= 0;
162 }
163 else
164 {
165 to_write= 1;
166 }
167
168 if ((sent_length= memcached_io_write(&ptr->hosts[server_key], "\r\n", 2, to_write)) == -1)
169 {
170 rc= MEMCACHED_WRITE_FAILURE;
171 goto error;
172 }
173
174 if (ptr->flags.no_reply)
175 return (to_write == 0) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS;
176
177 if (to_write == 0)
178 return MEMCACHED_BUFFERED;
179
180 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
181
182 if (rc == MEMCACHED_STORED)
183 return MEMCACHED_SUCCESS;
184 else
185 return rc;
186
187 error:
188 memcached_io_reset(&ptr->hosts[server_key]);
189
190 return rc;
191 }
192
193
194 memcached_return_t memcached_set(memcached_st *ptr, const char *key, size_t key_length,
195 const char *value, size_t value_length,
196 time_t expiration,
197 uint32_t flags)
198 {
199 memcached_return_t rc;
200 LIBMEMCACHED_MEMCACHED_SET_START();
201 rc= memcached_send(ptr, key, key_length,
202 key, key_length, value, value_length,
203 expiration, flags, 0, SET_OP);
204 LIBMEMCACHED_MEMCACHED_SET_END();
205 return rc;
206 }
207
208 memcached_return_t memcached_add(memcached_st *ptr,
209 const char *key, size_t key_length,
210 const char *value, size_t value_length,
211 time_t expiration,
212 uint32_t flags)
213 {
214 memcached_return_t rc;
215 LIBMEMCACHED_MEMCACHED_ADD_START();
216 rc= memcached_send(ptr, key, key_length,
217 key, key_length, value, value_length,
218 expiration, flags, 0, ADD_OP);
219 LIBMEMCACHED_MEMCACHED_ADD_END();
220 return rc;
221 }
222
223 memcached_return_t memcached_replace(memcached_st *ptr,
224 const char *key, size_t key_length,
225 const char *value, size_t value_length,
226 time_t expiration,
227 uint32_t flags)
228 {
229 memcached_return_t rc;
230 LIBMEMCACHED_MEMCACHED_REPLACE_START();
231 rc= memcached_send(ptr, key, key_length,
232 key, key_length, value, value_length,
233 expiration, flags, 0, REPLACE_OP);
234 LIBMEMCACHED_MEMCACHED_REPLACE_END();
235 return rc;
236 }
237
238 memcached_return_t memcached_prepend(memcached_st *ptr,
239 const char *key, size_t key_length,
240 const char *value, size_t value_length,
241 time_t expiration,
242 uint32_t flags)
243 {
244 memcached_return_t rc;
245 rc= memcached_send(ptr, key, key_length,
246 key, key_length, value, value_length,
247 expiration, flags, 0, PREPEND_OP);
248 return rc;
249 }
250
251 memcached_return_t memcached_append(memcached_st *ptr,
252 const char *key, size_t key_length,
253 const char *value, size_t value_length,
254 time_t expiration,
255 uint32_t flags)
256 {
257 memcached_return_t rc;
258 rc= memcached_send(ptr, key, key_length,
259 key, key_length, value, value_length,
260 expiration, flags, 0, APPEND_OP);
261 return rc;
262 }
263
264 memcached_return_t memcached_cas(memcached_st *ptr,
265 const char *key, size_t key_length,
266 const char *value, size_t value_length,
267 time_t expiration,
268 uint32_t flags,
269 uint64_t cas)
270 {
271 memcached_return_t rc;
272 rc= memcached_send(ptr, key, key_length,
273 key, key_length, value, value_length,
274 expiration, flags, cas, CAS_OP);
275 return rc;
276 }
277
278 memcached_return_t memcached_set_by_key(memcached_st *ptr,
279 const char *master_key __attribute__((unused)),
280 size_t master_key_length __attribute__((unused)),
281 const char *key, size_t key_length,
282 const char *value, size_t value_length,
283 time_t expiration,
284 uint32_t flags)
285 {
286 memcached_return_t rc;
287 LIBMEMCACHED_MEMCACHED_SET_START();
288 rc= memcached_send(ptr, master_key, master_key_length,
289 key, key_length, value, value_length,
290 expiration, flags, 0, SET_OP);
291 LIBMEMCACHED_MEMCACHED_SET_END();
292 return rc;
293 }
294
295 memcached_return_t memcached_add_by_key(memcached_st *ptr,
296 const char *master_key, size_t master_key_length,
297 const char *key, size_t key_length,
298 const char *value, size_t value_length,
299 time_t expiration,
300 uint32_t flags)
301 {
302 memcached_return_t rc;
303 LIBMEMCACHED_MEMCACHED_ADD_START();
304 rc= memcached_send(ptr, master_key, master_key_length,
305 key, key_length, value, value_length,
306 expiration, flags, 0, ADD_OP);
307 LIBMEMCACHED_MEMCACHED_ADD_END();
308 return rc;
309 }
310
311 memcached_return_t memcached_replace_by_key(memcached_st *ptr,
312 const char *master_key, size_t master_key_length,
313 const char *key, size_t key_length,
314 const char *value, size_t value_length,
315 time_t expiration,
316 uint32_t flags)
317 {
318 memcached_return_t rc;
319 LIBMEMCACHED_MEMCACHED_REPLACE_START();
320 rc= memcached_send(ptr, master_key, master_key_length,
321 key, key_length, value, value_length,
322 expiration, flags, 0, REPLACE_OP);
323 LIBMEMCACHED_MEMCACHED_REPLACE_END();
324 return rc;
325 }
326
327 memcached_return_t memcached_prepend_by_key(memcached_st *ptr,
328 const char *master_key, size_t master_key_length,
329 const char *key, size_t key_length,
330 const char *value, size_t value_length,
331 time_t expiration,
332 uint32_t flags)
333 {
334 memcached_return_t rc;
335 rc= memcached_send(ptr, master_key, master_key_length,
336 key, key_length, value, value_length,
337 expiration, flags, 0, PREPEND_OP);
338 return rc;
339 }
340
341 memcached_return_t memcached_append_by_key(memcached_st *ptr,
342 const char *master_key, size_t master_key_length,
343 const char *key, size_t key_length,
344 const char *value, size_t value_length,
345 time_t expiration,
346 uint32_t flags)
347 {
348 memcached_return_t rc;
349 rc= memcached_send(ptr, master_key, master_key_length,
350 key, key_length, value, value_length,
351 expiration, flags, 0, APPEND_OP);
352 return rc;
353 }
354
355 memcached_return_t memcached_cas_by_key(memcached_st *ptr,
356 const char *master_key, size_t master_key_length,
357 const char *key, size_t key_length,
358 const char *value, size_t value_length,
359 time_t expiration,
360 uint32_t flags,
361 uint64_t cas)
362 {
363 memcached_return_t rc;
364 rc= memcached_send(ptr, master_key, master_key_length,
365 key, key_length, value, value_length,
366 expiration, flags, cas, CAS_OP);
367 return rc;
368 }
369
370 static inline uint8_t get_com_code(memcached_storage_action_t verb, bool noreply)
371 {
372 /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
373 * be used uninitialized in this function. FAIL */
374 uint8_t ret= 0;
375
376 if (noreply)
377 switch (verb)
378 {
379 case SET_OP:
380 ret=PROTOCOL_BINARY_CMD_SETQ;
381 break;
382 case ADD_OP:
383 ret=PROTOCOL_BINARY_CMD_ADDQ;
384 break;
385 case CAS_OP: /* FALLTHROUGH */
386 case REPLACE_OP:
387 ret=PROTOCOL_BINARY_CMD_REPLACEQ;
388 break;
389 case APPEND_OP:
390 ret=PROTOCOL_BINARY_CMD_APPENDQ;
391 break;
392 case PREPEND_OP:
393 ret=PROTOCOL_BINARY_CMD_PREPENDQ;
394 break;
395 default:
396 WATCHPOINT_ASSERT(verb);
397 break;
398 }
399 else
400 switch (verb)
401 {
402 case SET_OP:
403 ret=PROTOCOL_BINARY_CMD_SET;
404 break;
405 case ADD_OP:
406 ret=PROTOCOL_BINARY_CMD_ADD;
407 break;
408 case CAS_OP: /* FALLTHROUGH */
409 case REPLACE_OP:
410 ret=PROTOCOL_BINARY_CMD_REPLACE;
411 break;
412 case APPEND_OP:
413 ret=PROTOCOL_BINARY_CMD_APPEND;
414 break;
415 case PREPEND_OP:
416 ret=PROTOCOL_BINARY_CMD_PREPEND;
417 break;
418 default:
419 WATCHPOINT_ASSERT(verb);
420 break;
421 }
422
423 return ret;
424 }
425
426
427
428 static memcached_return_t memcached_send_binary(memcached_st *ptr,
429 const char *master_key,
430 size_t master_key_length,
431 const char *key,
432 size_t key_length,
433 const char *value,
434 size_t value_length,
435 time_t expiration,
436 uint32_t flags,
437 uint64_t cas,
438 memcached_storage_action_t verb)
439 {
440 uint8_t flush;
441 protocol_binary_request_set request= {.bytes= {0}};
442 size_t send_length= sizeof(request.bytes);
443 uint32_t server_key= memcached_generate_hash(ptr, master_key,
444 master_key_length);
445 memcached_server_st *server= &ptr->hosts[server_key];
446 bool noreply= server->root->flags.no_reply;
447
448 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
449 request.message.header.request.opcode= get_com_code(verb, noreply);
450 request.message.header.request.keylen= htons((uint16_t)key_length);
451 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
452 if (verb == APPEND_OP || verb == PREPEND_OP)
453 send_length -= 8; /* append & prepend does not contain extras! */
454 else
455 {
456 request.message.header.request.extlen= 8;
457 request.message.body.flags= htonl(flags);
458 request.message.body.expiration= htonl((uint32_t)expiration);
459 }
460
461 request.message.header.request.bodylen= htonl((uint32_t) (key_length + value_length +
462 request.message.header.request.extlen));
463
464 if (cas)
465 request.message.header.request.cas= htonll(cas);
466
467 flush= (uint8_t) ((server->root->flags.buffer_requests && verb == SET_OP) ? 0 : 1);
468
469 if (server->root->flags.use_udp && !flush)
470 {
471 size_t cmd_size= send_length + key_length + value_length;
472
473 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
474 return MEMCACHED_WRITE_FAILURE;
475 if (cmd_size + server->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
476 memcached_io_write(server,NULL,0, 1);
477 }
478
479 /* write the header */
480 if ((memcached_do(server, (const char*)request.bytes, send_length, 0) != MEMCACHED_SUCCESS) ||
481 (memcached_io_write(server, key, key_length, 0) == -1) ||
482 (memcached_io_write(server, value, value_length, (char) flush) == -1))
483 {
484 memcached_io_reset(server);
485 return MEMCACHED_WRITE_FAILURE;
486 }
487
488 unlikely (verb == SET_OP && ptr->number_of_replicas > 0)
489 {
490 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
491
492 for (uint32_t x= 0; x < ptr->number_of_replicas; x++)
493 {
494 ++server_key;
495 if (server_key == ptr->number_of_hosts)
496 server_key= 0;
497
498 memcached_server_st *srv= &ptr->hosts[server_key];
499 if ((memcached_do(srv, (const char*)request.bytes,
500 send_length, 0) != MEMCACHED_SUCCESS) ||
501 (memcached_io_write(srv, key, key_length, 0) == -1) ||
502 (memcached_io_write(srv, value, value_length, (char) flush) == -1))
503 memcached_io_reset(srv);
504 else
505 memcached_server_response_decrement(srv);
506 }
507 }
508
509 if (flush == 0)
510 return MEMCACHED_BUFFERED;
511
512 if (noreply)
513 return MEMCACHED_SUCCESS;
514
515 return memcached_response(server, NULL, 0, NULL);
516 }
517