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 (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
110 rc
= MEMCACHED_WRITE_FAILURE
;
114 /* Send command header */
115 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
116 if (rc
!= MEMCACHED_SUCCESS
)
119 /* Send command body */
120 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
122 rc
= MEMCACHED_WRITE_FAILURE
;
126 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
131 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
133 rc
= MEMCACHED_WRITE_FAILURE
;
137 if (ptr
->flags
& MEM_NOREPLY
)
139 return (to_write
== 0) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
143 return MEMCACHED_BUFFERED
;
145 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
147 if (rc
== MEMCACHED_STORED
)
148 return MEMCACHED_SUCCESS
;
153 memcached_io_reset(&ptr
->hosts
[server_key
]);
159 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
160 const char *value
, size_t value_length
,
165 LIBMEMCACHED_MEMCACHED_SET_START();
166 rc
= memcached_send(ptr
, key
, key_length
,
167 key
, key_length
, value
, value_length
,
168 expiration
, flags
, 0, SET_OP
);
169 LIBMEMCACHED_MEMCACHED_SET_END();
173 memcached_return
memcached_add(memcached_st
*ptr
,
174 const char *key
, size_t key_length
,
175 const char *value
, size_t value_length
,
180 LIBMEMCACHED_MEMCACHED_ADD_START();
181 rc
= memcached_send(ptr
, key
, key_length
,
182 key
, key_length
, value
, value_length
,
183 expiration
, flags
, 0, ADD_OP
);
184 LIBMEMCACHED_MEMCACHED_ADD_END();
188 memcached_return
memcached_replace(memcached_st
*ptr
,
189 const char *key
, size_t key_length
,
190 const char *value
, size_t value_length
,
195 LIBMEMCACHED_MEMCACHED_REPLACE_START();
196 rc
= memcached_send(ptr
, key
, key_length
,
197 key
, key_length
, value
, value_length
,
198 expiration
, flags
, 0, REPLACE_OP
);
199 LIBMEMCACHED_MEMCACHED_REPLACE_END();
203 memcached_return
memcached_prepend(memcached_st
*ptr
,
204 const char *key
, size_t key_length
,
205 const char *value
, size_t value_length
,
210 rc
= memcached_send(ptr
, key
, key_length
,
211 key
, key_length
, value
, value_length
,
212 expiration
, flags
, 0, PREPEND_OP
);
216 memcached_return
memcached_append(memcached_st
*ptr
,
217 const char *key
, size_t key_length
,
218 const char *value
, size_t value_length
,
223 rc
= memcached_send(ptr
, key
, key_length
,
224 key
, key_length
, value
, value_length
,
225 expiration
, flags
, 0, APPEND_OP
);
229 memcached_return
memcached_cas(memcached_st
*ptr
,
230 const char *key
, size_t key_length
,
231 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
, cas
, CAS_OP
);
243 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
244 const char *master_key
__attribute__((unused
)),
245 size_t master_key_length
__attribute__((unused
)),
246 const char *key
, size_t key_length
,
247 const char *value
, size_t value_length
,
252 LIBMEMCACHED_MEMCACHED_SET_START();
253 rc
= memcached_send(ptr
, master_key
, master_key_length
,
254 key
, key_length
, value
, value_length
,
255 expiration
, flags
, 0, SET_OP
);
256 LIBMEMCACHED_MEMCACHED_SET_END();
260 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
261 const char *master_key
, size_t master_key_length
,
262 const char *key
, size_t key_length
,
263 const char *value
, size_t value_length
,
268 LIBMEMCACHED_MEMCACHED_ADD_START();
269 rc
= memcached_send(ptr
, master_key
, master_key_length
,
270 key
, key_length
, value
, value_length
,
271 expiration
, flags
, 0, ADD_OP
);
272 LIBMEMCACHED_MEMCACHED_ADD_END();
276 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
277 const char *master_key
, size_t master_key_length
,
278 const char *key
, size_t key_length
,
279 const char *value
, size_t value_length
,
284 LIBMEMCACHED_MEMCACHED_REPLACE_START();
285 rc
= memcached_send(ptr
, master_key
, master_key_length
,
286 key
, key_length
, value
, value_length
,
287 expiration
, flags
, 0, REPLACE_OP
);
288 LIBMEMCACHED_MEMCACHED_REPLACE_END();
292 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
293 const char *master_key
, size_t master_key_length
,
294 const char *key
, size_t key_length
,
295 const char *value
, size_t value_length
,
300 rc
= memcached_send(ptr
, master_key
, master_key_length
,
301 key
, key_length
, value
, value_length
,
302 expiration
, flags
, 0, PREPEND_OP
);
306 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
307 const char *master_key
, size_t master_key_length
,
308 const char *key
, size_t key_length
,
309 const char *value
, size_t value_length
,
314 rc
= memcached_send(ptr
, master_key
, master_key_length
,
315 key
, key_length
, value
, value_length
,
316 expiration
, flags
, 0, APPEND_OP
);
320 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
321 const char *master_key
, size_t master_key_length
,
322 const char *key
, size_t key_length
,
323 const char *value
, size_t value_length
,
329 rc
= memcached_send(ptr
, master_key
, master_key_length
,
330 key
, key_length
, value
, value_length
,
331 expiration
, flags
, cas
, CAS_OP
);
335 static inline uint8_t get_com_code(memcached_storage_action verb
, bool noreply
) {
342 ret
=PROTOCOL_BINARY_CMD_SETQ
;
345 ret
=PROTOCOL_BINARY_CMD_ADDQ
;
347 case CAS_OP
: /* FALLTHROUGH */
349 ret
=PROTOCOL_BINARY_CMD_REPLACEQ
;
352 ret
=PROTOCOL_BINARY_CMD_APPENDQ
;
355 ret
=PROTOCOL_BINARY_CMD_PREPENDQ
;
362 ret
=PROTOCOL_BINARY_CMD_SET
;
365 ret
=PROTOCOL_BINARY_CMD_ADD
;
367 case CAS_OP
: /* FALLTHROUGH */
369 ret
=PROTOCOL_BINARY_CMD_REPLACE
;
372 ret
=PROTOCOL_BINARY_CMD_APPEND
;
375 ret
=PROTOCOL_BINARY_CMD_PREPEND
;
384 static memcached_return
memcached_send_binary(memcached_server_st
* server
,
392 memcached_storage_action verb
)
395 protocol_binary_request_set request
= {.bytes
= {0}};
396 size_t send_length
= sizeof(request
.bytes
);
397 bool noreply
= server
->root
->flags
& MEM_NOREPLY
;
399 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
400 request
.message
.header
.request
.opcode
= get_com_code(verb
, noreply
);
401 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
402 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
403 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
404 send_length
-= 8; /* append & prepend does not contain extras! */
407 request
.message
.header
.request
.extlen
= 8;
408 request
.message
.body
.flags
= htonl(flags
);
409 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
412 request
.message
.header
.request
.bodylen
= htonl(key_length
+ value_length
+
413 request
.message
.header
.request
.extlen
);
416 request
.message
.header
.request
.cas
= htonll(cas
);
418 flush
= ((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1;
420 /* write the header */
421 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
422 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
423 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
425 memcached_io_reset(server
);
426 return MEMCACHED_WRITE_FAILURE
;
430 return MEMCACHED_BUFFERED
;
433 return MEMCACHED_SUCCESS
;
435 return memcached_response(server
, NULL
, 0, NULL
);