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