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 unlikely (key_length
== 0)
74 return MEMCACHED_NO_KEY_PROVIDED
;
76 unlikely (ptr
->number_of_hosts
== 0)
77 return MEMCACHED_NO_SERVERS
;
79 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test((char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
80 return MEMCACHED_BAD_KEY_PROVIDED
;
82 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
84 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
85 return memcached_send_binary(&ptr
->hosts
[server_key
], key
, key_length
,
86 value
, value_length
, expiration
,
90 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
91 "%s %s%.*s %u %llu %zu %llu\r\n", storage_op_string(verb
),
93 (int)key_length
, key
, flags
,
94 (unsigned long long)expiration
, value_length
,
95 (unsigned long long)cas
);
97 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
98 "%s %s%.*s %u %llu %zu\r\n", storage_op_string(verb
),
100 (int)key_length
, key
, flags
,
101 (unsigned long long)expiration
, value_length
);
103 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
105 rc
= MEMCACHED_WRITE_FAILURE
;
109 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
110 if (rc
!= MEMCACHED_SUCCESS
)
113 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
115 rc
= MEMCACHED_WRITE_FAILURE
;
119 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
124 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
126 rc
= MEMCACHED_WRITE_FAILURE
;
131 return MEMCACHED_BUFFERED
;
133 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
135 if (rc
== MEMCACHED_STORED
)
136 return MEMCACHED_SUCCESS
;
141 memcached_io_reset(&ptr
->hosts
[server_key
]);
147 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
148 const char *value
, size_t value_length
,
153 LIBMEMCACHED_MEMCACHED_SET_START();
154 rc
= memcached_send(ptr
, key
, key_length
,
155 key
, key_length
, value
, value_length
,
156 expiration
, flags
, 0, SET_OP
);
157 LIBMEMCACHED_MEMCACHED_SET_END();
161 memcached_return
memcached_add(memcached_st
*ptr
,
162 const char *key
, size_t key_length
,
163 const char *value
, size_t value_length
,
168 LIBMEMCACHED_MEMCACHED_ADD_START();
169 rc
= memcached_send(ptr
, key
, key_length
,
170 key
, key_length
, value
, value_length
,
171 expiration
, flags
, 0, ADD_OP
);
172 LIBMEMCACHED_MEMCACHED_ADD_END();
176 memcached_return
memcached_replace(memcached_st
*ptr
,
177 const char *key
, size_t key_length
,
178 const char *value
, size_t value_length
,
183 LIBMEMCACHED_MEMCACHED_REPLACE_START();
184 rc
= memcached_send(ptr
, key
, key_length
,
185 key
, key_length
, value
, value_length
,
186 expiration
, flags
, 0, REPLACE_OP
);
187 LIBMEMCACHED_MEMCACHED_REPLACE_END();
191 memcached_return
memcached_prepend(memcached_st
*ptr
,
192 const char *key
, size_t key_length
,
193 const char *value
, size_t value_length
,
198 rc
= memcached_send(ptr
, key
, key_length
,
199 key
, key_length
, value
, value_length
,
200 expiration
, flags
, 0, PREPEND_OP
);
204 memcached_return
memcached_append(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, APPEND_OP
);
217 memcached_return
memcached_cas(memcached_st
*ptr
,
218 const char *key
, size_t key_length
,
219 const char *value
, size_t value_length
,
225 rc
= memcached_send(ptr
, key
, key_length
,
226 key
, key_length
, value
, value_length
,
227 expiration
, flags
, cas
, CAS_OP
);
231 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
232 const char *master_key
, size_t master_key_length
,
233 const char *key
, size_t key_length
,
234 const char *value
, size_t value_length
,
239 LIBMEMCACHED_MEMCACHED_SET_START();
240 rc
= memcached_send(ptr
, key
, key_length
,
241 key
, key_length
, value
, value_length
,
242 expiration
, flags
, 0, SET_OP
);
243 LIBMEMCACHED_MEMCACHED_SET_END();
247 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
248 const char *master_key
, size_t master_key_length
,
249 const char *key
, size_t key_length
,
250 const char *value
, size_t value_length
,
255 LIBMEMCACHED_MEMCACHED_ADD_START();
256 rc
= memcached_send(ptr
, key
, key_length
,
257 key
, key_length
, value
, value_length
,
258 expiration
, flags
, 0, ADD_OP
);
259 LIBMEMCACHED_MEMCACHED_ADD_END();
263 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
264 const char *master_key
, size_t master_key_length
,
265 const char *key
, size_t key_length
,
266 const char *value
, size_t value_length
,
271 LIBMEMCACHED_MEMCACHED_REPLACE_START();
272 rc
= memcached_send(ptr
, key
, key_length
,
273 key
, key_length
, value
, value_length
,
274 expiration
, flags
, 0, REPLACE_OP
);
275 LIBMEMCACHED_MEMCACHED_REPLACE_END();
279 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
280 const char *master_key
, size_t master_key_length
,
281 const char *key
, size_t key_length
,
282 const char *value
, size_t value_length
,
287 rc
= memcached_send(ptr
, key
, key_length
,
288 key
, key_length
, value
, value_length
,
289 expiration
, flags
, 0, PREPEND_OP
);
293 memcached_return
memcached_append_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
, key
, key_length
,
302 key
, key_length
, value
, value_length
,
303 expiration
, flags
, 0, APPEND_OP
);
307 memcached_return
memcached_cas_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
,
316 rc
= memcached_send(ptr
, master_key
, master_key_length
,
317 key
, key_length
, value
, value_length
,
318 expiration
, flags
, cas
, CAS_OP
);
322 static memcached_return
memcached_send_binary(memcached_server_st
* server
,
330 memcached_storage_action verb
)
332 protocol_binary_request_set request
= {0};
333 size_t send_length
= sizeof(request
.bytes
);
335 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
339 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
342 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_ADD
;
345 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
348 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_APPEND
;
351 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_PREPEND
;
354 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
360 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
361 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
362 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
363 send_length
-= 8; /* append & prepend does not contain extras! */
365 request
.message
.header
.request
.extlen
= 8;
366 request
.message
.body
.flags
= htonl(flags
);
367 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
370 request
.message
.header
.request
.bodylen
= htonl(key_length
+ value_length
+
371 request
.message
.header
.request
.extlen
);
374 request
.message
.header
.request
.cas
= htonll(cas
);
376 char flush
= ((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1;
377 /* write the header */
378 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
379 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
380 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
382 memcached_io_reset(server
);
383 return MEMCACHED_WRITE_FAILURE
;
387 return MEMCACHED_BUFFERED
;
389 return memcached_response(server
, NULL
, 0, NULL
);