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 struct __write_vector_st vector
[]=
153 { .length
= write_length
, .buffer
= buffer
},
154 { .length
= value_length
, .buffer
= value
},
155 { .length
= 2, .buffer
= "\r\n" }
158 if (ptr
->flags
.buffer_requests
&& verb
== SET_OP
)
167 /* Send command header */
168 rc
= memcached_vdo(instance
, vector
, 3, to_write
);
169 if (rc
== MEMCACHED_SUCCESS
)
172 if (ptr
->flags
.no_reply
)
173 return (to_write
== false) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
175 if (to_write
== false)
176 return MEMCACHED_BUFFERED
;
178 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
180 if (rc
== MEMCACHED_STORED
)
181 return MEMCACHED_SUCCESS
;
187 if (rc
== MEMCACHED_WRITE_FAILURE
)
188 memcached_io_reset(instance
);
194 memcached_return_t
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
195 const char *value
, size_t value_length
,
199 memcached_return_t rc
;
200 LIBMEMCACHED_MEMCACHED_SET_START();
201 rc
= memcached_send(ptr
, key
, key_length
,
202 key
, key_length
, value
, value_length
,
203 expiration
, flags
, 0, SET_OP
);
204 LIBMEMCACHED_MEMCACHED_SET_END();
208 memcached_return_t
memcached_add(memcached_st
*ptr
,
209 const char *key
, size_t key_length
,
210 const char *value
, size_t value_length
,
214 memcached_return_t rc
;
215 LIBMEMCACHED_MEMCACHED_ADD_START();
216 rc
= memcached_send(ptr
, key
, key_length
,
217 key
, key_length
, value
, value_length
,
218 expiration
, flags
, 0, ADD_OP
);
219 LIBMEMCACHED_MEMCACHED_ADD_END();
223 memcached_return_t
memcached_replace(memcached_st
*ptr
,
224 const char *key
, size_t key_length
,
225 const char *value
, size_t value_length
,
229 memcached_return_t rc
;
230 LIBMEMCACHED_MEMCACHED_REPLACE_START();
231 rc
= memcached_send(ptr
, key
, key_length
,
232 key
, key_length
, value
, value_length
,
233 expiration
, flags
, 0, REPLACE_OP
);
234 LIBMEMCACHED_MEMCACHED_REPLACE_END();
238 memcached_return_t
memcached_prepend(memcached_st
*ptr
,
239 const char *key
, size_t key_length
,
240 const char *value
, size_t value_length
,
244 memcached_return_t rc
;
245 rc
= memcached_send(ptr
, key
, key_length
,
246 key
, key_length
, value
, value_length
,
247 expiration
, flags
, 0, PREPEND_OP
);
251 memcached_return_t
memcached_append(memcached_st
*ptr
,
252 const char *key
, size_t key_length
,
253 const char *value
, size_t value_length
,
257 memcached_return_t rc
;
258 rc
= memcached_send(ptr
, key
, key_length
,
259 key
, key_length
, value
, value_length
,
260 expiration
, flags
, 0, APPEND_OP
);
264 memcached_return_t
memcached_cas(memcached_st
*ptr
,
265 const char *key
, size_t key_length
,
266 const char *value
, size_t value_length
,
271 memcached_return_t rc
;
272 rc
= memcached_send(ptr
, key
, key_length
,
273 key
, key_length
, value
, value_length
,
274 expiration
, flags
, cas
, CAS_OP
);
278 memcached_return_t
memcached_set_by_key(memcached_st
*ptr
,
279 const char *master_key
__attribute__((unused
)),
280 size_t master_key_length
__attribute__((unused
)),
281 const char *key
, size_t key_length
,
282 const char *value
, size_t value_length
,
286 memcached_return_t rc
;
287 LIBMEMCACHED_MEMCACHED_SET_START();
288 rc
= memcached_send(ptr
, master_key
, master_key_length
,
289 key
, key_length
, value
, value_length
,
290 expiration
, flags
, 0, SET_OP
);
291 LIBMEMCACHED_MEMCACHED_SET_END();
295 memcached_return_t
memcached_add_by_key(memcached_st
*ptr
,
296 const char *master_key
, size_t master_key_length
,
297 const char *key
, size_t key_length
,
298 const char *value
, size_t value_length
,
302 memcached_return_t rc
;
303 LIBMEMCACHED_MEMCACHED_ADD_START();
304 rc
= memcached_send(ptr
, master_key
, master_key_length
,
305 key
, key_length
, value
, value_length
,
306 expiration
, flags
, 0, ADD_OP
);
307 LIBMEMCACHED_MEMCACHED_ADD_END();
311 memcached_return_t
memcached_replace_by_key(memcached_st
*ptr
,
312 const char *master_key
, size_t master_key_length
,
313 const char *key
, size_t key_length
,
314 const char *value
, size_t value_length
,
318 memcached_return_t rc
;
319 LIBMEMCACHED_MEMCACHED_REPLACE_START();
320 rc
= memcached_send(ptr
, master_key
, master_key_length
,
321 key
, key_length
, value
, value_length
,
322 expiration
, flags
, 0, REPLACE_OP
);
323 LIBMEMCACHED_MEMCACHED_REPLACE_END();
327 memcached_return_t
memcached_prepend_by_key(memcached_st
*ptr
,
328 const char *master_key
, size_t master_key_length
,
329 const char *key
, size_t key_length
,
330 const char *value
, size_t value_length
,
334 memcached_return_t rc
;
335 rc
= memcached_send(ptr
, master_key
, master_key_length
,
336 key
, key_length
, value
, value_length
,
337 expiration
, flags
, 0, PREPEND_OP
);
341 memcached_return_t
memcached_append_by_key(memcached_st
*ptr
,
342 const char *master_key
, size_t master_key_length
,
343 const char *key
, size_t key_length
,
344 const char *value
, size_t value_length
,
348 memcached_return_t rc
;
349 rc
= memcached_send(ptr
, master_key
, master_key_length
,
350 key
, key_length
, value
, value_length
,
351 expiration
, flags
, 0, APPEND_OP
);
355 memcached_return_t
memcached_cas_by_key(memcached_st
*ptr
,
356 const char *master_key
, size_t master_key_length
,
357 const char *key
, size_t key_length
,
358 const char *value
, size_t value_length
,
363 memcached_return_t rc
;
364 rc
= memcached_send(ptr
, master_key
, master_key_length
,
365 key
, key_length
, value
, value_length
,
366 expiration
, flags
, cas
, CAS_OP
);
370 static inline uint8_t get_com_code(memcached_storage_action_t verb
, bool noreply
)
372 /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
373 * be used uninitialized in this function. FAIL */
380 ret
=PROTOCOL_BINARY_CMD_SETQ
;
383 ret
=PROTOCOL_BINARY_CMD_ADDQ
;
385 case CAS_OP
: /* FALLTHROUGH */
387 ret
=PROTOCOL_BINARY_CMD_REPLACEQ
;
390 ret
=PROTOCOL_BINARY_CMD_APPENDQ
;
393 ret
=PROTOCOL_BINARY_CMD_PREPENDQ
;
396 WATCHPOINT_ASSERT(verb
);
403 ret
=PROTOCOL_BINARY_CMD_SET
;
406 ret
=PROTOCOL_BINARY_CMD_ADD
;
408 case CAS_OP
: /* FALLTHROUGH */
410 ret
=PROTOCOL_BINARY_CMD_REPLACE
;
413 ret
=PROTOCOL_BINARY_CMD_APPEND
;
416 ret
=PROTOCOL_BINARY_CMD_PREPEND
;
419 WATCHPOINT_ASSERT(verb
);
428 static memcached_return_t
memcached_send_binary(memcached_st
*ptr
,
429 const char *master_key
,
430 size_t master_key_length
,
438 memcached_storage_action_t verb
)
441 protocol_binary_request_set request
= {.bytes
= {0}};
442 size_t send_length
= sizeof(request
.bytes
);
443 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, master_key
,
446 memcached_server_write_instance_st server
=
447 memcached_server_instance_fetch(ptr
, server_key
);
449 bool noreply
= server
->root
->flags
.no_reply
;
451 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
452 request
.message
.header
.request
.opcode
= get_com_code(verb
, noreply
);
453 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ ptr
->prefix_key_length
));
454 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
455 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
456 send_length
-= 8; /* append & prepend does not contain extras! */
459 request
.message
.header
.request
.extlen
= 8;
460 request
.message
.body
.flags
= htonl(flags
);
461 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
464 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ ptr
->prefix_key_length
+ value_length
+
465 request
.message
.header
.request
.extlen
));
468 request
.message
.header
.request
.cas
= htonll(cas
);
470 flush
= (bool) ((server
->root
->flags
.buffer_requests
&& verb
== SET_OP
) ? 0 : 1);
472 if (server
->root
->flags
.use_udp
&& ! flush
)
474 size_t cmd_size
= send_length
+ key_length
+ value_length
;
476 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
478 return MEMCACHED_WRITE_FAILURE
;
480 if (cmd_size
+ server
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
482 memcached_io_write(server
, NULL
, 0, true);
486 struct __write_vector_st vector
[]=
488 { .length
= send_length
, .buffer
= request
.bytes
},
489 { .length
= ptr
->prefix_key_length
, .buffer
= ptr
->prefix_key
},
490 { .length
= key_length
, .buffer
= key
},
491 { .length
= value_length
, .buffer
= value
}
494 /* write the header */
495 memcached_return_t rc
;
496 if ((rc
= memcached_vdo(server
, vector
, 4, flush
)) != MEMCACHED_SUCCESS
)
498 memcached_io_reset(server
);
499 return (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
502 unlikely (verb
== SET_OP
&& ptr
->number_of_replicas
> 0)
504 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
506 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; x
++)
508 memcached_server_write_instance_st instance
;
511 if (server_key
== memcached_server_count(ptr
))
514 instance
= memcached_server_instance_fetch(ptr
, server_key
);
516 if (memcached_vdo(instance
, vector
, 4, false) != MEMCACHED_SUCCESS
)
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
);