10 #include "memcached_io.h"
19 } memcached_storage_action
;
22 static const char *storage_op_string(memcached_storage_action verb
)
39 return "tosserror"; /* This is impossible, fixes issue for compiler warning in VisualStudio */
45 static memcached_return
memcached_send_binary(memcached_st
*ptr
,
46 const char *master_key
,
47 size_t master_key_length
,
55 memcached_storage_action verb
);
57 static inline memcached_return
memcached_send(memcached_st
*ptr
,
58 const char *master_key
, size_t master_key_length
,
59 const char *key
, size_t key_length
,
60 const char *value
, size_t value_length
,
64 memcached_storage_action verb
)
70 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
71 unsigned int server_key
;
73 WATCHPOINT_ASSERT(!(value
== NULL
&& value_length
> 0));
75 rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
76 unlikely (rc
!= MEMCACHED_SUCCESS
)
79 unlikely (ptr
->number_of_hosts
== 0)
80 return MEMCACHED_NO_SERVERS
;
82 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
83 return MEMCACHED_BAD_KEY_PROVIDED
;
85 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
86 return memcached_send_binary(ptr
, master_key
, master_key_length
,
88 value
, value_length
, expiration
,
91 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
94 write_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
95 "%s %s%.*s %u %llu %zu %llu%s\r\n",
96 storage_op_string(verb
),
98 (int)key_length
, key
, flags
,
99 (unsigned long long)expiration
, value_length
,
100 (unsigned long long)cas
,
101 (ptr
->flags
& MEM_NOREPLY
) ? " noreply" : "");
104 char *buffer_ptr
= buffer
;
105 const char *command
= storage_op_string(verb
);
107 /* Copy in the command, no space needed, we handle that in the command function*/
108 memcpy(buffer_ptr
, command
, strlen(command
));
110 /* Copy in the key prefix, switch to the buffer_ptr */
111 buffer_ptr
= memcpy(buffer_ptr
+ strlen(command
) , ptr
->prefix_key
, strlen(ptr
->prefix_key
));
113 /* Copy in the key, adjust point if a key prefix was used. */
114 buffer_ptr
= memcpy(buffer_ptr
+ (ptr
->prefix_key
? strlen(ptr
->prefix_key
) : 0),
116 buffer_ptr
+= key_length
;
120 write_length
= (size_t)(buffer_ptr
- buffer
);
121 write_length
+= (size_t) snprintf(buffer_ptr
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
124 (unsigned long long)expiration
, value_length
,
125 (ptr
->flags
& MEM_NOREPLY
) ? " noreply" : "");
128 if (ptr
->flags
& MEM_USE_UDP
&& ptr
->flags
& MEM_BUFFER_REQUESTS
)
130 size_t cmd_size
= write_length
+ value_length
+ 2;
131 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
132 return MEMCACHED_WRITE_FAILURE
;
133 if (cmd_size
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
134 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
137 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
139 rc
= MEMCACHED_WRITE_FAILURE
;
143 /* Send command header */
144 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
145 if (rc
!= MEMCACHED_SUCCESS
)
148 /* Send command body */
149 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
151 rc
= MEMCACHED_WRITE_FAILURE
;
155 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
160 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
162 rc
= MEMCACHED_WRITE_FAILURE
;
166 if (ptr
->flags
& MEM_NOREPLY
)
167 return (to_write
== 0) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
170 return MEMCACHED_BUFFERED
;
172 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
174 if (rc
== MEMCACHED_STORED
)
175 return MEMCACHED_SUCCESS
;
180 memcached_io_reset(&ptr
->hosts
[server_key
]);
186 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
187 const char *value
, size_t value_length
,
192 LIBMEMCACHED_MEMCACHED_SET_START();
193 rc
= memcached_send(ptr
, key
, key_length
,
194 key
, key_length
, value
, value_length
,
195 expiration
, flags
, 0, SET_OP
);
196 LIBMEMCACHED_MEMCACHED_SET_END();
200 memcached_return
memcached_add(memcached_st
*ptr
,
201 const char *key
, size_t key_length
,
202 const char *value
, size_t value_length
,
207 LIBMEMCACHED_MEMCACHED_ADD_START();
208 rc
= memcached_send(ptr
, key
, key_length
,
209 key
, key_length
, value
, value_length
,
210 expiration
, flags
, 0, ADD_OP
);
211 LIBMEMCACHED_MEMCACHED_ADD_END();
215 memcached_return
memcached_replace(memcached_st
*ptr
,
216 const char *key
, size_t key_length
,
217 const char *value
, size_t value_length
,
222 LIBMEMCACHED_MEMCACHED_REPLACE_START();
223 rc
= memcached_send(ptr
, key
, key_length
,
224 key
, key_length
, value
, value_length
,
225 expiration
, flags
, 0, REPLACE_OP
);
226 LIBMEMCACHED_MEMCACHED_REPLACE_END();
230 memcached_return
memcached_prepend(memcached_st
*ptr
,
231 const char *key
, size_t key_length
,
232 const char *value
, size_t value_length
,
237 rc
= memcached_send(ptr
, key
, key_length
,
238 key
, key_length
, value
, value_length
,
239 expiration
, flags
, 0, PREPEND_OP
);
243 memcached_return
memcached_append(memcached_st
*ptr
,
244 const char *key
, size_t key_length
,
245 const char *value
, size_t value_length
,
250 rc
= memcached_send(ptr
, key
, key_length
,
251 key
, key_length
, value
, value_length
,
252 expiration
, flags
, 0, APPEND_OP
);
256 memcached_return
memcached_cas(memcached_st
*ptr
,
257 const char *key
, size_t key_length
,
258 const char *value
, size_t value_length
,
264 rc
= memcached_send(ptr
, key
, key_length
,
265 key
, key_length
, value
, value_length
,
266 expiration
, flags
, cas
, CAS_OP
);
270 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
271 const char *master_key
__attribute__((unused
)),
272 size_t master_key_length
__attribute__((unused
)),
273 const char *key
, size_t key_length
,
274 const char *value
, size_t value_length
,
279 LIBMEMCACHED_MEMCACHED_SET_START();
280 rc
= memcached_send(ptr
, master_key
, master_key_length
,
281 key
, key_length
, value
, value_length
,
282 expiration
, flags
, 0, SET_OP
);
283 LIBMEMCACHED_MEMCACHED_SET_END();
287 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
288 const char *master_key
, size_t master_key_length
,
289 const char *key
, size_t key_length
,
290 const char *value
, size_t value_length
,
295 LIBMEMCACHED_MEMCACHED_ADD_START();
296 rc
= memcached_send(ptr
, master_key
, master_key_length
,
297 key
, key_length
, value
, value_length
,
298 expiration
, flags
, 0, ADD_OP
);
299 LIBMEMCACHED_MEMCACHED_ADD_END();
303 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
304 const char *master_key
, size_t master_key_length
,
305 const char *key
, size_t key_length
,
306 const char *value
, size_t value_length
,
311 LIBMEMCACHED_MEMCACHED_REPLACE_START();
312 rc
= memcached_send(ptr
, master_key
, master_key_length
,
313 key
, key_length
, value
, value_length
,
314 expiration
, flags
, 0, REPLACE_OP
);
315 LIBMEMCACHED_MEMCACHED_REPLACE_END();
319 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
320 const char *master_key
, size_t master_key_length
,
321 const char *key
, size_t key_length
,
322 const char *value
, size_t value_length
,
327 rc
= memcached_send(ptr
, master_key
, master_key_length
,
328 key
, key_length
, value
, value_length
,
329 expiration
, flags
, 0, PREPEND_OP
);
333 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
334 const char *master_key
, size_t master_key_length
,
335 const char *key
, size_t key_length
,
336 const char *value
, size_t value_length
,
341 rc
= memcached_send(ptr
, master_key
, master_key_length
,
342 key
, key_length
, value
, value_length
,
343 expiration
, flags
, 0, APPEND_OP
);
347 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
348 const char *master_key
, size_t master_key_length
,
349 const char *key
, size_t key_length
,
350 const char *value
, size_t value_length
,
356 rc
= memcached_send(ptr
, master_key
, master_key_length
,
357 key
, key_length
, value
, value_length
,
358 expiration
, flags
, cas
, CAS_OP
);
362 static inline uint8_t get_com_code(memcached_storage_action verb
, bool noreply
)
364 /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
365 * be used uninitialized in this function. FAIL */
372 ret
=PROTOCOL_BINARY_CMD_SETQ
;
375 ret
=PROTOCOL_BINARY_CMD_ADDQ
;
377 case CAS_OP
: /* FALLTHROUGH */
379 ret
=PROTOCOL_BINARY_CMD_REPLACEQ
;
382 ret
=PROTOCOL_BINARY_CMD_APPENDQ
;
385 ret
=PROTOCOL_BINARY_CMD_PREPENDQ
;
388 WATCHPOINT_ASSERT(verb
);
395 ret
=PROTOCOL_BINARY_CMD_SET
;
398 ret
=PROTOCOL_BINARY_CMD_ADD
;
400 case CAS_OP
: /* FALLTHROUGH */
402 ret
=PROTOCOL_BINARY_CMD_REPLACE
;
405 ret
=PROTOCOL_BINARY_CMD_APPEND
;
408 ret
=PROTOCOL_BINARY_CMD_PREPEND
;
411 WATCHPOINT_ASSERT(verb
);
420 static memcached_return
memcached_send_binary(memcached_st
*ptr
,
421 const char *master_key
,
422 size_t master_key_length
,
430 memcached_storage_action verb
)
433 protocol_binary_request_set request
= {.bytes
= {0}};
434 size_t send_length
= sizeof(request
.bytes
);
435 uint32_t server_key
= memcached_generate_hash(ptr
, master_key
,
437 memcached_server_st
*server
= &ptr
->hosts
[server_key
];
438 bool noreply
= server
->root
->flags
& MEM_NOREPLY
;
440 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
441 request
.message
.header
.request
.opcode
= get_com_code(verb
, noreply
);
442 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
443 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
444 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
445 send_length
-= 8; /* append & prepend does not contain extras! */
448 request
.message
.header
.request
.extlen
= 8;
449 request
.message
.body
.flags
= htonl(flags
);
450 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
453 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ value_length
+
454 request
.message
.header
.request
.extlen
));
457 request
.message
.header
.request
.cas
= htonll(cas
);
459 flush
= (uint8_t) (((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1);
461 if ((server
->root
->flags
& MEM_USE_UDP
) && !flush
)
463 size_t cmd_size
= send_length
+ key_length
+ value_length
;
464 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
465 return MEMCACHED_WRITE_FAILURE
;
466 if (cmd_size
+ server
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
467 memcached_io_write(server
,NULL
,0, 1);
470 /* write the header */
471 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
472 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
473 (memcached_io_write(server
, value
, value_length
, (char) flush
) == -1))
475 memcached_io_reset(server
);
476 return MEMCACHED_WRITE_FAILURE
;
479 unlikely (verb
== SET_OP
&& ptr
->number_of_replicas
> 0)
481 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
483 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; x
++)
486 if (server_key
== ptr
->number_of_hosts
)
489 memcached_server_st
*srv
= &ptr
->hosts
[server_key
];
490 if ((memcached_do(srv
, (const char*)request
.bytes
,
491 send_length
, 0) != MEMCACHED_SUCCESS
) ||
492 (memcached_io_write(srv
, key
, key_length
, 0) == -1) ||
493 (memcached_io_write(srv
, value
, value_length
, (char) flush
) == -1))
494 memcached_io_reset(srv
);
496 memcached_server_response_decrement(srv
);
501 return MEMCACHED_BUFFERED
;
504 return MEMCACHED_SUCCESS
;
506 return memcached_response(server
, NULL
, 0, NULL
);