be0040a4197f6b621f0a900e662c92117ea8b89b
[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 }
149 else
150 {
151 struct __write_vector_st vector[]=
152 {
153 { .length= write_length, .buffer= buffer },
154 { .length= value_length, .buffer= value },
155 { .length= 2, .buffer= "\r\n" }
156 };
157
158 if (ptr->flags.buffer_requests && verb == SET_OP)
159 {
160 to_write= false;
161 }
162 else
163 {
164 to_write= true;
165 }
166
167 /* Send command header */
168 rc= memcached_vdo(instance, vector, 3, to_write);
169 if (rc == MEMCACHED_SUCCESS)
170 {
171
172 if (ptr->flags.no_reply)
173 return (to_write == false) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS;
174
175 if (to_write == false)
176 return MEMCACHED_BUFFERED;
177
178 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
179
180 if (rc == MEMCACHED_STORED)
181 return MEMCACHED_SUCCESS;
182 else
183 return rc;
184 }
185 }
186
187 if (rc == MEMCACHED_WRITE_FAILURE)
188 memcached_io_reset(instance);
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 bool 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_with_redistribution(ptr, master_key,
444 master_key_length);
445
446 memcached_server_write_instance_st server=
447 memcached_server_instance_fetch(ptr, server_key);
448
449 bool noreply= server->root->flags.no_reply;
450
451 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
452 request.message.header.request.opcode= get_com_code(verb, noreply);
453 request.message.header.request.keylen= htons((uint16_t)(key_length + ptr->prefix_key_length));
454 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
455 if (verb == APPEND_OP || verb == PREPEND_OP)
456 send_length -= 8; /* append & prepend does not contain extras! */
457 else
458 {
459 request.message.header.request.extlen= 8;
460 request.message.body.flags= htonl(flags);
461 request.message.body.expiration= htonl((uint32_t)expiration);
462 }
463
464 request.message.header.request.bodylen= htonl((uint32_t) (key_length + ptr->prefix_key_length + value_length +
465 request.message.header.request.extlen));
466
467 if (cas)
468 request.message.header.request.cas= htonll(cas);
469
470 flush= (bool) ((server->root->flags.buffer_requests && verb == SET_OP) ? 0 : 1);
471
472 if (server->root->flags.use_udp && ! flush)
473 {
474 size_t cmd_size= send_length + key_length + value_length;
475
476 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
477 {
478 return MEMCACHED_WRITE_FAILURE;
479 }
480 if (cmd_size + server->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
481 {
482 memcached_io_write(server, NULL, 0, true);
483 }
484 }
485
486 struct __write_vector_st vector[]=
487 {
488 { .length= send_length, .buffer= request.bytes },
489 { .length= ptr->prefix_key_length, .buffer= ptr->prefix_key },
490 { .length= key_length, .buffer= key },
491 { .length= value_length, .buffer= value }
492 };
493
494 /* write the header */
495 memcached_return_t rc;
496 if ((rc= memcached_vdo(server, vector, 4, flush)) != MEMCACHED_SUCCESS)
497 {
498 memcached_io_reset(server);
499 return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
500 }
501
502 unlikely (verb == SET_OP && ptr->number_of_replicas > 0)
503 {
504 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
505
506 for (uint32_t x= 0; x < ptr->number_of_replicas; x++)
507 {
508 memcached_server_write_instance_st instance;
509
510 ++server_key;
511 if (server_key == memcached_server_count(ptr))
512 server_key= 0;
513
514 instance= memcached_server_instance_fetch(ptr, server_key);
515
516 if (memcached_vdo(instance, vector, 4, false) != MEMCACHED_SUCCESS)
517 {
518 memcached_io_reset(instance);
519 }
520 else
521 {
522 memcached_server_response_decrement(instance);
523 }
524 }
525 }
526
527 if (flush == false)
528 {
529 return MEMCACHED_BUFFERED;
530 }
531
532 if (noreply)
533 {
534 return MEMCACHED_SUCCESS;
535 }
536
537 return memcached_response(server, NULL, 0, NULL);
538 }
539