1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
39 #include <libmemcached/common.h>
41 enum memcached_storage_action_t
{
51 static inline const char *storage_op_string(memcached_storage_action_t verb
)
77 static inline uint8_t get_com_code(memcached_storage_action_t verb
, bool noreply
)
84 return PROTOCOL_BINARY_CMD_SETQ
;
87 return PROTOCOL_BINARY_CMD_ADDQ
;
89 case CAS_OP
: /* FALLTHROUGH */
91 return PROTOCOL_BINARY_CMD_REPLACEQ
;
94 return PROTOCOL_BINARY_CMD_APPENDQ
;
97 return PROTOCOL_BINARY_CMD_PREPENDQ
;
107 return PROTOCOL_BINARY_CMD_ADD
;
109 case CAS_OP
: /* FALLTHROUGH */
111 return PROTOCOL_BINARY_CMD_REPLACE
;
114 return PROTOCOL_BINARY_CMD_APPEND
;
117 return PROTOCOL_BINARY_CMD_PREPEND
;
120 return PROTOCOL_BINARY_CMD_SET
;
123 static memcached_return_t
memcached_send_binary(memcached_st
*ptr
,
124 memcached_server_write_instance_st server
,
134 memcached_storage_action_t verb
)
136 protocol_binary_request_set request
= {};
137 size_t send_length
= sizeof(request
.bytes
);
139 bool noreply
= server
->root
->flags
.no_reply
;
141 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
142 request
.message
.header
.request
.opcode
= get_com_code(verb
, noreply
);
143 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->_namespace
)));
144 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
145 if (verb
== APPEND_OP
or verb
== PREPEND_OP
)
147 send_length
-= 8; /* append & prepend does not contain extras! */
151 request
.message
.header
.request
.extlen
= 8;
152 request
.message
.body
.flags
= htonl(flags
);
153 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
156 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ memcached_array_size(ptr
->_namespace
) + value_length
+
157 request
.message
.header
.request
.extlen
));
161 request
.message
.header
.request
.cas
= memcached_htonll(cas
);
164 if (server
->root
->flags
.use_udp
and flush
== false)
166 size_t cmd_size
= send_length
+ key_length
+ value_length
;
168 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
170 return MEMCACHED_WRITE_FAILURE
;
172 if (cmd_size
+ server
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
174 memcached_io_write(server
, NULL
, 0, true);
178 struct libmemcached_io_vector_st vector
[]=
180 { request
.bytes
, send_length
},
181 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
183 { value
, value_length
}
186 /* write the header */
187 memcached_return_t rc
;
188 if ((rc
= memcached_vdo(server
, vector
, 4, flush
)) != MEMCACHED_SUCCESS
)
190 memcached_io_reset(server
);
192 if (ptr
->error_messages
== NULL
)
194 memcached_set_error(*server
, rc
, MEMCACHED_AT
);
197 return MEMCACHED_WRITE_FAILURE
;
200 if (verb
== SET_OP
&& ptr
->number_of_replicas
> 0)
202 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
203 WATCHPOINT_STRING("replicating");
205 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; x
++)
208 if (server_key
== memcached_server_count(ptr
))
213 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
215 if (memcached_vdo(instance
, vector
, 4, false) != MEMCACHED_SUCCESS
)
217 memcached_io_reset(instance
);
221 memcached_server_response_decrement(instance
);
228 return MEMCACHED_BUFFERED
;
233 return MEMCACHED_SUCCESS
;
236 return memcached_response(server
, NULL
, 0, NULL
);
239 static memcached_return_t
memcached_send_ascii(memcached_st
*ptr
,
240 memcached_server_write_instance_st instance
,
242 const size_t key_length
,
244 const size_t value_length
,
245 const time_t expiration
,
246 const uint32_t flags
,
249 const memcached_storage_action_t verb
)
251 // Invert the logic to make it simpler to read the code
252 bool reply
= (ptr
->flags
.no_reply
) ? false : true;
254 char flags_buffer
[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH
+1];
255 int flags_buffer_length
= snprintf(flags_buffer
, sizeof(flags_buffer
), " %u", flags
);
256 if (size_t(flags_buffer_length
) >= sizeof(flags_buffer
) or flags_buffer_length
< 0)
258 return memcached_set_error(*instance
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
259 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
262 char expiration_buffer
[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH
+1];
263 int expiration_buffer_length
= snprintf(expiration_buffer
, sizeof(expiration_buffer
), " %llu", (unsigned long long)expiration
);
264 if (size_t(expiration_buffer_length
) >= sizeof(expiration_buffer
) or expiration_buffer_length
< 0)
266 return memcached_set_error(*instance
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
267 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
270 char value_buffer
[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH
+1];
271 int value_buffer_length
= snprintf(value_buffer
, sizeof(value_buffer
), " %llu", (unsigned long long)value_length
);
272 if (size_t(value_buffer_length
) >= sizeof(value_buffer
) or value_buffer_length
< 0)
274 return memcached_set_error(*instance
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
275 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
278 char cas_buffer
[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH
+1];
279 int cas_buffer_length
= 0;
282 cas_buffer_length
= snprintf(cas_buffer
, sizeof(cas_buffer
), " %llu", (unsigned long long)cas
);
283 if (size_t(cas_buffer_length
) >= sizeof(cas_buffer
) or cas_buffer_length
< 0)
285 return memcached_set_error(*instance
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
286 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
290 struct libmemcached_io_vector_st vector
[]=
292 { storage_op_string(verb
), strlen(storage_op_string(verb
))},
293 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
295 { flags_buffer
, flags_buffer_length
},
296 { expiration_buffer
, expiration_buffer_length
},
297 { value_buffer
, value_buffer_length
},
298 { cas_buffer
, cas_buffer_length
},
299 { " noreply", reply
? 0 : memcached_literal_param_size(" noreply") },
300 { memcached_literal_param("\r\n") },
301 { value
, value_length
},
302 { memcached_literal_param("\r\n") }
305 if (memcached_is_udp(instance
->root
))
307 size_t write_length
= io_vector_total_size(vector
, 11);
309 size_t cmd_size
= write_length
+ value_length
+2;
310 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
312 return memcached_set_error(*ptr
, MEMCACHED_WRITE_FAILURE
, MEMCACHED_AT
);
315 if (cmd_size
+ instance
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
317 memcached_io_write(instance
, NULL
, 0, true);
321 /* Send command header */
322 memcached_return_t rc
= memcached_vdo(instance
, vector
, 11, flush
);
323 if (rc
== MEMCACHED_SUCCESS
)
325 if (ptr
->flags
.no_reply
and flush
)
327 rc
= MEMCACHED_SUCCESS
;
329 else if (flush
== false)
331 rc
= MEMCACHED_BUFFERED
;
335 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
336 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
338 if (rc
== MEMCACHED_STORED
)
340 rc
= MEMCACHED_SUCCESS
;
345 if (rc
== MEMCACHED_WRITE_FAILURE
)
347 memcached_io_reset(instance
);
350 if (memcached_failed(rc
) and ptr
->error_messages
== NULL
)
352 memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
358 static inline memcached_return_t
memcached_send(memcached_st
*ptr
,
359 const char *group_key
, size_t group_key_length
,
360 const char *key
, size_t key_length
,
361 const char *value
, size_t value_length
,
362 const time_t expiration
,
363 const uint32_t flags
,
365 memcached_storage_action_t verb
)
367 memcached_return_t rc
;
368 if (memcached_failed(rc
= initialize_query(ptr
)))
373 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
378 if (memcached_failed(memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
380 return MEMCACHED_BAD_KEY_PROVIDED
;
383 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
384 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
386 WATCHPOINT_SET(instance
->io_wait_count
.read
= 0);
387 WATCHPOINT_SET(instance
->io_wait_count
.write
= 0);
389 bool flush
= (bool) ((instance
->root
->flags
.buffer_requests
&& verb
== SET_OP
) ? 0 : 1);
390 if (ptr
->flags
.binary_protocol
)
392 rc
= memcached_send_binary(ptr
, instance
, server_key
,
394 value
, value_length
, expiration
,
395 flags
, cas
, flush
, verb
);
399 rc
= memcached_send_ascii(ptr
, instance
,
401 value
, value_length
, expiration
,
402 flags
, cas
, flush
, verb
);
409 memcached_return_t
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
410 const char *value
, size_t value_length
,
414 memcached_return_t rc
;
415 LIBMEMCACHED_MEMCACHED_SET_START();
416 rc
= memcached_send(ptr
, key
, key_length
,
417 key
, key_length
, value
, value_length
,
418 expiration
, flags
, 0, SET_OP
);
419 LIBMEMCACHED_MEMCACHED_SET_END();
423 memcached_return_t
memcached_add(memcached_st
*ptr
,
424 const char *key
, size_t key_length
,
425 const char *value
, size_t value_length
,
429 memcached_return_t rc
;
430 LIBMEMCACHED_MEMCACHED_ADD_START();
431 rc
= memcached_send(ptr
, key
, key_length
,
432 key
, key_length
, value
, value_length
,
433 expiration
, flags
, 0, ADD_OP
);
434 LIBMEMCACHED_MEMCACHED_ADD_END();
438 memcached_return_t
memcached_replace(memcached_st
*ptr
,
439 const char *key
, size_t key_length
,
440 const char *value
, size_t value_length
,
444 memcached_return_t rc
;
445 LIBMEMCACHED_MEMCACHED_REPLACE_START();
446 rc
= memcached_send(ptr
, key
, key_length
,
447 key
, key_length
, value
, value_length
,
448 expiration
, flags
, 0, REPLACE_OP
);
449 LIBMEMCACHED_MEMCACHED_REPLACE_END();
453 memcached_return_t
memcached_prepend(memcached_st
*ptr
,
454 const char *key
, size_t key_length
,
455 const char *value
, size_t value_length
,
459 memcached_return_t rc
;
460 rc
= memcached_send(ptr
, key
, key_length
,
461 key
, key_length
, value
, value_length
,
462 expiration
, flags
, 0, PREPEND_OP
);
466 memcached_return_t
memcached_append(memcached_st
*ptr
,
467 const char *key
, size_t key_length
,
468 const char *value
, size_t value_length
,
472 memcached_return_t rc
;
473 rc
= memcached_send(ptr
, key
, key_length
,
474 key
, key_length
, value
, value_length
,
475 expiration
, flags
, 0, APPEND_OP
);
479 memcached_return_t
memcached_cas(memcached_st
*ptr
,
480 const char *key
, size_t key_length
,
481 const char *value
, size_t value_length
,
486 memcached_return_t rc
;
487 rc
= memcached_send(ptr
, key
, key_length
,
488 key
, key_length
, value
, value_length
,
489 expiration
, flags
, cas
, CAS_OP
);
493 memcached_return_t
memcached_set_by_key(memcached_st
*ptr
,
494 const char *group_key
,
495 size_t group_key_length
,
496 const char *key
, size_t key_length
,
497 const char *value
, size_t value_length
,
501 memcached_return_t rc
;
502 LIBMEMCACHED_MEMCACHED_SET_START();
503 rc
= memcached_send(ptr
, group_key
, group_key_length
,
504 key
, key_length
, value
, value_length
,
505 expiration
, flags
, 0, SET_OP
);
506 LIBMEMCACHED_MEMCACHED_SET_END();
510 memcached_return_t
memcached_add_by_key(memcached_st
*ptr
,
511 const char *group_key
, size_t group_key_length
,
512 const char *key
, size_t key_length
,
513 const char *value
, size_t value_length
,
517 memcached_return_t rc
;
518 LIBMEMCACHED_MEMCACHED_ADD_START();
519 rc
= memcached_send(ptr
, group_key
, group_key_length
,
520 key
, key_length
, value
, value_length
,
521 expiration
, flags
, 0, ADD_OP
);
522 LIBMEMCACHED_MEMCACHED_ADD_END();
526 memcached_return_t
memcached_replace_by_key(memcached_st
*ptr
,
527 const char *group_key
, size_t group_key_length
,
528 const char *key
, size_t key_length
,
529 const char *value
, size_t value_length
,
533 memcached_return_t rc
;
534 LIBMEMCACHED_MEMCACHED_REPLACE_START();
535 rc
= memcached_send(ptr
, group_key
, group_key_length
,
536 key
, key_length
, value
, value_length
,
537 expiration
, flags
, 0, REPLACE_OP
);
538 LIBMEMCACHED_MEMCACHED_REPLACE_END();
542 memcached_return_t
memcached_prepend_by_key(memcached_st
*ptr
,
543 const char *group_key
, size_t group_key_length
,
544 const char *key
, size_t key_length
,
545 const char *value
, size_t value_length
,
549 memcached_return_t rc
;
550 rc
= memcached_send(ptr
, group_key
, group_key_length
,
551 key
, key_length
, value
, value_length
,
552 expiration
, flags
, 0, PREPEND_OP
);
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
,
563 memcached_return_t rc
;
564 rc
= memcached_send(ptr
, group_key
, group_key_length
,
565 key
, key_length
, value
, value_length
,
566 expiration
, flags
, 0, APPEND_OP
);
570 memcached_return_t
memcached_cas_by_key(memcached_st
*ptr
,
571 const char *group_key
, size_t group_key_length
,
572 const char *key
, size_t key_length
,
573 const char *value
, size_t value_length
,
578 memcached_return_t rc
;
579 rc
= memcached_send(ptr
, group_key
, group_key_length
,
580 key
, key_length
, value
, value_length
,
581 expiration
, flags
, cas
, CAS_OP
);