c++: fix return value type
[awesomized/libmemcached] / libmemcached / storage.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38
39 #include <libmemcached/common.h>
40
41 enum memcached_storage_action_t {
42 SET_OP,
43 REPLACE_OP,
44 ADD_OP,
45 PREPEND_OP,
46 APPEND_OP,
47 CAS_OP
48 };
49
50 /* Inline this */
51 static inline const char *storage_op_string(memcached_storage_action_t verb)
52 {
53 switch (verb)
54 {
55 case REPLACE_OP:
56 return "replace ";
57
58 case ADD_OP:
59 return "add ";
60
61 case PREPEND_OP:
62 return "prepend ";
63
64 case APPEND_OP:
65 return "append ";
66
67 case CAS_OP:
68 return "cas ";
69
70 case SET_OP:
71 break;
72 }
73
74 return "set ";
75 }
76
77 static inline bool can_by_encrypted(const memcached_storage_action_t verb)
78 {
79 switch (verb)
80 {
81 case SET_OP:
82 case ADD_OP:
83 case CAS_OP:
84 case REPLACE_OP:
85 return true;
86
87 case APPEND_OP:
88 case PREPEND_OP:
89 break;
90 }
91
92 return false;
93 }
94
95 static inline uint8_t get_com_code(const memcached_storage_action_t verb, const bool reply)
96 {
97 if (reply == false)
98 {
99 switch (verb)
100 {
101 case SET_OP:
102 return PROTOCOL_BINARY_CMD_SETQ;
103
104 case ADD_OP:
105 return PROTOCOL_BINARY_CMD_ADDQ;
106
107 case CAS_OP: /* FALLTHROUGH */
108 case REPLACE_OP:
109 return PROTOCOL_BINARY_CMD_REPLACEQ;
110
111 case APPEND_OP:
112 return PROTOCOL_BINARY_CMD_APPENDQ;
113
114 case PREPEND_OP:
115 return PROTOCOL_BINARY_CMD_PREPENDQ;
116 }
117 }
118
119 switch (verb)
120 {
121 case SET_OP:
122 break;
123
124 case ADD_OP:
125 return PROTOCOL_BINARY_CMD_ADD;
126
127 case CAS_OP: /* FALLTHROUGH */
128 case REPLACE_OP:
129 return PROTOCOL_BINARY_CMD_REPLACE;
130
131 case APPEND_OP:
132 return PROTOCOL_BINARY_CMD_APPEND;
133
134 case PREPEND_OP:
135 return PROTOCOL_BINARY_CMD_PREPEND;
136 }
137
138 return PROTOCOL_BINARY_CMD_SET;
139 }
140
141 static memcached_return_t memcached_send_binary(Memcached *ptr,
142 memcached_instance_st* server,
143 uint32_t server_key,
144 const char *key,
145 const size_t key_length,
146 const char *value,
147 const size_t value_length,
148 const time_t expiration,
149 const uint32_t flags,
150 const uint64_t cas,
151 const bool flush,
152 const bool reply,
153 memcached_storage_action_t verb)
154 {
155 protocol_binary_request_set request= {};
156 size_t send_length= sizeof(request.bytes);
157
158 initialize_binary_request(server, request.message.header);
159
160 request.message.header.request.opcode= get_com_code(verb, reply);
161 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
162 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
163 if (verb == APPEND_OP or verb == PREPEND_OP)
164 {
165 send_length -= 8; /* append & prepend does not contain extras! */
166 }
167 else
168 {
169 request.message.header.request.extlen= 8;
170 request.message.body.flags= htonl(flags);
171 request.message.body.expiration= htonl((uint32_t)expiration);
172 }
173
174 request.message.header.request.bodylen= htonl((uint32_t) (key_length + memcached_array_size(ptr->_namespace) + value_length +
175 request.message.header.request.extlen));
176
177 if (cas)
178 {
179 request.message.header.request.cas= memcached_htonll(cas);
180 }
181
182 libmemcached_io_vector_st vector[]=
183 {
184 { NULL, 0 },
185 { request.bytes, send_length },
186 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
187 { key, key_length },
188 { value, value_length }
189 };
190
191 /* write the header */
192 memcached_return_t rc;
193 if ((rc= memcached_vdo(server, vector, 5, flush)) != MEMCACHED_SUCCESS)
194 {
195 assert(memcached_last_error(server->root) != MEMCACHED_SUCCESS);
196 return memcached_last_error(server->root);
197 }
198
199 if (verb == SET_OP and ptr->number_of_replicas > 0)
200 {
201 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
202 WATCHPOINT_STRING("replicating");
203
204 for (uint32_t x= 0; x < ptr->number_of_replicas; x++)
205 {
206 ++server_key;
207 if (server_key == memcached_server_count(ptr))
208 {
209 server_key= 0;
210 }
211
212 memcached_instance_st* instance= memcached_instance_fetch(ptr, server_key);
213
214 if (memcached_success(memcached_vdo(instance, vector, 5, false)))
215 {
216 memcached_server_response_decrement(instance);
217 }
218 }
219 }
220
221 if (flush == false)
222 {
223 return MEMCACHED_BUFFERED;
224 }
225
226 // No reply always assumes success
227 if (reply == false)
228 {
229 return MEMCACHED_SUCCESS;
230 }
231
232 return memcached_response(server, NULL, 0, NULL);
233 }
234
235 static memcached_return_t memcached_send_ascii(Memcached *ptr,
236 memcached_instance_st* instance,
237 const char *key,
238 const size_t key_length,
239 const char *value,
240 const size_t value_length,
241 const time_t expiration,
242 const uint32_t flags,
243 const uint64_t cas,
244 const bool flush,
245 const bool reply,
246 const memcached_storage_action_t verb)
247 {
248 char flags_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
249 int flags_buffer_length= snprintf(flags_buffer, sizeof(flags_buffer), " %u", flags);
250 if (size_t(flags_buffer_length) >= sizeof(flags_buffer) or flags_buffer_length < 0)
251 {
252 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
253 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
254 }
255
256 char expiration_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
257 int expiration_buffer_length= snprintf(expiration_buffer, sizeof(expiration_buffer), " %llu", (unsigned long long)expiration);
258 if (size_t(expiration_buffer_length) >= sizeof(expiration_buffer) or expiration_buffer_length < 0)
259 {
260 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
261 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
262 }
263
264 char value_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
265 int value_buffer_length= snprintf(value_buffer, sizeof(value_buffer), " %llu", (unsigned long long)value_length);
266 if (size_t(value_buffer_length) >= sizeof(value_buffer) or value_buffer_length < 0)
267 {
268 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
269 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
270 }
271
272 char cas_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
273 int cas_buffer_length= 0;
274 if (cas)
275 {
276 cas_buffer_length= snprintf(cas_buffer, sizeof(cas_buffer), " %llu", (unsigned long long)cas);
277 if (size_t(cas_buffer_length) >= sizeof(cas_buffer) or cas_buffer_length < 0)
278 {
279 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
280 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
281 }
282 }
283
284 libmemcached_io_vector_st vector[]=
285 {
286 { NULL, 0 },
287 { storage_op_string(verb), strlen(storage_op_string(verb))},
288 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
289 { key, key_length },
290 { flags_buffer, size_t(flags_buffer_length) },
291 { expiration_buffer, size_t(expiration_buffer_length) },
292 { value_buffer, size_t(value_buffer_length) },
293 { cas_buffer, size_t(cas_buffer_length) },
294 { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") },
295 { memcached_literal_param("\r\n") },
296 { value, value_length },
297 { memcached_literal_param("\r\n") }
298 };
299
300 /* Send command header */
301 memcached_return_t rc= memcached_vdo(instance, vector, 12, flush);
302
303 // If we should not reply, return with MEMCACHED_SUCCESS, unless error
304 if (reply == false)
305 {
306 return memcached_success(rc) ? MEMCACHED_SUCCESS : rc;
307 }
308
309 if (flush == false)
310 {
311 return memcached_success(rc) ? MEMCACHED_BUFFERED : rc;
312 }
313
314 if (rc == MEMCACHED_SUCCESS)
315 {
316 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
317 rc= memcached_response(instance, buffer, sizeof(buffer), NULL);
318
319 if (rc == MEMCACHED_STORED)
320 {
321 return MEMCACHED_SUCCESS;
322 }
323 }
324
325 assert(memcached_failed(rc));
326 #if 0
327 if (memcached_has_error(ptr) == false)
328 {
329 return memcached_set_error(*ptr, rc, MEMCACHED_AT);
330 }
331 #endif
332
333 return rc;
334 }
335
336 static inline memcached_return_t memcached_send(memcached_st *shell,
337 const char *group_key, size_t group_key_length,
338 const char *key, size_t key_length,
339 const char *value, size_t value_length,
340 const time_t expiration,
341 const uint32_t flags,
342 const uint64_t cas,
343 memcached_storage_action_t verb)
344 {
345 Memcached* ptr= memcached2Memcached(shell);
346 memcached_return_t rc;
347 if (memcached_failed(rc= initialize_query(ptr, true)))
348 {
349 return rc;
350 }
351
352 if (memcached_failed(memcached_key_test(*ptr, (const char **)&key, &key_length, 1)))
353 {
354 return memcached_last_error(ptr);
355 }
356
357 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
358 memcached_instance_st* instance= memcached_instance_fetch(ptr, server_key);
359
360 WATCHPOINT_SET(instance->io_wait_count.read= 0);
361 WATCHPOINT_SET(instance->io_wait_count.write= 0);
362
363 bool flush= true;
364 if (memcached_is_buffering(instance->root) and verb == SET_OP)
365 {
366 flush= false;
367 }
368
369 bool reply= memcached_is_replying(ptr);
370
371 hashkit_string_st* destination= NULL;
372
373 if (memcached_is_encrypted(ptr))
374 {
375 if (can_by_encrypted(verb) == false)
376 {
377 return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
378 memcached_literal_param("Operation not allowed while encyrption is enabled"));
379 }
380
381 if ((destination= hashkit_encrypt(&ptr->hashkit, value, value_length)) == NULL)
382 {
383 return rc;
384 }
385 value= hashkit_string_c_str(destination);
386 value_length= hashkit_string_length(destination);
387 }
388
389 if (memcached_is_binary(ptr))
390 {
391 rc= memcached_send_binary(ptr, instance, server_key,
392 key, key_length,
393 value, value_length, expiration,
394 flags, cas, flush, reply, verb);
395 }
396 else
397 {
398 rc= memcached_send_ascii(ptr, instance,
399 key, key_length,
400 value, value_length, expiration,
401 flags, cas, flush, reply, verb);
402 }
403
404 hashkit_string_free(destination);
405
406 return rc;
407 }
408
409
410 memcached_return_t memcached_set(memcached_st *ptr, const char *key, size_t key_length,
411 const char *value, size_t value_length,
412 time_t expiration,
413 uint32_t flags)
414 {
415 memcached_return_t rc;
416 LIBMEMCACHED_MEMCACHED_SET_START();
417 rc= memcached_send(ptr, key, key_length,
418 key, key_length, value, value_length,
419 expiration, flags, 0, SET_OP);
420 LIBMEMCACHED_MEMCACHED_SET_END();
421 return rc;
422 }
423
424 memcached_return_t memcached_add(memcached_st *ptr,
425 const char *key, size_t key_length,
426 const char *value, size_t value_length,
427 time_t expiration,
428 uint32_t flags)
429 {
430 memcached_return_t rc;
431 LIBMEMCACHED_MEMCACHED_ADD_START();
432 rc= memcached_send(ptr, key, key_length,
433 key, key_length, value, value_length,
434 expiration, flags, 0, ADD_OP);
435
436 LIBMEMCACHED_MEMCACHED_ADD_END();
437 return rc;
438 }
439
440 memcached_return_t memcached_replace(memcached_st *ptr,
441 const char *key, size_t key_length,
442 const char *value, size_t value_length,
443 time_t expiration,
444 uint32_t flags)
445 {
446 memcached_return_t rc;
447 LIBMEMCACHED_MEMCACHED_REPLACE_START();
448 rc= memcached_send(ptr, key, key_length,
449 key, key_length, value, value_length,
450 expiration, flags, 0, REPLACE_OP);
451 LIBMEMCACHED_MEMCACHED_REPLACE_END();
452 return rc;
453 }
454
455 memcached_return_t memcached_prepend(memcached_st *ptr,
456 const char *key, size_t key_length,
457 const char *value, size_t value_length,
458 time_t expiration,
459 uint32_t flags)
460 {
461 memcached_return_t rc;
462 rc= memcached_send(ptr, key, key_length,
463 key, key_length, value, value_length,
464 expiration, flags, 0, PREPEND_OP);
465 return rc;
466 }
467
468 memcached_return_t memcached_append(memcached_st *ptr,
469 const char *key, size_t key_length,
470 const char *value, size_t value_length,
471 time_t expiration,
472 uint32_t flags)
473 {
474 memcached_return_t rc;
475 rc= memcached_send(ptr, key, key_length,
476 key, key_length, value, value_length,
477 expiration, flags, 0, APPEND_OP);
478 return rc;
479 }
480
481 memcached_return_t memcached_cas(memcached_st *ptr,
482 const char *key, size_t key_length,
483 const char *value, size_t value_length,
484 time_t expiration,
485 uint32_t flags,
486 uint64_t cas)
487 {
488 memcached_return_t rc;
489 rc= memcached_send(ptr, key, key_length,
490 key, key_length, value, value_length,
491 expiration, flags, cas, CAS_OP);
492 return rc;
493 }
494
495 memcached_return_t memcached_set_by_key(memcached_st *ptr,
496 const char *group_key,
497 size_t group_key_length,
498 const char *key, size_t key_length,
499 const char *value, size_t value_length,
500 time_t expiration,
501 uint32_t flags)
502 {
503 memcached_return_t rc;
504 LIBMEMCACHED_MEMCACHED_SET_START();
505 rc= memcached_send(ptr, group_key, group_key_length,
506 key, key_length, value, value_length,
507 expiration, flags, 0, SET_OP);
508 LIBMEMCACHED_MEMCACHED_SET_END();
509 return rc;
510 }
511
512 memcached_return_t memcached_add_by_key(memcached_st *ptr,
513 const char *group_key, size_t group_key_length,
514 const char *key, size_t key_length,
515 const char *value, size_t value_length,
516 time_t expiration,
517 uint32_t flags)
518 {
519 memcached_return_t rc;
520 LIBMEMCACHED_MEMCACHED_ADD_START();
521 rc= memcached_send(ptr, group_key, group_key_length,
522 key, key_length, value, value_length,
523 expiration, flags, 0, ADD_OP);
524 LIBMEMCACHED_MEMCACHED_ADD_END();
525 return rc;
526 }
527
528 memcached_return_t memcached_replace_by_key(memcached_st *ptr,
529 const char *group_key, size_t group_key_length,
530 const char *key, size_t key_length,
531 const char *value, size_t value_length,
532 time_t expiration,
533 uint32_t flags)
534 {
535 memcached_return_t rc;
536 LIBMEMCACHED_MEMCACHED_REPLACE_START();
537 rc= memcached_send(ptr, group_key, group_key_length,
538 key, key_length, value, value_length,
539 expiration, flags, 0, REPLACE_OP);
540 LIBMEMCACHED_MEMCACHED_REPLACE_END();
541 return rc;
542 }
543
544 memcached_return_t memcached_prepend_by_key(memcached_st *ptr,
545 const char *group_key, size_t group_key_length,
546 const char *key, size_t key_length,
547 const char *value, size_t value_length,
548 time_t expiration,
549 uint32_t flags)
550 {
551 return memcached_send(ptr, group_key, group_key_length,
552 key, key_length, value, value_length,
553 expiration, flags, 0, PREPEND_OP);
554 }
555
556 memcached_return_t memcached_append_by_key(memcached_st *ptr,
557 const char *group_key, size_t group_key_length,
558 const char *key, size_t key_length,
559 const char *value, size_t value_length,
560 time_t expiration,
561 uint32_t flags)
562 {
563 return memcached_send(ptr, group_key, group_key_length,
564 key, key_length, value, value_length,
565 expiration, flags, 0, APPEND_OP);
566 }
567
568 memcached_return_t memcached_cas_by_key(memcached_st *ptr,
569 const char *group_key, size_t group_key_length,
570 const char *key, size_t key_length,
571 const char *value, size_t value_length,
572 time_t expiration,
573 uint32_t flags,
574 uint64_t cas)
575 {
576 return memcached_send(ptr, group_key, group_key_length,
577 key, key_length, value, value_length,
578 expiration, flags, cas, CAS_OP);
579 }
580