2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: Storage related functions, aka set, replace,..
21 } memcached_storage_action_t
;
24 static inline const char *storage_op_string(memcached_storage_action_t verb
)
41 return "tosserror"; /* This is impossible, fixes issue for compiler warning in VisualStudio */
47 static memcached_return_t
memcached_send_binary(memcached_st
*ptr
,
48 const char *master_key
,
49 size_t master_key_length
,
57 memcached_storage_action_t verb
);
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
,
66 memcached_storage_action_t verb
)
70 memcached_return_t rc
;
71 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
73 memcached_server_write_instance_st instance
;
75 WATCHPOINT_ASSERT(!(value
== NULL
&& value_length
> 0));
77 rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
78 unlikely (rc
!= MEMCACHED_SUCCESS
)
81 unlikely (memcached_server_count(ptr
) == 0)
82 return MEMCACHED_NO_SERVERS
;
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
;
87 if (ptr
->flags
.binary_protocol
)
89 return memcached_send_binary(ptr
, master_key
, master_key_length
,
91 value
, value_length
, expiration
,
95 server_key
= memcached_generate_hash_with_redistribution(ptr
, master_key
, master_key_length
);
96 instance
= memcached_server_instance_fetch(ptr
, server_key
);
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
,
105 (int)key_length
, key
, flags
,
106 (unsigned long long)expiration
, value_length
,
107 (unsigned long long)cas
,
108 (ptr
->flags
.no_reply
) ? " noreply" : "");
112 char *buffer_ptr
= buffer
;
113 const char *command
= storage_op_string(verb
);
115 /* Copy in the command, no space needed, we handle that in the command function*/
116 memcpy(buffer_ptr
, command
, strlen(command
));
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
);
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),
124 buffer_ptr
+= key_length
;
128 write_length
= (size_t)(buffer_ptr
- buffer
);
129 write_length
+= (size_t) snprintf(buffer_ptr
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
132 (unsigned long long)expiration
, value_length
,
133 ptr
->flags
.no_reply
? " noreply" : "");
136 if (ptr
->flags
.use_udp
&& ptr
->flags
.buffer_requests
)
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);
145 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
147 rc
= MEMCACHED_WRITE_FAILURE
;
151 /* Send command header */
152 rc
= memcached_do(instance
, buffer
, write_length
, false);
153 if (rc
!= MEMCACHED_SUCCESS
)
156 /* Send command body */
157 if (memcached_io_write(instance
, value
, value_length
, false) == -1)
159 rc
= MEMCACHED_WRITE_FAILURE
;
163 if (ptr
->flags
.buffer_requests
&& verb
== SET_OP
)
172 if (memcached_io_write(instance
, "\r\n", 2, to_write
) == -1)
174 rc
= MEMCACHED_WRITE_FAILURE
;
178 if (ptr
->flags
.no_reply
)
179 return (to_write
== false) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
181 if (to_write
== false)
182 return MEMCACHED_BUFFERED
;
184 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
186 if (rc
== MEMCACHED_STORED
)
187 return MEMCACHED_SUCCESS
;
192 if (rc
== MEMCACHED_WRITE_FAILURE
)
193 memcached_io_reset(instance
);
199 memcached_return_t
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
200 const char *value
, size_t value_length
,
204 memcached_return_t rc
;
205 LIBMEMCACHED_MEMCACHED_SET_START();
206 rc
= memcached_send(ptr
, key
, key_length
,
207 key
, key_length
, value
, value_length
,
208 expiration
, flags
, 0, SET_OP
);
209 LIBMEMCACHED_MEMCACHED_SET_END();
213 memcached_return_t
memcached_add(memcached_st
*ptr
,
214 const char *key
, size_t key_length
,
215 const char *value
, size_t value_length
,
219 memcached_return_t rc
;
220 LIBMEMCACHED_MEMCACHED_ADD_START();
221 rc
= memcached_send(ptr
, key
, key_length
,
222 key
, key_length
, value
, value_length
,
223 expiration
, flags
, 0, ADD_OP
);
224 LIBMEMCACHED_MEMCACHED_ADD_END();
228 memcached_return_t
memcached_replace(memcached_st
*ptr
,
229 const char *key
, size_t key_length
,
230 const char *value
, size_t value_length
,
234 memcached_return_t rc
;
235 LIBMEMCACHED_MEMCACHED_REPLACE_START();
236 rc
= memcached_send(ptr
, key
, key_length
,
237 key
, key_length
, value
, value_length
,
238 expiration
, flags
, 0, REPLACE_OP
);
239 LIBMEMCACHED_MEMCACHED_REPLACE_END();
243 memcached_return_t
memcached_prepend(memcached_st
*ptr
,
244 const char *key
, size_t key_length
,
245 const char *value
, size_t value_length
,
249 memcached_return_t rc
;
250 rc
= memcached_send(ptr
, key
, key_length
,
251 key
, key_length
, value
, value_length
,
252 expiration
, flags
, 0, PREPEND_OP
);
256 memcached_return_t
memcached_append(memcached_st
*ptr
,
257 const char *key
, size_t key_length
,
258 const char *value
, size_t value_length
,
262 memcached_return_t rc
;
263 rc
= memcached_send(ptr
, key
, key_length
,
264 key
, key_length
, value
, value_length
,
265 expiration
, flags
, 0, APPEND_OP
);
269 memcached_return_t
memcached_cas(memcached_st
*ptr
,
270 const char *key
, size_t key_length
,
271 const char *value
, size_t value_length
,
276 memcached_return_t rc
;
277 rc
= memcached_send(ptr
, key
, key_length
,
278 key
, key_length
, value
, value_length
,
279 expiration
, flags
, cas
, CAS_OP
);
283 memcached_return_t
memcached_set_by_key(memcached_st
*ptr
,
284 const char *master_key
__attribute__((unused
)),
285 size_t master_key_length
__attribute__((unused
)),
286 const char *key
, size_t key_length
,
287 const char *value
, size_t value_length
,
291 memcached_return_t rc
;
292 LIBMEMCACHED_MEMCACHED_SET_START();
293 rc
= memcached_send(ptr
, master_key
, master_key_length
,
294 key
, key_length
, value
, value_length
,
295 expiration
, flags
, 0, SET_OP
);
296 LIBMEMCACHED_MEMCACHED_SET_END();
300 memcached_return_t
memcached_add_by_key(memcached_st
*ptr
,
301 const char *master_key
, size_t master_key_length
,
302 const char *key
, size_t key_length
,
303 const char *value
, size_t value_length
,
307 memcached_return_t rc
;
308 LIBMEMCACHED_MEMCACHED_ADD_START();
309 rc
= memcached_send(ptr
, master_key
, master_key_length
,
310 key
, key_length
, value
, value_length
,
311 expiration
, flags
, 0, ADD_OP
);
312 LIBMEMCACHED_MEMCACHED_ADD_END();
316 memcached_return_t
memcached_replace_by_key(memcached_st
*ptr
,
317 const char *master_key
, size_t master_key_length
,
318 const char *key
, size_t key_length
,
319 const char *value
, size_t value_length
,
323 memcached_return_t rc
;
324 LIBMEMCACHED_MEMCACHED_REPLACE_START();
325 rc
= memcached_send(ptr
, master_key
, master_key_length
,
326 key
, key_length
, value
, value_length
,
327 expiration
, flags
, 0, REPLACE_OP
);
328 LIBMEMCACHED_MEMCACHED_REPLACE_END();
332 memcached_return_t
memcached_prepend_by_key(memcached_st
*ptr
,
333 const char *master_key
, size_t master_key_length
,
334 const char *key
, size_t key_length
,
335 const char *value
, size_t value_length
,
339 memcached_return_t rc
;
340 rc
= memcached_send(ptr
, master_key
, master_key_length
,
341 key
, key_length
, value
, value_length
,
342 expiration
, flags
, 0, PREPEND_OP
);
346 memcached_return_t
memcached_append_by_key(memcached_st
*ptr
,
347 const char *master_key
, size_t master_key_length
,
348 const char *key
, size_t key_length
,
349 const char *value
, size_t value_length
,
353 memcached_return_t rc
;
354 rc
= memcached_send(ptr
, master_key
, master_key_length
,
355 key
, key_length
, value
, value_length
,
356 expiration
, flags
, 0, APPEND_OP
);
360 memcached_return_t
memcached_cas_by_key(memcached_st
*ptr
,
361 const char *master_key
, size_t master_key_length
,
362 const char *key
, size_t key_length
,
363 const char *value
, size_t value_length
,
368 memcached_return_t rc
;
369 rc
= memcached_send(ptr
, master_key
, master_key_length
,
370 key
, key_length
, value
, value_length
,
371 expiration
, flags
, cas
, CAS_OP
);
375 static inline uint8_t get_com_code(memcached_storage_action_t verb
, bool noreply
)
377 /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
378 * be used uninitialized in this function. FAIL */
385 ret
=PROTOCOL_BINARY_CMD_SETQ
;
388 ret
=PROTOCOL_BINARY_CMD_ADDQ
;
390 case CAS_OP
: /* FALLTHROUGH */
392 ret
=PROTOCOL_BINARY_CMD_REPLACEQ
;
395 ret
=PROTOCOL_BINARY_CMD_APPENDQ
;
398 ret
=PROTOCOL_BINARY_CMD_PREPENDQ
;
401 WATCHPOINT_ASSERT(verb
);
408 ret
=PROTOCOL_BINARY_CMD_SET
;
411 ret
=PROTOCOL_BINARY_CMD_ADD
;
413 case CAS_OP
: /* FALLTHROUGH */
415 ret
=PROTOCOL_BINARY_CMD_REPLACE
;
418 ret
=PROTOCOL_BINARY_CMD_APPEND
;
421 ret
=PROTOCOL_BINARY_CMD_PREPEND
;
424 WATCHPOINT_ASSERT(verb
);
433 static memcached_return_t
memcached_send_binary(memcached_st
*ptr
,
434 const char *master_key
,
435 size_t master_key_length
,
443 memcached_storage_action_t verb
)
446 protocol_binary_request_set request
= {.bytes
= {0}};
447 size_t send_length
= sizeof(request
.bytes
);
448 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, master_key
,
451 memcached_server_write_instance_st server
=
452 memcached_server_instance_fetch(ptr
, server_key
);
454 bool noreply
= server
->root
->flags
.no_reply
;
456 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
457 request
.message
.header
.request
.opcode
= get_com_code(verb
, noreply
);
458 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
459 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
460 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
461 send_length
-= 8; /* append & prepend does not contain extras! */
464 request
.message
.header
.request
.extlen
= 8;
465 request
.message
.body
.flags
= htonl(flags
);
466 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
469 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ value_length
+
470 request
.message
.header
.request
.extlen
));
473 request
.message
.header
.request
.cas
= htonll(cas
);
475 flush
= (bool) ((server
->root
->flags
.buffer_requests
&& verb
== SET_OP
) ? 0 : 1);
477 if (server
->root
->flags
.use_udp
&& ! flush
)
479 size_t cmd_size
= send_length
+ key_length
+ value_length
;
481 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
483 return MEMCACHED_WRITE_FAILURE
;
485 if (cmd_size
+ server
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
487 memcached_io_write(server
, NULL
, 0, true);
491 /* write the header */
492 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, false) != MEMCACHED_SUCCESS
) ||
493 (memcached_io_write(server
, key
, key_length
, false) == -1) ||
494 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
496 memcached_io_reset(server
);
497 return MEMCACHED_WRITE_FAILURE
;
500 unlikely (verb
== SET_OP
&& ptr
->number_of_replicas
> 0)
502 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
504 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; x
++)
506 memcached_server_write_instance_st instance
;
509 if (server_key
== memcached_server_count(ptr
))
512 instance
= memcached_server_instance_fetch(ptr
, server_key
);
514 if ((memcached_do(instance
, (const char*)request
.bytes
,
515 send_length
, false) != MEMCACHED_SUCCESS
) ||
516 (memcached_io_write(instance
, key
, key_length
, false) == -1) ||
517 (memcached_io_write(instance
, value
, value_length
, flush
) == -1))
519 memcached_io_reset(instance
);
523 memcached_server_response_decrement(instance
);
530 return MEMCACHED_BUFFERED
;
535 return MEMCACHED_SUCCESS
;
538 return memcached_response(server
, NULL
, 0, NULL
);