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
)
110 size_t cmd_size
= write_length
+ value_length
+ 2;
111 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
112 return MEMCACHED_WRITE_FAILURE
;
113 if (cmd_size
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
114 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
117 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
119 rc
= MEMCACHED_WRITE_FAILURE
;
123 /* Send command header */
124 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
125 if (rc
!= MEMCACHED_SUCCESS
)
128 /* Send command body */
129 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
131 rc
= MEMCACHED_WRITE_FAILURE
;
135 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
140 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
142 rc
= MEMCACHED_WRITE_FAILURE
;
146 if (ptr
->flags
& MEM_NOREPLY
)
147 return (to_write
== 0) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
150 return MEMCACHED_BUFFERED
;
152 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
154 if (rc
== MEMCACHED_STORED
)
155 return MEMCACHED_SUCCESS
;
160 memcached_io_reset(&ptr
->hosts
[server_key
]);
166 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
167 const char *value
, size_t value_length
,
172 LIBMEMCACHED_MEMCACHED_SET_START();
173 rc
= memcached_send(ptr
, key
, key_length
,
174 key
, key_length
, value
, value_length
,
175 expiration
, flags
, 0, SET_OP
);
176 LIBMEMCACHED_MEMCACHED_SET_END();
180 memcached_return
memcached_add(memcached_st
*ptr
,
181 const char *key
, size_t key_length
,
182 const char *value
, size_t value_length
,
187 LIBMEMCACHED_MEMCACHED_ADD_START();
188 rc
= memcached_send(ptr
, key
, key_length
,
189 key
, key_length
, value
, value_length
,
190 expiration
, flags
, 0, ADD_OP
);
191 LIBMEMCACHED_MEMCACHED_ADD_END();
195 memcached_return
memcached_replace(memcached_st
*ptr
,
196 const char *key
, size_t key_length
,
197 const char *value
, size_t value_length
,
202 LIBMEMCACHED_MEMCACHED_REPLACE_START();
203 rc
= memcached_send(ptr
, key
, key_length
,
204 key
, key_length
, value
, value_length
,
205 expiration
, flags
, 0, REPLACE_OP
);
206 LIBMEMCACHED_MEMCACHED_REPLACE_END();
210 memcached_return
memcached_prepend(memcached_st
*ptr
,
211 const char *key
, size_t key_length
,
212 const char *value
, size_t value_length
,
217 rc
= memcached_send(ptr
, key
, key_length
,
218 key
, key_length
, value
, value_length
,
219 expiration
, flags
, 0, PREPEND_OP
);
223 memcached_return
memcached_append(memcached_st
*ptr
,
224 const char *key
, size_t key_length
,
225 const char *value
, size_t value_length
,
230 rc
= memcached_send(ptr
, key
, key_length
,
231 key
, key_length
, value
, value_length
,
232 expiration
, flags
, 0, APPEND_OP
);
236 memcached_return
memcached_cas(memcached_st
*ptr
,
237 const char *key
, size_t key_length
,
238 const char *value
, size_t value_length
,
244 rc
= memcached_send(ptr
, key
, key_length
,
245 key
, key_length
, value
, value_length
,
246 expiration
, flags
, cas
, CAS_OP
);
250 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
251 const char *master_key
__attribute__((unused
)),
252 size_t master_key_length
__attribute__((unused
)),
253 const char *key
, size_t key_length
,
254 const char *value
, size_t value_length
,
259 LIBMEMCACHED_MEMCACHED_SET_START();
260 rc
= memcached_send(ptr
, master_key
, master_key_length
,
261 key
, key_length
, value
, value_length
,
262 expiration
, flags
, 0, SET_OP
);
263 LIBMEMCACHED_MEMCACHED_SET_END();
267 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
268 const char *master_key
, size_t master_key_length
,
269 const char *key
, size_t key_length
,
270 const char *value
, size_t value_length
,
275 LIBMEMCACHED_MEMCACHED_ADD_START();
276 rc
= memcached_send(ptr
, master_key
, master_key_length
,
277 key
, key_length
, value
, value_length
,
278 expiration
, flags
, 0, ADD_OP
);
279 LIBMEMCACHED_MEMCACHED_ADD_END();
283 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
284 const char *master_key
, size_t master_key_length
,
285 const char *key
, size_t key_length
,
286 const char *value
, size_t value_length
,
291 LIBMEMCACHED_MEMCACHED_REPLACE_START();
292 rc
= memcached_send(ptr
, master_key
, master_key_length
,
293 key
, key_length
, value
, value_length
,
294 expiration
, flags
, 0, REPLACE_OP
);
295 LIBMEMCACHED_MEMCACHED_REPLACE_END();
299 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
300 const char *master_key
, size_t master_key_length
,
301 const char *key
, size_t key_length
,
302 const char *value
, size_t value_length
,
307 rc
= memcached_send(ptr
, master_key
, master_key_length
,
308 key
, key_length
, value
, value_length
,
309 expiration
, flags
, 0, PREPEND_OP
);
313 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
314 const char *master_key
, size_t master_key_length
,
315 const char *key
, size_t key_length
,
316 const char *value
, size_t value_length
,
321 rc
= memcached_send(ptr
, master_key
, master_key_length
,
322 key
, key_length
, value
, value_length
,
323 expiration
, flags
, 0, APPEND_OP
);
327 memcached_return
memcached_cas_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
,
336 rc
= memcached_send(ptr
, master_key
, master_key_length
,
337 key
, key_length
, value
, value_length
,
338 expiration
, flags
, cas
, CAS_OP
);
342 static inline uint8_t get_com_code(memcached_storage_action verb
, bool noreply
)
350 ret
=PROTOCOL_BINARY_CMD_SETQ
;
353 ret
=PROTOCOL_BINARY_CMD_ADDQ
;
355 case CAS_OP
: /* FALLTHROUGH */
357 ret
=PROTOCOL_BINARY_CMD_REPLACEQ
;
360 ret
=PROTOCOL_BINARY_CMD_APPENDQ
;
363 ret
=PROTOCOL_BINARY_CMD_PREPENDQ
;
370 ret
=PROTOCOL_BINARY_CMD_SET
;
373 ret
=PROTOCOL_BINARY_CMD_ADD
;
375 case CAS_OP
: /* FALLTHROUGH */
377 ret
=PROTOCOL_BINARY_CMD_REPLACE
;
380 ret
=PROTOCOL_BINARY_CMD_APPEND
;
383 ret
=PROTOCOL_BINARY_CMD_PREPEND
;
392 static memcached_return
memcached_send_binary(memcached_server_st
* server
,
400 memcached_storage_action verb
)
403 protocol_binary_request_set request
= {.bytes
= {0}};
404 size_t send_length
= sizeof(request
.bytes
);
405 bool noreply
= server
->root
->flags
& MEM_NOREPLY
;
407 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
408 request
.message
.header
.request
.opcode
= get_com_code(verb
, noreply
);
409 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
410 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
411 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
412 send_length
-= 8; /* append & prepend does not contain extras! */
415 request
.message
.header
.request
.extlen
= 8;
416 request
.message
.body
.flags
= htonl(flags
);
417 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
420 request
.message
.header
.request
.bodylen
= htonl(key_length
+ value_length
+
421 request
.message
.header
.request
.extlen
);
424 request
.message
.header
.request
.cas
= htonll(cas
);
426 flush
= ((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1;
428 if ((server
->root
->flags
& MEM_USE_UDP
) && !flush
)
430 size_t cmd_size
= send_length
+ key_length
+ value_length
;
431 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
432 return MEMCACHED_WRITE_FAILURE
;
433 if (cmd_size
+ server
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
434 memcached_io_write(server
,NULL
,0, 1);
437 /* write the header */
438 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
439 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
440 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
442 memcached_io_reset(server
);
443 return MEMCACHED_WRITE_FAILURE
;
447 return MEMCACHED_BUFFERED
;
450 return MEMCACHED_SUCCESS
;
452 return memcached_response(server
, NULL
, 0, NULL
);