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 memcached_server_response_decrement(&ptr
->hosts
[server_key
]);
140 return (to_write
== 0) ? MEMCACHED_BUFFERED
: MEMCACHED_SUCCESS
;
144 return MEMCACHED_BUFFERED
;
146 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
148 if (rc
== MEMCACHED_STORED
)
149 return MEMCACHED_SUCCESS
;
154 memcached_io_reset(&ptr
->hosts
[server_key
]);
160 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
161 const char *value
, size_t value_length
,
166 LIBMEMCACHED_MEMCACHED_SET_START();
167 rc
= memcached_send(ptr
, key
, key_length
,
168 key
, key_length
, value
, value_length
,
169 expiration
, flags
, 0, SET_OP
);
170 LIBMEMCACHED_MEMCACHED_SET_END();
174 memcached_return
memcached_add(memcached_st
*ptr
,
175 const char *key
, size_t key_length
,
176 const char *value
, size_t value_length
,
181 LIBMEMCACHED_MEMCACHED_ADD_START();
182 rc
= memcached_send(ptr
, key
, key_length
,
183 key
, key_length
, value
, value_length
,
184 expiration
, flags
, 0, ADD_OP
);
185 LIBMEMCACHED_MEMCACHED_ADD_END();
189 memcached_return
memcached_replace(memcached_st
*ptr
,
190 const char *key
, size_t key_length
,
191 const char *value
, size_t value_length
,
196 LIBMEMCACHED_MEMCACHED_REPLACE_START();
197 rc
= memcached_send(ptr
, key
, key_length
,
198 key
, key_length
, value
, value_length
,
199 expiration
, flags
, 0, REPLACE_OP
);
200 LIBMEMCACHED_MEMCACHED_REPLACE_END();
204 memcached_return
memcached_prepend(memcached_st
*ptr
,
205 const char *key
, size_t key_length
,
206 const char *value
, size_t value_length
,
211 rc
= memcached_send(ptr
, key
, key_length
,
212 key
, key_length
, value
, value_length
,
213 expiration
, flags
, 0, PREPEND_OP
);
217 memcached_return
memcached_append(memcached_st
*ptr
,
218 const char *key
, size_t key_length
,
219 const char *value
, size_t value_length
,
224 rc
= memcached_send(ptr
, key
, key_length
,
225 key
, key_length
, value
, value_length
,
226 expiration
, flags
, 0, APPEND_OP
);
230 memcached_return
memcached_cas(memcached_st
*ptr
,
231 const char *key
, size_t key_length
,
232 const char *value
, size_t value_length
,
238 rc
= memcached_send(ptr
, key
, key_length
,
239 key
, key_length
, value
, value_length
,
240 expiration
, flags
, cas
, CAS_OP
);
244 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
245 const char *master_key
__attribute__((unused
)),
246 size_t master_key_length
__attribute__((unused
)),
247 const char *key
, size_t key_length
,
248 const char *value
, size_t value_length
,
253 LIBMEMCACHED_MEMCACHED_SET_START();
254 rc
= memcached_send(ptr
, master_key
, master_key_length
,
255 key
, key_length
, value
, value_length
,
256 expiration
, flags
, 0, SET_OP
);
257 LIBMEMCACHED_MEMCACHED_SET_END();
261 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
262 const char *master_key
, size_t master_key_length
,
263 const char *key
, size_t key_length
,
264 const char *value
, size_t value_length
,
269 LIBMEMCACHED_MEMCACHED_ADD_START();
270 rc
= memcached_send(ptr
, master_key
, master_key_length
,
271 key
, key_length
, value
, value_length
,
272 expiration
, flags
, 0, ADD_OP
);
273 LIBMEMCACHED_MEMCACHED_ADD_END();
277 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
278 const char *master_key
, size_t master_key_length
,
279 const char *key
, size_t key_length
,
280 const char *value
, size_t value_length
,
285 LIBMEMCACHED_MEMCACHED_REPLACE_START();
286 rc
= memcached_send(ptr
, master_key
, master_key_length
,
287 key
, key_length
, value
, value_length
,
288 expiration
, flags
, 0, REPLACE_OP
);
289 LIBMEMCACHED_MEMCACHED_REPLACE_END();
293 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
294 const char *master_key
, size_t master_key_length
,
295 const char *key
, size_t key_length
,
296 const char *value
, size_t value_length
,
301 rc
= memcached_send(ptr
, master_key
, master_key_length
,
302 key
, key_length
, value
, value_length
,
303 expiration
, flags
, 0, PREPEND_OP
);
307 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
308 const char *master_key
, size_t master_key_length
,
309 const char *key
, size_t key_length
,
310 const char *value
, size_t value_length
,
315 rc
= memcached_send(ptr
, master_key
, master_key_length
,
316 key
, key_length
, value
, value_length
,
317 expiration
, flags
, 0, APPEND_OP
);
321 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
322 const char *master_key
, size_t master_key_length
,
323 const char *key
, size_t key_length
,
324 const char *value
, size_t value_length
,
330 rc
= memcached_send(ptr
, master_key
, master_key_length
,
331 key
, key_length
, value
, value_length
,
332 expiration
, flags
, cas
, CAS_OP
);
336 static memcached_return
memcached_send_binary(memcached_server_st
* server
,
344 memcached_storage_action verb
)
347 protocol_binary_request_set request
= {.bytes
= {0}};
348 size_t send_length
= sizeof(request
.bytes
);
350 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
354 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
357 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_ADD
;
360 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
363 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_APPEND
;
366 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_PREPEND
;
369 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
373 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
374 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
375 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
376 send_length
-= 8; /* append & prepend does not contain extras! */
379 request
.message
.header
.request
.extlen
= 8;
380 request
.message
.body
.flags
= htonl(flags
);
381 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
384 request
.message
.header
.request
.bodylen
= htonl(key_length
+ value_length
+
385 request
.message
.header
.request
.extlen
);
388 request
.message
.header
.request
.cas
= htonll(cas
);
390 flush
= ((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1;
392 /* write the header */
393 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
394 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
395 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
397 memcached_io_reset(server
);
398 return MEMCACHED_WRITE_FAILURE
;
402 return MEMCACHED_BUFFERED
;
404 return memcached_response(server
, NULL
, 0, NULL
);