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
];
72 unsigned int server_key
;
74 WATCHPOINT_ASSERT(!(value
== NULL
&& value_length
> 0));
76 rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
77 unlikely (rc
!= MEMCACHED_SUCCESS
)
80 unlikely (ptr
->number_of_hosts
== 0)
81 return MEMCACHED_NO_SERVERS
;
83 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
84 return MEMCACHED_BAD_KEY_PROVIDED
;
86 if (ptr
->flags
.binary_protocol
)
88 return memcached_send_binary(ptr
, master_key
, master_key_length
,
90 value
, value_length
, expiration
,
94 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
98 write_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
99 "%s %s%.*s %u %llu %zu %llu%s\r\n",
100 storage_op_string(verb
),
102 (int)key_length
, key
, flags
,
103 (unsigned long long)expiration
, value_length
,
104 (unsigned long long)cas
,
105 (ptr
->flags
.no_reply
) ? " noreply" : "");
109 char *buffer_ptr
= buffer
;
110 const char *command
= storage_op_string(verb
);
112 /* Copy in the command, no space needed, we handle that in the command function*/
113 memcpy(buffer_ptr
, command
, strlen(command
));
115 /* Copy in the key prefix, switch to the buffer_ptr */
116 buffer_ptr
= memcpy((buffer_ptr
+ strlen(command
)), ptr
->prefix_key
, strlen(ptr
->prefix_key
));
118 /* Copy in the key, adjust point if a key prefix was used. */
119 buffer_ptr
= memcpy(buffer_ptr
+ (ptr
->prefix_key
? strlen(ptr
->prefix_key
) : 0),
121 buffer_ptr
+= key_length
;
125 write_length
= (size_t)(buffer_ptr
- buffer
);
126 write_length
+= (size_t) snprintf(buffer_ptr
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
129 (unsigned long long)expiration
, value_length
,
130 ptr
->flags
.no_reply
? " noreply" : "");
133 if (ptr
->flags
.use_udp
&& ptr
->flags
.buffer_requests
)
135 size_t cmd_size
= write_length
+ value_length
+ 2;
136 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
137 return MEMCACHED_WRITE_FAILURE
;
138 if (cmd_size
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
139 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
142 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
144 rc
= MEMCACHED_WRITE_FAILURE
;
148 /* Send command header */
149 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
150 if (rc
!= MEMCACHED_SUCCESS
)
153 /* Send command body */
154 if (memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0) == -1)
156 rc
= MEMCACHED_WRITE_FAILURE
;
160 if (ptr
->flags
.buffer_requests
&& verb
== SET_OP
)
169 if (memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
) == -1)
171 rc
= MEMCACHED_WRITE_FAILURE
;
175 if (ptr
->flags
.no_reply
)
176 return (to_write
== 0) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
179 return MEMCACHED_BUFFERED
;
181 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
183 if (rc
== MEMCACHED_STORED
)
184 return MEMCACHED_SUCCESS
;
189 memcached_io_reset(&ptr
->hosts
[server_key
]);
195 memcached_return_t
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
196 const char *value
, size_t value_length
,
200 memcached_return_t rc
;
201 LIBMEMCACHED_MEMCACHED_SET_START();
202 rc
= memcached_send(ptr
, key
, key_length
,
203 key
, key_length
, value
, value_length
,
204 expiration
, flags
, 0, SET_OP
);
205 LIBMEMCACHED_MEMCACHED_SET_END();
209 memcached_return_t
memcached_add(memcached_st
*ptr
,
210 const char *key
, size_t key_length
,
211 const char *value
, size_t value_length
,
215 memcached_return_t rc
;
216 LIBMEMCACHED_MEMCACHED_ADD_START();
217 rc
= memcached_send(ptr
, key
, key_length
,
218 key
, key_length
, value
, value_length
,
219 expiration
, flags
, 0, ADD_OP
);
220 LIBMEMCACHED_MEMCACHED_ADD_END();
224 memcached_return_t
memcached_replace(memcached_st
*ptr
,
225 const char *key
, size_t key_length
,
226 const char *value
, size_t value_length
,
230 memcached_return_t rc
;
231 LIBMEMCACHED_MEMCACHED_REPLACE_START();
232 rc
= memcached_send(ptr
, key
, key_length
,
233 key
, key_length
, value
, value_length
,
234 expiration
, flags
, 0, REPLACE_OP
);
235 LIBMEMCACHED_MEMCACHED_REPLACE_END();
239 memcached_return_t
memcached_prepend(memcached_st
*ptr
,
240 const char *key
, size_t key_length
,
241 const char *value
, size_t value_length
,
245 memcached_return_t rc
;
246 rc
= memcached_send(ptr
, key
, key_length
,
247 key
, key_length
, value
, value_length
,
248 expiration
, flags
, 0, PREPEND_OP
);
252 memcached_return_t
memcached_append(memcached_st
*ptr
,
253 const char *key
, size_t key_length
,
254 const char *value
, size_t value_length
,
258 memcached_return_t rc
;
259 rc
= memcached_send(ptr
, key
, key_length
,
260 key
, key_length
, value
, value_length
,
261 expiration
, flags
, 0, APPEND_OP
);
265 memcached_return_t
memcached_cas(memcached_st
*ptr
,
266 const char *key
, size_t key_length
,
267 const char *value
, size_t value_length
,
272 memcached_return_t rc
;
273 rc
= memcached_send(ptr
, key
, key_length
,
274 key
, key_length
, value
, value_length
,
275 expiration
, flags
, cas
, CAS_OP
);
279 memcached_return_t
memcached_set_by_key(memcached_st
*ptr
,
280 const char *master_key
__attribute__((unused
)),
281 size_t master_key_length
__attribute__((unused
)),
282 const char *key
, size_t key_length
,
283 const char *value
, size_t value_length
,
287 memcached_return_t rc
;
288 LIBMEMCACHED_MEMCACHED_SET_START();
289 rc
= memcached_send(ptr
, master_key
, master_key_length
,
290 key
, key_length
, value
, value_length
,
291 expiration
, flags
, 0, SET_OP
);
292 LIBMEMCACHED_MEMCACHED_SET_END();
296 memcached_return_t
memcached_add_by_key(memcached_st
*ptr
,
297 const char *master_key
, size_t master_key_length
,
298 const char *key
, size_t key_length
,
299 const char *value
, size_t value_length
,
303 memcached_return_t rc
;
304 LIBMEMCACHED_MEMCACHED_ADD_START();
305 rc
= memcached_send(ptr
, master_key
, master_key_length
,
306 key
, key_length
, value
, value_length
,
307 expiration
, flags
, 0, ADD_OP
);
308 LIBMEMCACHED_MEMCACHED_ADD_END();
312 memcached_return_t
memcached_replace_by_key(memcached_st
*ptr
,
313 const char *master_key
, size_t master_key_length
,
314 const char *key
, size_t key_length
,
315 const char *value
, size_t value_length
,
319 memcached_return_t rc
;
320 LIBMEMCACHED_MEMCACHED_REPLACE_START();
321 rc
= memcached_send(ptr
, master_key
, master_key_length
,
322 key
, key_length
, value
, value_length
,
323 expiration
, flags
, 0, REPLACE_OP
);
324 LIBMEMCACHED_MEMCACHED_REPLACE_END();
328 memcached_return_t
memcached_prepend_by_key(memcached_st
*ptr
,
329 const char *master_key
, size_t master_key_length
,
330 const char *key
, size_t key_length
,
331 const char *value
, size_t value_length
,
335 memcached_return_t rc
;
336 rc
= memcached_send(ptr
, master_key
, master_key_length
,
337 key
, key_length
, value
, value_length
,
338 expiration
, flags
, 0, PREPEND_OP
);
342 memcached_return_t
memcached_append_by_key(memcached_st
*ptr
,
343 const char *master_key
, size_t master_key_length
,
344 const char *key
, size_t key_length
,
345 const char *value
, size_t value_length
,
349 memcached_return_t rc
;
350 rc
= memcached_send(ptr
, master_key
, master_key_length
,
351 key
, key_length
, value
, value_length
,
352 expiration
, flags
, 0, APPEND_OP
);
356 memcached_return_t
memcached_cas_by_key(memcached_st
*ptr
,
357 const char *master_key
, size_t master_key_length
,
358 const char *key
, size_t key_length
,
359 const char *value
, size_t value_length
,
364 memcached_return_t rc
;
365 rc
= memcached_send(ptr
, master_key
, master_key_length
,
366 key
, key_length
, value
, value_length
,
367 expiration
, flags
, cas
, CAS_OP
);
371 static inline uint8_t get_com_code(memcached_storage_action_t verb
, bool noreply
)
373 /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
374 * be used uninitialized in this function. FAIL */
381 ret
=PROTOCOL_BINARY_CMD_SETQ
;
384 ret
=PROTOCOL_BINARY_CMD_ADDQ
;
386 case CAS_OP
: /* FALLTHROUGH */
388 ret
=PROTOCOL_BINARY_CMD_REPLACEQ
;
391 ret
=PROTOCOL_BINARY_CMD_APPENDQ
;
394 ret
=PROTOCOL_BINARY_CMD_PREPENDQ
;
397 WATCHPOINT_ASSERT(verb
);
404 ret
=PROTOCOL_BINARY_CMD_SET
;
407 ret
=PROTOCOL_BINARY_CMD_ADD
;
409 case CAS_OP
: /* FALLTHROUGH */
411 ret
=PROTOCOL_BINARY_CMD_REPLACE
;
414 ret
=PROTOCOL_BINARY_CMD_APPEND
;
417 ret
=PROTOCOL_BINARY_CMD_PREPEND
;
420 WATCHPOINT_ASSERT(verb
);
429 static memcached_return_t
memcached_send_binary(memcached_st
*ptr
,
430 const char *master_key
,
431 size_t master_key_length
,
439 memcached_storage_action_t verb
)
442 protocol_binary_request_set request
= {.bytes
= {0}};
443 size_t send_length
= sizeof(request
.bytes
);
444 uint32_t server_key
= memcached_generate_hash(ptr
, master_key
,
446 memcached_server_st
*server
= &ptr
->hosts
[server_key
];
447 bool noreply
= server
->root
->flags
.no_reply
;
449 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
450 request
.message
.header
.request
.opcode
= get_com_code(verb
, noreply
);
451 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
452 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
453 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
454 send_length
-= 8; /* append & prepend does not contain extras! */
457 request
.message
.header
.request
.extlen
= 8;
458 request
.message
.body
.flags
= htonl(flags
);
459 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
462 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ value_length
+
463 request
.message
.header
.request
.extlen
));
466 request
.message
.header
.request
.cas
= htonll(cas
);
468 flush
= (uint8_t) ((server
->root
->flags
.buffer_requests
&& verb
== SET_OP
) ? 0 : 1);
470 if (server
->root
->flags
.use_udp
&& !flush
)
472 size_t cmd_size
= send_length
+ key_length
+ value_length
;
474 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
475 return MEMCACHED_WRITE_FAILURE
;
476 if (cmd_size
+ server
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
477 memcached_io_write(server
,NULL
,0, 1);
480 /* write the header */
481 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
482 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
483 (memcached_io_write(server
, value
, value_length
, (char) flush
) == -1))
485 memcached_io_reset(server
);
486 return MEMCACHED_WRITE_FAILURE
;
489 unlikely (verb
== SET_OP
&& ptr
->number_of_replicas
> 0)
491 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
493 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; x
++)
496 if (server_key
== ptr
->number_of_hosts
)
499 memcached_server_st
*srv
= &ptr
->hosts
[server_key
];
500 if ((memcached_do(srv
, (const char*)request
.bytes
,
501 send_length
, 0) != MEMCACHED_SUCCESS
) ||
502 (memcached_io_write(srv
, key
, key_length
, 0) == -1) ||
503 (memcached_io_write(srv
, value
, value_length
, (char) flush
) == -1))
504 memcached_io_reset(srv
);
506 memcached_server_response_decrement(srv
);
511 return MEMCACHED_BUFFERED
;
514 return MEMCACHED_SUCCESS
;
516 return memcached_response(server
, NULL
, 0, NULL
);