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