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
.binary_protocol
);
76 unlikely (rc
!= MEMCACHED_SUCCESS
)
79 unlikely (ptr
->number_of_hosts
== 0)
80 return MEMCACHED_NO_SERVERS
;
82 if (ptr
->flags
.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
.binary_protocol
)
87 return memcached_send_binary(ptr
, master_key
, master_key_length
,
89 value
, value_length
, expiration
,
93 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
97 write_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
98 "%s %s%.*s %u %llu %zu %llu%s\r\n",
99 storage_op_string(verb
),
101 (int)key_length
, key
, flags
,
102 (unsigned long long)expiration
, value_length
,
103 (unsigned long long)cas
,
104 (ptr
->flags
.no_reply
) ? " noreply" : "");
108 char *buffer_ptr
= buffer
;
109 const char *command
= storage_op_string(verb
);
111 /* Copy in the command, no space needed, we handle that in the command function*/
112 memcpy(buffer_ptr
, command
, strlen(command
));
114 /* Copy in the key prefix, switch to the buffer_ptr */
115 buffer_ptr
= memcpy(buffer_ptr
+ strlen(command
) , ptr
->prefix_key
, strlen(ptr
->prefix_key
));
117 /* Copy in the key, adjust point if a key prefix was used. */
118 buffer_ptr
= memcpy(buffer_ptr
+ (ptr
->prefix_key
? strlen(ptr
->prefix_key
) : 0),
120 buffer_ptr
+= key_length
;
124 write_length
= (size_t)(buffer_ptr
- buffer
);
125 write_length
+= (size_t) snprintf(buffer_ptr
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
128 (unsigned long long)expiration
, value_length
,
129 ptr
->flags
.no_reply
? " noreply" : "");
132 if (ptr
->flags
.use_udp
&& ptr
->flags
.buffer_requests
)
134 size_t cmd_size
= write_length
+ value_length
+ 2;
135 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
136 return MEMCACHED_WRITE_FAILURE
;
137 if (cmd_size
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
138 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
141 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
143 rc
= MEMCACHED_WRITE_FAILURE
;
147 /* Send command header */
148 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
149 if (rc
!= MEMCACHED_SUCCESS
)
152 /* Send command body */
153 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
155 rc
= MEMCACHED_WRITE_FAILURE
;
159 if (ptr
->flags
.buffer_requests
&& verb
== SET_OP
)
168 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
170 rc
= MEMCACHED_WRITE_FAILURE
;
174 if (ptr
->flags
.no_reply
)
175 return (to_write
== 0) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
178 return MEMCACHED_BUFFERED
;
180 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
182 if (rc
== MEMCACHED_STORED
)
183 return MEMCACHED_SUCCESS
;
188 memcached_io_reset(&ptr
->hosts
[server_key
]);
194 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
195 const char *value
, size_t value_length
,
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
memcached_add(memcached_st
*ptr
,
209 const char *key
, size_t key_length
,
210 const char *value
, size_t value_length
,
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
memcached_replace(memcached_st
*ptr
,
224 const char *key
, size_t key_length
,
225 const char *value
, size_t value_length
,
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
memcached_prepend(memcached_st
*ptr
,
239 const char *key
, size_t key_length
,
240 const char *value
, size_t value_length
,
245 rc
= memcached_send(ptr
, key
, key_length
,
246 key
, key_length
, value
, value_length
,
247 expiration
, flags
, 0, PREPEND_OP
);
251 memcached_return
memcached_append(memcached_st
*ptr
,
252 const char *key
, size_t key_length
,
253 const char *value
, size_t value_length
,
258 rc
= memcached_send(ptr
, key
, key_length
,
259 key
, key_length
, value
, value_length
,
260 expiration
, flags
, 0, APPEND_OP
);
264 memcached_return
memcached_cas(memcached_st
*ptr
,
265 const char *key
, size_t key_length
,
266 const char *value
, size_t value_length
,
272 rc
= memcached_send(ptr
, key
, key_length
,
273 key
, key_length
, value
, value_length
,
274 expiration
, flags
, cas
, CAS_OP
);
278 memcached_return
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
,
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
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
,
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
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
,
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
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
,
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
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
,
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
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
,
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 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
memcached_send_binary(memcached_st
*ptr
,
429 const char *master_key
,
430 size_t master_key_length
,
438 memcached_storage_action 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(ptr
, master_key
,
445 memcached_server_st
*server
= &ptr
->hosts
[server_key
];
446 bool noreply
= server
->root
->flags
.no_reply
;
448 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
449 request
.message
.header
.request
.opcode
= get_com_code(verb
, noreply
);
450 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
451 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
452 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
453 send_length
-= 8; /* append & prepend does not contain extras! */
456 request
.message
.header
.request
.extlen
= 8;
457 request
.message
.body
.flags
= htonl(flags
);
458 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
461 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ value_length
+
462 request
.message
.header
.request
.extlen
));
465 request
.message
.header
.request
.cas
= htonll(cas
);
467 flush
= (uint8_t) ((server
->root
->flags
.buffer_requests
&& verb
== SET_OP
) ? 0 : 1);
469 if (server
->root
->flags
.use_udp
&& !flush
)
471 size_t cmd_size
= send_length
+ key_length
+ value_length
;
473 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
474 return MEMCACHED_WRITE_FAILURE
;
475 if (cmd_size
+ server
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
476 memcached_io_write(server
,NULL
,0, 1);
479 /* write the header */
480 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
481 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
482 (memcached_io_write(server
, value
, value_length
, (char) flush
) == -1))
484 memcached_io_reset(server
);
485 return MEMCACHED_WRITE_FAILURE
;
488 unlikely (verb
== SET_OP
&& ptr
->number_of_replicas
> 0)
490 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SETQ
;
492 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; x
++)
495 if (server_key
== ptr
->number_of_hosts
)
498 memcached_server_st
*srv
= &ptr
->hosts
[server_key
];
499 if ((memcached_do(srv
, (const char*)request
.bytes
,
500 send_length
, 0) != MEMCACHED_SUCCESS
) ||
501 (memcached_io_write(srv
, key
, key_length
, 0) == -1) ||
502 (memcached_io_write(srv
, value
, value_length
, (char) flush
) == -1))
503 memcached_io_reset(srv
);
505 memcached_server_response_decrement(srv
);
510 return MEMCACHED_BUFFERED
;
513 return MEMCACHED_SUCCESS
;
515 return memcached_response(server
, NULL
, 0, NULL
);