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
) && (memcached_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" : "");
101 char *buffer_ptr
= buffer
;
102 const char *command
= storage_op_string(verb
);
104 /* Copy in the command, no space needed, we handle that in the command function*/
105 memcpy(buffer_ptr
, command
, strlen(command
));
107 /* Copy in the key prefix, switch to the buffer_ptr */
108 buffer_ptr
= memcpy(buffer_ptr
+ strlen(command
) , ptr
->prefix_key
, strlen(ptr
->prefix_key
));
110 /* Copy in the key, adjust point if a key prefix was used. */
111 buffer_ptr
= memcpy(buffer_ptr
+ (ptr
->prefix_key
? strlen(ptr
->prefix_key
) : 0),
113 buffer_ptr
+= key_length
;
117 write_length
= (size_t)(buffer_ptr
- buffer
);
118 write_length
+= snprintf(buffer_ptr
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
121 (unsigned long long)expiration
, value_length
,
122 (ptr
->flags
& MEM_NOREPLY
) ? " noreply" : "");
125 if (ptr
->flags
& MEM_USE_UDP
&& ptr
->flags
& MEM_BUFFER_REQUESTS
)
127 size_t cmd_size
= write_length
+ value_length
+ 2;
128 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
129 return MEMCACHED_WRITE_FAILURE
;
130 if (cmd_size
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
131 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
134 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
136 rc
= MEMCACHED_WRITE_FAILURE
;
140 /* Send command header */
141 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
142 if (rc
!= MEMCACHED_SUCCESS
)
145 /* Send command body */
146 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
148 rc
= MEMCACHED_WRITE_FAILURE
;
152 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
157 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
159 rc
= MEMCACHED_WRITE_FAILURE
;
163 if (ptr
->flags
& MEM_NOREPLY
)
164 return (to_write
== 0) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
167 return MEMCACHED_BUFFERED
;
169 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
171 if (rc
== MEMCACHED_STORED
)
172 return MEMCACHED_SUCCESS
;
177 memcached_io_reset(&ptr
->hosts
[server_key
]);
183 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
184 const char *value
, size_t value_length
,
189 LIBMEMCACHED_MEMCACHED_SET_START();
190 rc
= memcached_send(ptr
, key
, key_length
,
191 key
, key_length
, value
, value_length
,
192 expiration
, flags
, 0, SET_OP
);
193 LIBMEMCACHED_MEMCACHED_SET_END();
197 memcached_return
memcached_add(memcached_st
*ptr
,
198 const char *key
, size_t key_length
,
199 const char *value
, size_t value_length
,
204 LIBMEMCACHED_MEMCACHED_ADD_START();
205 rc
= memcached_send(ptr
, key
, key_length
,
206 key
, key_length
, value
, value_length
,
207 expiration
, flags
, 0, ADD_OP
);
208 LIBMEMCACHED_MEMCACHED_ADD_END();
212 memcached_return
memcached_replace(memcached_st
*ptr
,
213 const char *key
, size_t key_length
,
214 const char *value
, size_t value_length
,
219 LIBMEMCACHED_MEMCACHED_REPLACE_START();
220 rc
= memcached_send(ptr
, key
, key_length
,
221 key
, key_length
, value
, value_length
,
222 expiration
, flags
, 0, REPLACE_OP
);
223 LIBMEMCACHED_MEMCACHED_REPLACE_END();
227 memcached_return
memcached_prepend(memcached_st
*ptr
,
228 const char *key
, size_t key_length
,
229 const char *value
, size_t value_length
,
234 rc
= memcached_send(ptr
, key
, key_length
,
235 key
, key_length
, value
, value_length
,
236 expiration
, flags
, 0, PREPEND_OP
);
240 memcached_return
memcached_append(memcached_st
*ptr
,
241 const char *key
, size_t key_length
,
242 const char *value
, size_t value_length
,
247 rc
= memcached_send(ptr
, key
, key_length
,
248 key
, key_length
, value
, value_length
,
249 expiration
, flags
, 0, APPEND_OP
);
253 memcached_return
memcached_cas(memcached_st
*ptr
,
254 const char *key
, size_t key_length
,
255 const char *value
, size_t value_length
,
261 rc
= memcached_send(ptr
, key
, key_length
,
262 key
, key_length
, value
, value_length
,
263 expiration
, flags
, cas
, CAS_OP
);
267 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
268 const char *master_key
__attribute__((unused
)),
269 size_t master_key_length
__attribute__((unused
)),
270 const char *key
, size_t key_length
,
271 const char *value
, size_t value_length
,
276 LIBMEMCACHED_MEMCACHED_SET_START();
277 rc
= memcached_send(ptr
, master_key
, master_key_length
,
278 key
, key_length
, value
, value_length
,
279 expiration
, flags
, 0, SET_OP
);
280 LIBMEMCACHED_MEMCACHED_SET_END();
284 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
285 const char *master_key
, size_t master_key_length
,
286 const char *key
, size_t key_length
,
287 const char *value
, size_t value_length
,
292 LIBMEMCACHED_MEMCACHED_ADD_START();
293 rc
= memcached_send(ptr
, master_key
, master_key_length
,
294 key
, key_length
, value
, value_length
,
295 expiration
, flags
, 0, ADD_OP
);
296 LIBMEMCACHED_MEMCACHED_ADD_END();
300 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
301 const char *master_key
, size_t master_key_length
,
302 const char *key
, size_t key_length
,
303 const char *value
, size_t value_length
,
308 LIBMEMCACHED_MEMCACHED_REPLACE_START();
309 rc
= memcached_send(ptr
, master_key
, master_key_length
,
310 key
, key_length
, value
, value_length
,
311 expiration
, flags
, 0, REPLACE_OP
);
312 LIBMEMCACHED_MEMCACHED_REPLACE_END();
316 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
317 const char *master_key
, size_t master_key_length
,
318 const char *key
, size_t key_length
,
319 const char *value
, size_t value_length
,
324 rc
= memcached_send(ptr
, master_key
, master_key_length
,
325 key
, key_length
, value
, value_length
,
326 expiration
, flags
, 0, PREPEND_OP
);
330 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
331 const char *master_key
, size_t master_key_length
,
332 const char *key
, size_t key_length
,
333 const char *value
, size_t value_length
,
338 rc
= memcached_send(ptr
, master_key
, master_key_length
,
339 key
, key_length
, value
, value_length
,
340 expiration
, flags
, 0, APPEND_OP
);
344 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
345 const char *master_key
, size_t master_key_length
,
346 const char *key
, size_t key_length
,
347 const char *value
, size_t value_length
,
353 rc
= memcached_send(ptr
, master_key
, master_key_length
,
354 key
, key_length
, value
, value_length
,
355 expiration
, flags
, cas
, CAS_OP
);
359 static inline uint8_t get_com_code(memcached_storage_action verb
, bool noreply
)
361 /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
362 * be used uninitialized in this function. FAIL */
369 ret
=PROTOCOL_BINARY_CMD_SETQ
;
372 ret
=PROTOCOL_BINARY_CMD_ADDQ
;
374 case CAS_OP
: /* FALLTHROUGH */
376 ret
=PROTOCOL_BINARY_CMD_REPLACEQ
;
379 ret
=PROTOCOL_BINARY_CMD_APPENDQ
;
382 ret
=PROTOCOL_BINARY_CMD_PREPENDQ
;
389 ret
=PROTOCOL_BINARY_CMD_SET
;
392 ret
=PROTOCOL_BINARY_CMD_ADD
;
394 case CAS_OP
: /* FALLTHROUGH */
396 ret
=PROTOCOL_BINARY_CMD_REPLACE
;
399 ret
=PROTOCOL_BINARY_CMD_APPEND
;
402 ret
=PROTOCOL_BINARY_CMD_PREPEND
;
411 static memcached_return
memcached_send_binary(memcached_server_st
* server
,
419 memcached_storage_action verb
)
422 protocol_binary_request_set request
= {.bytes
= {0}};
423 size_t send_length
= sizeof(request
.bytes
);
424 bool noreply
= server
->root
->flags
& MEM_NOREPLY
;
426 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
427 request
.message
.header
.request
.opcode
= get_com_code(verb
, noreply
);
428 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
429 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
430 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
431 send_length
-= 8; /* append & prepend does not contain extras! */
434 request
.message
.header
.request
.extlen
= 8;
435 request
.message
.body
.flags
= htonl(flags
);
436 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
439 request
.message
.header
.request
.bodylen
= htonl(key_length
+ value_length
+
440 request
.message
.header
.request
.extlen
);
443 request
.message
.header
.request
.cas
= htonll(cas
);
445 flush
= ((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1;
447 if ((server
->root
->flags
& MEM_USE_UDP
) && !flush
)
449 size_t cmd_size
= send_length
+ key_length
+ value_length
;
450 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
451 return MEMCACHED_WRITE_FAILURE
;
452 if (cmd_size
+ server
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
453 memcached_io_write(server
,NULL
,0, 1);
456 /* write the header */
457 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
458 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
459 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
461 memcached_io_reset(server
);
462 return MEMCACHED_WRITE_FAILURE
;
466 return MEMCACHED_BUFFERED
;
469 return MEMCACHED_SUCCESS
;
471 return memcached_response(server
, NULL
, 0, NULL
);