Fixed autoconf macros for cross compile
[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 memcached_server_write_instance_st server,
49 uint32_t server_key,
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 server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
88 instance= memcached_server_instance_fetch(ptr, server_key);
89
90 WATCHPOINT_SET(instance->io_wait_count.read= 0);
91 WATCHPOINT_SET(instance->io_wait_count.write= 0);
92
93 if (ptr->flags.binary_protocol)
94 {
95 rc= memcached_send_binary(ptr, instance, server_key,
96 key, key_length,
97 value, value_length, expiration,
98 flags, cas, verb);
99 WATCHPOINT_IF_LABELED_NUMBER(instance->io_wait_count.read > 2, "read IO_WAIT", instance->io_wait_count.read);
100 WATCHPOINT_IF_LABELED_NUMBER(instance->io_wait_count.write > 2, "write_IO_WAIT", instance->io_wait_count.write);
101 }
102 else
103 {
104
105 if (cas)
106 {
107 write_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
108 "%s %.*s%.*s %u %llu %zu %llu%s\r\n",
109 storage_op_string(verb),
110 (int)ptr->prefix_key_length,
111 ptr->prefix_key,
112 (int)key_length, key, flags,
113 (unsigned long long)expiration, value_length,
114 (unsigned long long)cas,
115 (ptr->flags.no_reply) ? " noreply" : "");
116 }
117 else
118 {
119 char *buffer_ptr= buffer;
120 const char *command= storage_op_string(verb);
121
122 /* Copy in the command, no space needed, we handle that in the command function*/
123 memcpy(buffer_ptr, command, strlen(command));
124
125 /* Copy in the key prefix, switch to the buffer_ptr */
126 buffer_ptr= memcpy((buffer_ptr + strlen(command)), ptr->prefix_key, ptr->prefix_key_length);
127
128 /* Copy in the key, adjust point if a key prefix was used. */
129 buffer_ptr= memcpy(buffer_ptr + (ptr->prefix_key_length ? ptr->prefix_key_length : 0),
130 key, key_length);
131 buffer_ptr+= key_length;
132 buffer_ptr[0]= ' ';
133 buffer_ptr++;
134
135 write_length= (size_t)(buffer_ptr - buffer);
136 write_length+= (size_t) snprintf(buffer_ptr, MEMCACHED_DEFAULT_COMMAND_SIZE,
137 "%u %llu %zu%s\r\n",
138 flags,
139 (unsigned long long)expiration, value_length,
140 ptr->flags.no_reply ? " noreply" : "");
141 }
142
143 if (ptr->flags.use_udp && ptr->flags.buffer_requests)
144 {
145 size_t cmd_size= write_length + value_length + 2;
146 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
147 return MEMCACHED_WRITE_FAILURE;
148 if (cmd_size + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
149 memcached_io_write(instance, NULL, 0, true);
150 }
151
152 if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
153 {
154 rc= MEMCACHED_WRITE_FAILURE;
155 }
156 else
157 {
158 struct __write_vector_st vector[]=
159 {
160 { .length= write_length, .buffer= buffer },
161 { .length= value_length, .buffer= value },
162 { .length= 2, .buffer= "\r\n" }
163 };
164
165 if (ptr->flags.buffer_requests && verb == SET_OP)
166 {
167 to_write= false;
168 }
169 else
170 {
171 to_write= true;
172 }
173
174 /* Send command header */
175 rc= memcached_vdo(instance, vector, 3, to_write);
176 if (rc == MEMCACHED_SUCCESS)
177 {
178
179 if (ptr->flags.no_reply)
180 {
181 rc= (to_write == false) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS;
182 }
183 else if (to_write == false)
184 {
185 rc= MEMCACHED_BUFFERED;
186 }
187 else
188 {
189 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
190
191 if (rc == MEMCACHED_STORED)
192 rc= MEMCACHED_SUCCESS;
193 }
194 }
195 }
196
197 if (rc == MEMCACHED_WRITE_FAILURE)
198 memcached_io_reset(instance);
199 }
200
201 WATCHPOINT_IF_LABELED_NUMBER(instance->io_wait_count.read > 2, "read IO_WAIT", instance->io_wait_count.read);
202 WATCHPOINT_IF_LABELED_NUMBER(instance->io_wait_count.write > 2, "write_IO_WAIT", instance->io_wait_count.write);
203
204 return rc;
205 }
206
207
208 memcached_return_t memcached_set(memcached_st *ptr, const char *key, size_t key_length,
209 const char *value, size_t value_length,
210 time_t expiration,
211 uint32_t flags)
212 {
213 memcached_return_t rc;
214 LIBMEMCACHED_MEMCACHED_SET_START();
215 rc= memcached_send(ptr, key, key_length,
216 key, key_length, value, value_length,
217 expiration, flags, 0, SET_OP);
218 LIBMEMCACHED_MEMCACHED_SET_END();
219 return rc;
220 }
221
222 memcached_return_t memcached_add(memcached_st *ptr,
223 const char *key, size_t key_length,
224 const char *value, size_t value_length,
225 time_t expiration,
226 uint32_t flags)
227 {
228 memcached_return_t rc;
229 LIBMEMCACHED_MEMCACHED_ADD_START();
230 rc= memcached_send(ptr, key, key_length,
231 key, key_length, value, value_length,
232 expiration, flags, 0, ADD_OP);
233 LIBMEMCACHED_MEMCACHED_ADD_END();
234 return rc;
235 }
236
237 memcached_return_t memcached_replace(memcached_st *ptr,
238 const char *key, size_t key_length,
239 const char *value, size_t value_length,
240 time_t expiration,
241 uint32_t flags)
242 {
243 memcached_return_t rc;
244 LIBMEMCACHED_MEMCACHED_REPLACE_START();
245 rc= memcached_send(ptr, key, key_length,
246 key, key_length, value, value_length,
247 expiration, flags, 0, REPLACE_OP);
248 LIBMEMCACHED_MEMCACHED_REPLACE_END();
249 return rc;
250 }
251
252 memcached_return_t memcached_prepend(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, PREPEND_OP);
262 return rc;
263 }
264
265 memcached_return_t memcached_append(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 {
271 memcached_return_t rc;
272 rc= memcached_send(ptr, key, key_length,
273 key, key_length, value, value_length,
274 expiration, flags, 0, APPEND_OP);
275 return rc;
276 }
277
278 memcached_return_t memcached_cas(memcached_st *ptr,
279 const char *key, size_t key_length,
280 const char *value, size_t value_length,
281 time_t expiration,
282 uint32_t flags,
283 uint64_t cas)
284 {
285 memcached_return_t rc;
286 rc= memcached_send(ptr, key, key_length,
287 key, key_length, value, value_length,
288 expiration, flags, cas, CAS_OP);
289 return rc;
290 }
291
292 memcached_return_t memcached_set_by_key(memcached_st *ptr,
293 const char *master_key __attribute__((unused)),
294 size_t master_key_length __attribute__((unused)),
295 const char *key, size_t key_length,
296 const char *value, size_t value_length,
297 time_t expiration,
298 uint32_t flags)
299 {
300 memcached_return_t rc;
301 LIBMEMCACHED_MEMCACHED_SET_START();
302 rc= memcached_send(ptr, master_key, master_key_length,
303 key, key_length, value, value_length,
304 expiration, flags, 0, SET_OP);
305 LIBMEMCACHED_MEMCACHED_SET_END();
306 return rc;
307 }
308
309 memcached_return_t memcached_add_by_key(memcached_st *ptr,
310 const char *master_key, size_t master_key_length,
311 const char *key, size_t key_length,
312 const char *value, size_t value_length,
313 time_t expiration,
314 uint32_t flags)
315 {
316 memcached_return_t rc;
317 LIBMEMCACHED_MEMCACHED_ADD_START();
318 rc= memcached_send(ptr, master_key, master_key_length,
319 key, key_length, value, value_length,
320 expiration, flags, 0, ADD_OP);
321 LIBMEMCACHED_MEMCACHED_ADD_END();
322 return rc;
323 }
324
325 memcached_return_t memcached_replace_by_key(memcached_st *ptr,
326 const char *master_key, size_t master_key_length,
327 const char *key, size_t key_length,
328 const char *value, size_t value_length,
329 time_t expiration,
330 uint32_t flags)
331 {
332 memcached_return_t rc;
333 LIBMEMCACHED_MEMCACHED_REPLACE_START();
334 rc= memcached_send(ptr, master_key, master_key_length,
335 key, key_length, value, value_length,
336 expiration, flags, 0, REPLACE_OP);
337 LIBMEMCACHED_MEMCACHED_REPLACE_END();
338 return rc;
339 }
340
341 memcached_return_t memcached_prepend_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, PREPEND_OP);
352 return rc;
353 }
354
355 memcached_return_t memcached_append_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 {
362 memcached_return_t rc;
363 rc= memcached_send(ptr, master_key, master_key_length,
364 key, key_length, value, value_length,
365 expiration, flags, 0, APPEND_OP);
366 return rc;
367 }
368
369 memcached_return_t memcached_cas_by_key(memcached_st *ptr,
370 const char *master_key, size_t master_key_length,
371 const char *key, size_t key_length,
372 const char *value, size_t value_length,
373 time_t expiration,
374 uint32_t flags,
375 uint64_t cas)
376 {
377 memcached_return_t rc;
378 rc= memcached_send(ptr, master_key, master_key_length,
379 key, key_length, value, value_length,
380 expiration, flags, cas, CAS_OP);
381 return rc;
382 }
383
384 static inline uint8_t get_com_code(memcached_storage_action_t verb, bool noreply)
385 {
386 /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
387 * be used uninitialized in this function. FAIL */
388 uint8_t ret= 0;
389
390 if (noreply)
391 switch (verb)
392 {
393 case SET_OP:
394 ret=PROTOCOL_BINARY_CMD_SETQ;
395 break;
396 case ADD_OP:
397 ret=PROTOCOL_BINARY_CMD_ADDQ;
398 break;
399 case CAS_OP: /* FALLTHROUGH */
400 case REPLACE_OP:
401 ret=PROTOCOL_BINARY_CMD_REPLACEQ;
402 break;
403 case APPEND_OP:
404 ret=PROTOCOL_BINARY_CMD_APPENDQ;
405 break;
406 case PREPEND_OP:
407 ret=PROTOCOL_BINARY_CMD_PREPENDQ;
408 break;
409 default:
410 WATCHPOINT_ASSERT(verb);
411 break;
412 }
413 else
414 switch (verb)
415 {
416 case SET_OP:
417 ret=PROTOCOL_BINARY_CMD_SET;
418 break;
419 case ADD_OP:
420 ret=PROTOCOL_BINARY_CMD_ADD;
421 break;
422 case CAS_OP: /* FALLTHROUGH */
423 case REPLACE_OP:
424 ret=PROTOCOL_BINARY_CMD_REPLACE;
425 break;
426 case APPEND_OP:
427 ret=PROTOCOL_BINARY_CMD_APPEND;
428 break;
429 case PREPEND_OP:
430 ret=PROTOCOL_BINARY_CMD_PREPEND;
431 break;
432 default:
433 WATCHPOINT_ASSERT(verb);
434 break;
435 }
436
437 return ret;
438 }
439
440
441
442 static memcached_return_t memcached_send_binary(memcached_st *ptr,
443 memcached_server_write_instance_st server,
444 uint32_t server_key,
445 const char *key,
446 size_t key_length,
447 const char *value,
448 size_t value_length,
449 time_t expiration,
450 uint32_t flags,
451 uint64_t cas,
452 memcached_storage_action_t verb)
453 {
454 bool flush;
455 protocol_binary_request_set request= {.bytes= {0}};
456 size_t send_length= sizeof(request.bytes);
457
458 bool noreply= server->root->flags.no_reply;
459
460 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
461 request.message.header.request.opcode= get_com_code(verb, noreply);
462 request.message.header.request.keylen= htons((uint16_t)(key_length + ptr->prefix_key_length));
463 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
464 if (verb == APPEND_OP || verb == PREPEND_OP)
465 send_length -= 8; /* append & prepend does not contain extras! */
466 else
467 {
468 request.message.header.request.extlen= 8;
469 request.message.body.flags= htonl(flags);
470 request.message.body.expiration= htonl((uint32_t)expiration);
471 }
472
473 request.message.header.request.bodylen= htonl((uint32_t) (key_length + ptr->prefix_key_length + value_length +
474 request.message.header.request.extlen));
475
476 if (cas)
477 request.message.header.request.cas= htonll(cas);
478
479 flush= (bool) ((server->root->flags.buffer_requests && verb == SET_OP) ? 0 : 1);
480
481 if (server->root->flags.use_udp && ! flush)
482 {
483 size_t cmd_size= send_length + key_length + value_length;
484
485 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
486 {
487 return MEMCACHED_WRITE_FAILURE;
488 }
489 if (cmd_size + server->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
490 {
491 memcached_io_write(server, NULL, 0, true);
492 }
493 }
494
495 struct __write_vector_st vector[]=
496 {
497 { .length= send_length, .buffer= request.bytes },
498 { .length= ptr->prefix_key_length, .buffer= ptr->prefix_key },
499 { .length= key_length, .buffer= key },
500 { .length= value_length, .buffer= value }
501 };
502
503 /* write the header */
504 memcached_return_t rc;
505 if ((rc= memcached_vdo(server, vector, 4, flush)) != MEMCACHED_SUCCESS)
506 {
507 memcached_io_reset(server);
508 return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
509 }
510
511 if (verb == SET_OP && ptr->number_of_replicas > 0)
512 {
513 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
514 WATCHPOINT_STRING("replicating");
515
516 for (uint32_t x= 0; x < ptr->number_of_replicas; x++)
517 {
518 memcached_server_write_instance_st instance;
519
520 ++server_key;
521 if (server_key == memcached_server_count(ptr))
522 server_key= 0;
523
524 instance= memcached_server_instance_fetch(ptr, server_key);
525
526 if (memcached_vdo(instance, vector, 4, false) != MEMCACHED_SUCCESS)
527 {
528 memcached_io_reset(instance);
529 }
530 else
531 {
532 memcached_server_response_decrement(instance);
533 }
534 }
535 }
536
537 if (flush == false)
538 {
539 return MEMCACHED_BUFFERED;
540 }
541
542 if (noreply)
543 {
544 return MEMCACHED_SUCCESS;
545 }
546
547 return memcached_response(server, NULL, 0, NULL);
548 }
549