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_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(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
, 0);
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
== 0) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
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 memcached_io_reset(instance
);
198 memcached_return_t
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
199 const char *value
, size_t value_length
,
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();
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
,
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();
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
,
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();
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
,
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
);
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
,
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
);
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
,
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
);
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
,
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();
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
,
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();
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
,
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();
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
,
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
);
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
,
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
);
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
,
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
);
374 static inline uint8_t get_com_code(memcached_storage_action_t verb
, bool noreply
)
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 */
384 ret
=PROTOCOL_BINARY_CMD_SETQ
;
387 ret
=PROTOCOL_BINARY_CMD_ADDQ
;
389 case CAS_OP
: /* FALLTHROUGH */
391 ret
=PROTOCOL_BINARY_CMD_REPLACEQ
;
394 ret
=PROTOCOL_BINARY_CMD_APPENDQ
;
397 ret
=PROTOCOL_BINARY_CMD_PREPENDQ
;
400 WATCHPOINT_ASSERT(verb
);
407 ret
=PROTOCOL_BINARY_CMD_SET
;
410 ret
=PROTOCOL_BINARY_CMD_ADD
;
412 case CAS_OP
: /* FALLTHROUGH */
414 ret
=PROTOCOL_BINARY_CMD_REPLACE
;
417 ret
=PROTOCOL_BINARY_CMD_APPEND
;
420 ret
=PROTOCOL_BINARY_CMD_PREPEND
;
423 WATCHPOINT_ASSERT(verb
);
432 static memcached_return_t
memcached_send_binary(memcached_st
*ptr
,
433 const char *master_key
,
434 size_t master_key_length
,
442 memcached_storage_action_t verb
)
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
,
450 memcached_server_instance_st
*server
=
451 memcached_server_instance_fetch(ptr
, server_key
);
453 bool noreply
= server
->root
->flags
.no_reply
;
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! */
463 request
.message
.header
.request
.extlen
= 8;
464 request
.message
.body
.flags
= htonl(flags
);
465 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
468 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ value_length
+
469 request
.message
.header
.request
.extlen
));
472 request
.message
.header
.request
.cas
= htonll(cas
);
474 flush
= (uint8_t) ((server
->root
->flags
.buffer_requests
&& verb
== SET_OP
) ? 0 : 1);
476 if (server
->root
->flags
.use_udp
&& !flush
)
478 size_t cmd_size
= send_length
+ key_length
+ value_length
;
480 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
482 return MEMCACHED_WRITE_FAILURE
;
484 if (cmd_size
+ server
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
486 memcached_io_write(server
, NULL
, 0, true);
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
, false) == -1) ||
493 (memcached_io_write(server
, value
, value_length
, (char) flush
) == -1))
495 memcached_io_reset(server
);
496 return MEMCACHED_WRITE_FAILURE
;
499 unlikely (verb
== SET_OP
&& ptr
->number_of_replicas
> 0)
501 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
503 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; x
++)
505 memcached_server_instance_st
*instance
;
508 if (server_key
== memcached_server_count(ptr
))
511 instance
= memcached_server_instance_fetch(ptr
, server_key
);
513 if ((memcached_do(instance
, (const char*)request
.bytes
,
514 send_length
, 0) != MEMCACHED_SUCCESS
) ||
515 (memcached_io_write(instance
, key
, key_length
, false) == -1) ||
516 (memcached_io_write(instance
, value
, value_length
, (char) flush
) == -1))
518 memcached_io_reset(instance
);
522 memcached_server_response_decrement(instance
);
529 return MEMCACHED_BUFFERED
;
534 return MEMCACHED_SUCCESS
;
537 return memcached_response(server
, NULL
, 0, NULL
);