@$#$@$ gcc
[m6w6/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
152 struct __write_vector_st vector[]=
153 {
154 { .length= write_length, .buffer= buffer },
155 { .length= value_length, .buffer= value },
156 { .length= 2, .buffer= "\r\n" }
157 };
158
159 if (ptr->flags.buffer_requests && verb == SET_OP)
160 {
161 to_write= false;
162 }
163 else
164 {
165 to_write= true;
166 }
167
168 /* Send command header */
169 rc= memcached_vdo(instance, vector, 3, to_write);
170 if (rc == MEMCACHED_SUCCESS)
171 {
172
173 if (ptr->flags.no_reply)
174 return (to_write == false) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS;
175
176 if (to_write == false)
177 return MEMCACHED_BUFFERED;
178
179 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
180
181 if (rc == MEMCACHED_STORED)
182 return MEMCACHED_SUCCESS;
183 else
184 return rc;
185 }
186
187 error:
188
189 memcached_io_reset(instance);
190
191 return rc;
192 }
193
194
195 memcached_return_t memcached_set(memcached_st *ptr, const char *key, size_t key_length,
196 const char *value, size_t value_length,
197 time_t expiration,
198 uint32_t flags)
199 {
200 memcached_return_t rc;
201 LIBMEMCACHED_MEMCACHED_SET_START();
202 rc= memcached_send(ptr, key, key_length,
203 key, key_length, value, value_length,
204 expiration, flags, 0, SET_OP);
205 LIBMEMCACHED_MEMCACHED_SET_END();
206 return rc;
207 }
208
209 memcached_return_t memcached_add(memcached_st *ptr,
210 const char *key, size_t key_length,
211 const char *value, size_t value_length,
212 time_t expiration,
213 uint32_t flags)
214 {
215 memcached_return_t rc;
216 LIBMEMCACHED_MEMCACHED_ADD_START();
217 rc= memcached_send(ptr, key, key_length,
218 key, key_length, value, value_length,
219 expiration, flags, 0, ADD_OP);
220 LIBMEMCACHED_MEMCACHED_ADD_END();
221 return rc;
222 }
223
224 memcached_return_t memcached_replace(memcached_st *ptr,
225 const char *key, size_t key_length,
226 const char *value, size_t value_length,
227 time_t expiration,
228 uint32_t flags)
229 {
230 memcached_return_t rc;
231 LIBMEMCACHED_MEMCACHED_REPLACE_START();
232 rc= memcached_send(ptr, key, key_length,
233 key, key_length, value, value_length,
234 expiration, flags, 0, REPLACE_OP);
235 LIBMEMCACHED_MEMCACHED_REPLACE_END();
236 return rc;
237 }
238
239 memcached_return_t memcached_prepend(memcached_st *ptr,
240 const char *key, size_t key_length,
241 const char *value, size_t value_length,
242 time_t expiration,
243 uint32_t flags)
244 {
245 memcached_return_t rc;
246 rc= memcached_send(ptr, key, key_length,
247 key, key_length, value, value_length,
248 expiration, flags, 0, PREPEND_OP);
249 return rc;
250 }
251
252 memcached_return_t memcached_append(memcached_st *ptr,
253 const char *key, size_t key_length,
254 const char *value, size_t value_length,
255 time_t expiration,
256 uint32_t flags)
257 {
258 memcached_return_t rc;
259 rc= memcached_send(ptr, key, key_length,
260 key, key_length, value, value_length,
261 expiration, flags, 0, APPEND_OP);
262 return rc;
263 }
264
265 memcached_return_t memcached_cas(memcached_st *ptr,
266 const char *key, size_t key_length,
267 const char *value, size_t value_length,
268 time_t expiration,
269 uint32_t flags,
270 uint64_t cas)
271 {
272 memcached_return_t rc;
273 rc= memcached_send(ptr, key, key_length,
274 key, key_length, value, value_length,
275 expiration, flags, cas, CAS_OP);
276 return rc;
277 }
278
279 memcached_return_t memcached_set_by_key(memcached_st *ptr,
280 const char *master_key __attribute__((unused)),
281 size_t master_key_length __attribute__((unused)),
282 const char *key, size_t key_length,
283 const char *value, size_t value_length,
284 time_t expiration,
285 uint32_t flags)
286 {
287 memcached_return_t rc;
288 LIBMEMCACHED_MEMCACHED_SET_START();
289 rc= memcached_send(ptr, master_key, master_key_length,
290 key, key_length, value, value_length,
291 expiration, flags, 0, SET_OP);
292 LIBMEMCACHED_MEMCACHED_SET_END();
293 return rc;
294 }
295
296 memcached_return_t memcached_add_by_key(memcached_st *ptr,
297 const char *master_key, size_t master_key_length,
298 const char *key, size_t key_length,
299 const char *value, size_t value_length,
300 time_t expiration,
301 uint32_t flags)
302 {
303 memcached_return_t rc;
304 LIBMEMCACHED_MEMCACHED_ADD_START();
305 rc= memcached_send(ptr, master_key, master_key_length,
306 key, key_length, value, value_length,
307 expiration, flags, 0, ADD_OP);
308 LIBMEMCACHED_MEMCACHED_ADD_END();
309 return rc;
310 }
311
312 memcached_return_t memcached_replace_by_key(memcached_st *ptr,
313 const char *master_key, size_t master_key_length,
314 const char *key, size_t key_length,
315 const char *value, size_t value_length,
316 time_t expiration,
317 uint32_t flags)
318 {
319 memcached_return_t rc;
320 LIBMEMCACHED_MEMCACHED_REPLACE_START();
321 rc= memcached_send(ptr, master_key, master_key_length,
322 key, key_length, value, value_length,
323 expiration, flags, 0, REPLACE_OP);
324 LIBMEMCACHED_MEMCACHED_REPLACE_END();
325 return rc;
326 }
327
328 memcached_return_t memcached_prepend_by_key(memcached_st *ptr,
329 const char *master_key, size_t master_key_length,
330 const char *key, size_t key_length,
331 const char *value, size_t value_length,
332 time_t expiration,
333 uint32_t flags)
334 {
335 memcached_return_t rc;
336 rc= memcached_send(ptr, master_key, master_key_length,
337 key, key_length, value, value_length,
338 expiration, flags, 0, PREPEND_OP);
339 return rc;
340 }
341
342 memcached_return_t memcached_append_by_key(memcached_st *ptr,
343 const char *master_key, size_t master_key_length,
344 const char *key, size_t key_length,
345 const char *value, size_t value_length,
346 time_t expiration,
347 uint32_t flags)
348 {
349 memcached_return_t rc;
350 rc= memcached_send(ptr, master_key, master_key_length,
351 key, key_length, value, value_length,
352 expiration, flags, 0, APPEND_OP);
353 return rc;
354 }
355
356 memcached_return_t memcached_cas_by_key(memcached_st *ptr,
357 const char *master_key, size_t master_key_length,
358 const char *key, size_t key_length,
359 const char *value, size_t value_length,
360 time_t expiration,
361 uint32_t flags,
362 uint64_t cas)
363 {
364 memcached_return_t rc;
365 rc= memcached_send(ptr, master_key, master_key_length,
366 key, key_length, value, value_length,
367 expiration, flags, cas, CAS_OP);
368 return rc;
369 }
370
371 static inline uint8_t get_com_code(memcached_storage_action_t verb, bool noreply)
372 {
373 /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
374 * be used uninitialized in this function. FAIL */
375 uint8_t ret= 0;
376
377 if (noreply)
378 switch (verb)
379 {
380 case SET_OP:
381 ret=PROTOCOL_BINARY_CMD_SETQ;
382 break;
383 case ADD_OP:
384 ret=PROTOCOL_BINARY_CMD_ADDQ;
385 break;
386 case CAS_OP: /* FALLTHROUGH */
387 case REPLACE_OP:
388 ret=PROTOCOL_BINARY_CMD_REPLACEQ;
389 break;
390 case APPEND_OP:
391 ret=PROTOCOL_BINARY_CMD_APPENDQ;
392 break;
393 case PREPEND_OP:
394 ret=PROTOCOL_BINARY_CMD_PREPENDQ;
395 break;
396 default:
397 WATCHPOINT_ASSERT(verb);
398 break;
399 }
400 else
401 switch (verb)
402 {
403 case SET_OP:
404 ret=PROTOCOL_BINARY_CMD_SET;
405 break;
406 case ADD_OP:
407 ret=PROTOCOL_BINARY_CMD_ADD;
408 break;
409 case CAS_OP: /* FALLTHROUGH */
410 case REPLACE_OP:
411 ret=PROTOCOL_BINARY_CMD_REPLACE;
412 break;
413 case APPEND_OP:
414 ret=PROTOCOL_BINARY_CMD_APPEND;
415 break;
416 case PREPEND_OP:
417 ret=PROTOCOL_BINARY_CMD_PREPEND;
418 break;
419 default:
420 WATCHPOINT_ASSERT(verb);
421 break;
422 }
423
424 return ret;
425 }
426
427
428
429 static memcached_return_t memcached_send_binary(memcached_st *ptr,
430 const char *master_key,
431 size_t master_key_length,
432 const char *key,
433 size_t key_length,
434 const char *value,
435 size_t value_length,
436 time_t expiration,
437 uint32_t flags,
438 uint64_t cas,
439 memcached_storage_action_t verb)
440 {
441 bool flush;
442 protocol_binary_request_set request= {.bytes= {0}};
443 size_t send_length= sizeof(request.bytes);
444 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, master_key,
445 master_key_length);
446
447 memcached_server_write_instance_st server=
448 memcached_server_instance_fetch(ptr, server_key);
449
450 bool noreply= server->root->flags.no_reply;
451
452 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
453 request.message.header.request.opcode= get_com_code(verb, noreply);
454 request.message.header.request.keylen= htons((uint16_t)(key_length + ptr->prefix_key_length));
455 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
456 if (verb == APPEND_OP || verb == PREPEND_OP)
457 send_length -= 8; /* append & prepend does not contain extras! */
458 else
459 {
460 request.message.header.request.extlen= 8;
461 request.message.body.flags= htonl(flags);
462 request.message.body.expiration= htonl((uint32_t)expiration);
463 }
464
465 request.message.header.request.bodylen= htonl((uint32_t) (key_length + ptr->prefix_key_length + value_length +
466 request.message.header.request.extlen));
467
468 if (cas)
469 request.message.header.request.cas= htonll(cas);
470
471 flush= (bool) ((server->root->flags.buffer_requests && verb == SET_OP) ? 0 : 1);
472
473 if (server->root->flags.use_udp && ! flush)
474 {
475 size_t cmd_size= send_length + key_length + value_length;
476
477 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
478 {
479 return MEMCACHED_WRITE_FAILURE;
480 }
481 if (cmd_size + server->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
482 {
483 memcached_io_write(server, NULL, 0, true);
484 }
485 }
486
487 struct __write_vector_st vector[]=
488 {
489 { .length= send_length, .buffer= request.bytes },
490 { .length= ptr->prefix_key_length, .buffer= ptr->prefix_key },
491 { .length= key_length, .buffer= key },
492 { .length= value_length, .buffer= value }
493 };
494
495 /* write the header */
496 memcached_return_t rc;
497 if ((rc= memcached_vdo(server, vector, 4, flush)) != MEMCACHED_SUCCESS)
498 {
499 memcached_io_reset(server);
500 return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
501 }
502
503 unlikely (verb == SET_OP && ptr->number_of_replicas > 0)
504 {
505 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
506
507 for (uint32_t x= 0; x < ptr->number_of_replicas; x++)
508 {
509 memcached_server_write_instance_st instance;
510
511 ++server_key;
512 if (server_key == memcached_server_count(ptr))
513 server_key= 0;
514
515 instance= memcached_server_instance_fetch(ptr, server_key);
516
517 if (memcached_vdo(instance, vector, 4, false) != MEMCACHED_SUCCESS)
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