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\r\n", storage_op_string(verb
),
94 (int)key_length
, key
, flags
,
95 (unsigned long long)expiration
, value_length
,
96 (unsigned long long)cas
);
98 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
99 "%s %s%.*s %u %llu %zu\r\n", storage_op_string(verb
),
101 (int)key_length
, key
, flags
,
102 (unsigned long long)expiration
, value_length
);
104 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
106 rc
= MEMCACHED_WRITE_FAILURE
;
110 /* Send command header */
111 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
112 if (rc
!= MEMCACHED_SUCCESS
)
115 /* Send command body */
116 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
118 rc
= MEMCACHED_WRITE_FAILURE
;
122 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
127 if (ptr
->flags
& MEM_NOREPLY
)
129 if (memcached_io_write(&ptr
->hosts
[server_key
], " noreply\r\n",
132 rc
= MEMCACHED_WRITE_FAILURE
;
136 memcached_server_response_decrement(&ptr
->hosts
[server_key
]);
137 return MEMCACHED_SUCCESS
;
140 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
142 rc
= MEMCACHED_WRITE_FAILURE
;
147 return MEMCACHED_BUFFERED
;
149 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
151 if (rc
== MEMCACHED_STORED
)
152 return MEMCACHED_SUCCESS
;
157 memcached_io_reset(&ptr
->hosts
[server_key
]);
163 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
164 const char *value
, size_t value_length
,
169 LIBMEMCACHED_MEMCACHED_SET_START();
170 rc
= memcached_send(ptr
, key
, key_length
,
171 key
, key_length
, value
, value_length
,
172 expiration
, flags
, 0, SET_OP
);
173 LIBMEMCACHED_MEMCACHED_SET_END();
177 memcached_return
memcached_add(memcached_st
*ptr
,
178 const char *key
, size_t key_length
,
179 const char *value
, size_t value_length
,
184 LIBMEMCACHED_MEMCACHED_ADD_START();
185 rc
= memcached_send(ptr
, key
, key_length
,
186 key
, key_length
, value
, value_length
,
187 expiration
, flags
, 0, ADD_OP
);
188 LIBMEMCACHED_MEMCACHED_ADD_END();
192 memcached_return
memcached_replace(memcached_st
*ptr
,
193 const char *key
, size_t key_length
,
194 const char *value
, size_t value_length
,
199 LIBMEMCACHED_MEMCACHED_REPLACE_START();
200 rc
= memcached_send(ptr
, key
, key_length
,
201 key
, key_length
, value
, value_length
,
202 expiration
, flags
, 0, REPLACE_OP
);
203 LIBMEMCACHED_MEMCACHED_REPLACE_END();
207 memcached_return
memcached_prepend(memcached_st
*ptr
,
208 const char *key
, size_t key_length
,
209 const char *value
, size_t value_length
,
214 rc
= memcached_send(ptr
, key
, key_length
,
215 key
, key_length
, value
, value_length
,
216 expiration
, flags
, 0, PREPEND_OP
);
220 memcached_return
memcached_append(memcached_st
*ptr
,
221 const char *key
, size_t key_length
,
222 const char *value
, size_t value_length
,
227 rc
= memcached_send(ptr
, key
, key_length
,
228 key
, key_length
, value
, value_length
,
229 expiration
, flags
, 0, APPEND_OP
);
233 memcached_return
memcached_cas(memcached_st
*ptr
,
234 const char *key
, size_t key_length
,
235 const char *value
, size_t value_length
,
241 rc
= memcached_send(ptr
, key
, key_length
,
242 key
, key_length
, value
, value_length
,
243 expiration
, flags
, cas
, CAS_OP
);
247 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
248 const char *master_key
__attribute__((unused
)),
249 size_t master_key_length
__attribute__((unused
)),
250 const char *key
, size_t key_length
,
251 const char *value
, size_t value_length
,
256 LIBMEMCACHED_MEMCACHED_SET_START();
257 rc
= memcached_send(ptr
, master_key
, master_key_length
,
258 key
, key_length
, value
, value_length
,
259 expiration
, flags
, 0, SET_OP
);
260 LIBMEMCACHED_MEMCACHED_SET_END();
264 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
265 const char *master_key
, size_t master_key_length
,
266 const char *key
, size_t key_length
,
267 const char *value
, size_t value_length
,
272 LIBMEMCACHED_MEMCACHED_ADD_START();
273 rc
= memcached_send(ptr
, master_key
, master_key_length
,
274 key
, key_length
, value
, value_length
,
275 expiration
, flags
, 0, ADD_OP
);
276 LIBMEMCACHED_MEMCACHED_ADD_END();
280 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
281 const char *master_key
, size_t master_key_length
,
282 const char *key
, size_t key_length
,
283 const char *value
, size_t value_length
,
288 LIBMEMCACHED_MEMCACHED_REPLACE_START();
289 rc
= memcached_send(ptr
, master_key
, master_key_length
,
290 key
, key_length
, value
, value_length
,
291 expiration
, flags
, 0, REPLACE_OP
);
292 LIBMEMCACHED_MEMCACHED_REPLACE_END();
296 memcached_return
memcached_prepend_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
,
304 rc
= memcached_send(ptr
, master_key
, master_key_length
,
305 key
, key_length
, value
, value_length
,
306 expiration
, flags
, 0, PREPEND_OP
);
310 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
311 const char *master_key
, size_t master_key_length
,
312 const char *key
, size_t key_length
,
313 const char *value
, size_t value_length
,
318 rc
= memcached_send(ptr
, master_key
, master_key_length
,
319 key
, key_length
, value
, value_length
,
320 expiration
, flags
, 0, APPEND_OP
);
324 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
325 const char *master_key
, size_t master_key_length
,
326 const char *key
, size_t key_length
,
327 const char *value
, size_t value_length
,
333 rc
= memcached_send(ptr
, master_key
, master_key_length
,
334 key
, key_length
, value
, value_length
,
335 expiration
, flags
, cas
, CAS_OP
);
339 static memcached_return
memcached_send_binary(memcached_server_st
* server
,
347 memcached_storage_action verb
)
350 protocol_binary_request_set request
= {.bytes
= {0}};
351 size_t send_length
= sizeof(request
.bytes
);
353 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
357 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
360 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_ADD
;
363 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
366 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_APPEND
;
369 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_PREPEND
;
372 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
376 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
377 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
378 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
379 send_length
-= 8; /* append & prepend does not contain extras! */
382 request
.message
.header
.request
.extlen
= 8;
383 request
.message
.body
.flags
= htonl(flags
);
384 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
387 request
.message
.header
.request
.bodylen
= htonl(key_length
+ value_length
+
388 request
.message
.header
.request
.extlen
);
391 request
.message
.header
.request
.cas
= htonll(cas
);
393 flush
= ((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1;
395 /* The binary protocol does not implement NOREPLY right now, so I need to
396 * parse the return message from the server. The problem we tried to solve
397 * is the fact that the async mode will buffer commands, and if we issue a
398 * lot of sets followed by a get we have to flush the send buffer before we
399 * can send the get command (and all those commands have to be executed).
400 * The workaround for the binary is that we send the command, but we await
401 * parsing of the result message until we really need to do it..
403 if (server
->root
->flags
& MEM_NOREPLY
)
406 /* write the header */
407 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
408 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
409 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
411 memcached_io_reset(server
);
412 return MEMCACHED_WRITE_FAILURE
;
415 if (flush
== 0 || server
->root
->flags
& MEM_NOREPLY
)
416 return MEMCACHED_BUFFERED
;
418 return memcached_response(server
, NULL
, 0, NULL
);