10 #include "memcached_io.h"
19 } memcached_storage_action
;
22 static 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_server_st
* server
,
53 memcached_storage_action verb
);
55 static inline memcached_return
memcached_send(memcached_st
*ptr
,
56 const char *master_key
, size_t master_key_length
,
57 const char *key
, size_t key_length
,
58 const char *value
, size_t value_length
,
62 memcached_storage_action verb
)
68 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
69 unsigned int server_key
;
71 WATCHPOINT_ASSERT(!(value
== NULL
&& value_length
> 0));
73 rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
74 unlikely (rc
!= MEMCACHED_SUCCESS
)
77 unlikely (ptr
->number_of_hosts
== 0)
78 return MEMCACHED_NO_SERVERS
;
80 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test((char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
81 return MEMCACHED_BAD_KEY_PROVIDED
;
83 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
85 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
86 return memcached_send_binary(&ptr
->hosts
[server_key
], key
, key_length
,
87 value
, value_length
, expiration
,
91 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
92 "%s %s%.*s %u %llu %zu %llu%s\r\n",
93 storage_op_string(verb
),
95 (int)key_length
, key
, flags
,
96 (unsigned long long)expiration
, value_length
,
97 (unsigned long long)cas
,
98 (ptr
->flags
& MEM_NOREPLY
) ? " noreply" : "");
100 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
101 "%s %s%.*s %u %llu %zu%s\r\n",
102 storage_op_string(verb
),
104 (int)key_length
, key
, flags
,
105 (unsigned long long)expiration
, value_length
,
106 (ptr
->flags
& MEM_NOREPLY
) ? " noreply" : "");
108 if (ptr
->flags
& MEM_USE_UDP
&& ptr
->flags
& MEM_BUFFER_REQUESTS
) {
109 size_t cmd_size
= write_length
+ value_length
+ 2;
110 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
111 return MEMCACHED_WRITE_FAILURE
;
112 if (cmd_size
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
113 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
116 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
118 rc
= MEMCACHED_WRITE_FAILURE
;
122 /* Send command header */
123 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
124 if (rc
!= MEMCACHED_SUCCESS
)
127 /* Send command body */
128 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
130 rc
= MEMCACHED_WRITE_FAILURE
;
134 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
139 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
141 rc
= MEMCACHED_WRITE_FAILURE
;
145 if (ptr
->flags
& MEM_NOREPLY
)
146 return (to_write
== 0) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
149 return MEMCACHED_BUFFERED
;
151 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
153 if (rc
== MEMCACHED_STORED
)
154 return MEMCACHED_SUCCESS
;
159 memcached_io_reset(&ptr
->hosts
[server_key
]);
165 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
166 const char *value
, size_t value_length
,
171 LIBMEMCACHED_MEMCACHED_SET_START();
172 rc
= memcached_send(ptr
, key
, key_length
,
173 key
, key_length
, value
, value_length
,
174 expiration
, flags
, 0, SET_OP
);
175 LIBMEMCACHED_MEMCACHED_SET_END();
179 memcached_return
memcached_add(memcached_st
*ptr
,
180 const char *key
, size_t key_length
,
181 const char *value
, size_t value_length
,
186 LIBMEMCACHED_MEMCACHED_ADD_START();
187 rc
= memcached_send(ptr
, key
, key_length
,
188 key
, key_length
, value
, value_length
,
189 expiration
, flags
, 0, ADD_OP
);
190 LIBMEMCACHED_MEMCACHED_ADD_END();
194 memcached_return
memcached_replace(memcached_st
*ptr
,
195 const char *key
, size_t key_length
,
196 const char *value
, size_t value_length
,
201 LIBMEMCACHED_MEMCACHED_REPLACE_START();
202 rc
= memcached_send(ptr
, key
, key_length
,
203 key
, key_length
, value
, value_length
,
204 expiration
, flags
, 0, REPLACE_OP
);
205 LIBMEMCACHED_MEMCACHED_REPLACE_END();
209 memcached_return
memcached_prepend(memcached_st
*ptr
,
210 const char *key
, size_t key_length
,
211 const char *value
, size_t value_length
,
216 rc
= memcached_send(ptr
, key
, key_length
,
217 key
, key_length
, value
, value_length
,
218 expiration
, flags
, 0, PREPEND_OP
);
222 memcached_return
memcached_append(memcached_st
*ptr
,
223 const char *key
, size_t key_length
,
224 const char *value
, size_t value_length
,
229 rc
= memcached_send(ptr
, key
, key_length
,
230 key
, key_length
, value
, value_length
,
231 expiration
, flags
, 0, APPEND_OP
);
235 memcached_return
memcached_cas(memcached_st
*ptr
,
236 const char *key
, size_t key_length
,
237 const char *value
, size_t value_length
,
243 rc
= memcached_send(ptr
, key
, key_length
,
244 key
, key_length
, value
, value_length
,
245 expiration
, flags
, cas
, CAS_OP
);
249 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
250 const char *master_key
__attribute__((unused
)),
251 size_t master_key_length
__attribute__((unused
)),
252 const char *key
, size_t key_length
,
253 const char *value
, size_t value_length
,
258 LIBMEMCACHED_MEMCACHED_SET_START();
259 rc
= memcached_send(ptr
, master_key
, master_key_length
,
260 key
, key_length
, value
, value_length
,
261 expiration
, flags
, 0, SET_OP
);
262 LIBMEMCACHED_MEMCACHED_SET_END();
266 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
267 const char *master_key
, size_t master_key_length
,
268 const char *key
, size_t key_length
,
269 const char *value
, size_t value_length
,
274 LIBMEMCACHED_MEMCACHED_ADD_START();
275 rc
= memcached_send(ptr
, master_key
, master_key_length
,
276 key
, key_length
, value
, value_length
,
277 expiration
, flags
, 0, ADD_OP
);
278 LIBMEMCACHED_MEMCACHED_ADD_END();
282 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
283 const char *master_key
, size_t master_key_length
,
284 const char *key
, size_t key_length
,
285 const char *value
, size_t value_length
,
290 LIBMEMCACHED_MEMCACHED_REPLACE_START();
291 rc
= memcached_send(ptr
, master_key
, master_key_length
,
292 key
, key_length
, value
, value_length
,
293 expiration
, flags
, 0, REPLACE_OP
);
294 LIBMEMCACHED_MEMCACHED_REPLACE_END();
298 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
299 const char *master_key
, size_t master_key_length
,
300 const char *key
, size_t key_length
,
301 const char *value
, size_t value_length
,
306 rc
= memcached_send(ptr
, master_key
, master_key_length
,
307 key
, key_length
, value
, value_length
,
308 expiration
, flags
, 0, PREPEND_OP
);
312 memcached_return
memcached_append_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
,
320 rc
= memcached_send(ptr
, master_key
, master_key_length
,
321 key
, key_length
, value
, value_length
,
322 expiration
, flags
, 0, APPEND_OP
);
326 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
327 const char *master_key
, size_t master_key_length
,
328 const char *key
, size_t key_length
,
329 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
, cas
, CAS_OP
);
341 static inline uint8_t get_com_code(memcached_storage_action verb
, bool noreply
) {
348 ret
=PROTOCOL_BINARY_CMD_SETQ
;
351 ret
=PROTOCOL_BINARY_CMD_ADDQ
;
353 case CAS_OP
: /* FALLTHROUGH */
355 ret
=PROTOCOL_BINARY_CMD_REPLACEQ
;
358 ret
=PROTOCOL_BINARY_CMD_APPENDQ
;
361 ret
=PROTOCOL_BINARY_CMD_PREPENDQ
;
368 ret
=PROTOCOL_BINARY_CMD_SET
;
371 ret
=PROTOCOL_BINARY_CMD_ADD
;
373 case CAS_OP
: /* FALLTHROUGH */
375 ret
=PROTOCOL_BINARY_CMD_REPLACE
;
378 ret
=PROTOCOL_BINARY_CMD_APPEND
;
381 ret
=PROTOCOL_BINARY_CMD_PREPEND
;
390 static memcached_return
memcached_send_binary(memcached_server_st
* server
,
398 memcached_storage_action verb
)
401 protocol_binary_request_set request
= {.bytes
= {0}};
402 size_t send_length
= sizeof(request
.bytes
);
403 bool noreply
= server
->root
->flags
& MEM_NOREPLY
;
405 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
406 request
.message
.header
.request
.opcode
= get_com_code(verb
, noreply
);
407 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
408 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
409 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
410 send_length
-= 8; /* append & prepend does not contain extras! */
413 request
.message
.header
.request
.extlen
= 8;
414 request
.message
.body
.flags
= htonl(flags
);
415 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
418 request
.message
.header
.request
.bodylen
= htonl(key_length
+ value_length
+
419 request
.message
.header
.request
.extlen
);
422 request
.message
.header
.request
.cas
= htonll(cas
);
424 flush
= ((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1;
426 if ((server
->root
->flags
& MEM_USE_UDP
) && !flush
)
428 size_t cmd_size
= send_length
+ key_length
+ value_length
;
429 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
430 return MEMCACHED_WRITE_FAILURE
;
431 if (cmd_size
+ server
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
432 memcached_io_write(server
,NULL
,0, 1);
435 /* write the header */
436 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
437 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
438 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
440 memcached_io_reset(server
);
441 return MEMCACHED_WRITE_FAILURE
;
445 return MEMCACHED_BUFFERED
;
448 return MEMCACHED_SUCCESS
;
450 return memcached_response(server
, NULL
, 0, NULL
);