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
__attribute__((unused
)),
233 size_t master_key_length
__attribute__((unused
)),
234 const char *key
, size_t key_length
,
235 const char *value
, size_t value_length
,
240 LIBMEMCACHED_MEMCACHED_SET_START();
241 rc
= memcached_send(ptr
, master_key
, master_key_length
,
242 key
, key_length
, value
, value_length
,
243 expiration
, flags
, 0, SET_OP
);
244 LIBMEMCACHED_MEMCACHED_SET_END();
248 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
249 const char *master_key
, size_t master_key_length
,
250 const char *key
, size_t key_length
,
251 const char *value
, size_t value_length
,
256 LIBMEMCACHED_MEMCACHED_ADD_START();
257 rc
= memcached_send(ptr
, master_key
, master_key_length
,
258 key
, key_length
, value
, value_length
,
259 expiration
, flags
, 0, ADD_OP
);
260 LIBMEMCACHED_MEMCACHED_ADD_END();
264 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
265 const char *master_key
, size_t master_key_length
,
266 const char *key
, size_t key_length
,
267 const char *value
, size_t value_length
,
272 LIBMEMCACHED_MEMCACHED_REPLACE_START();
273 rc
= memcached_send(ptr
, master_key
, master_key_length
,
274 key
, key_length
, value
, value_length
,
275 expiration
, flags
, 0, REPLACE_OP
);
276 LIBMEMCACHED_MEMCACHED_REPLACE_END();
280 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
281 const char *master_key
, size_t master_key_length
,
282 const char *key
, size_t key_length
,
283 const char *value
, size_t value_length
,
288 rc
= memcached_send(ptr
, master_key
, master_key_length
,
289 key
, key_length
, value
, value_length
,
290 expiration
, flags
, 0, PREPEND_OP
);
294 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
295 const char *master_key
, size_t master_key_length
,
296 const char *key
, size_t key_length
,
297 const char *value
, size_t value_length
,
302 rc
= memcached_send(ptr
, master_key
, master_key_length
,
303 key
, key_length
, value
, value_length
,
304 expiration
, flags
, 0, APPEND_OP
);
308 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
309 const char *master_key
, size_t master_key_length
,
310 const char *key
, size_t key_length
,
311 const char *value
, size_t value_length
,
317 rc
= memcached_send(ptr
, master_key
, master_key_length
,
318 key
, key_length
, value
, value_length
,
319 expiration
, flags
, cas
, CAS_OP
);
323 static memcached_return
memcached_send_binary(memcached_server_st
* server
,
331 memcached_storage_action verb
)
334 protocol_binary_request_set request
= {.bytes
= {0}};
335 size_t send_length
= sizeof(request
.bytes
);
337 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
341 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
344 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_ADD
;
347 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
350 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_APPEND
;
353 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_PREPEND
;
356 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! */
366 request
.message
.header
.request
.extlen
= 8;
367 request
.message
.body
.flags
= htonl(flags
);
368 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
371 request
.message
.header
.request
.bodylen
= htonl(key_length
+ value_length
+
372 request
.message
.header
.request
.extlen
);
375 request
.message
.header
.request
.cas
= htonll(cas
);
377 flush
= ((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1;
378 /* write the header */
379 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
380 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
381 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
383 memcached_io_reset(server
);
384 return MEMCACHED_WRITE_FAILURE
;
388 return MEMCACHED_BUFFERED
;
390 return memcached_response(server
, NULL
, 0, NULL
);