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\r\n", storage_op_string(verb
),
94 (int)key_length
, key
, flags
,
95 (unsigned long long)expiration
, value_length
,
96 (unsigned long long)cas
);
98 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
99 "%s %s%.*s %u %llu %zu\r\n", storage_op_string(verb
),
101 (int)key_length
, key
, flags
,
102 (unsigned long long)expiration
, value_length
);
104 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
106 rc
= MEMCACHED_WRITE_FAILURE
;
110 /* Send command header */
111 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
112 if (rc
!= MEMCACHED_SUCCESS
)
115 /* Send command body */
116 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
118 rc
= MEMCACHED_WRITE_FAILURE
;
122 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
127 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
129 rc
= MEMCACHED_WRITE_FAILURE
;
134 return MEMCACHED_BUFFERED
;
136 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
138 if (rc
== MEMCACHED_STORED
)
139 return MEMCACHED_SUCCESS
;
144 memcached_io_reset(&ptr
->hosts
[server_key
]);
150 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
151 const char *value
, size_t value_length
,
156 LIBMEMCACHED_MEMCACHED_SET_START();
157 rc
= memcached_send(ptr
, key
, key_length
,
158 key
, key_length
, value
, value_length
,
159 expiration
, flags
, 0, SET_OP
);
160 LIBMEMCACHED_MEMCACHED_SET_END();
164 memcached_return
memcached_add(memcached_st
*ptr
,
165 const char *key
, size_t key_length
,
166 const char *value
, size_t value_length
,
171 LIBMEMCACHED_MEMCACHED_ADD_START();
172 rc
= memcached_send(ptr
, key
, key_length
,
173 key
, key_length
, value
, value_length
,
174 expiration
, flags
, 0, ADD_OP
);
175 LIBMEMCACHED_MEMCACHED_ADD_END();
179 memcached_return
memcached_replace(memcached_st
*ptr
,
180 const char *key
, size_t key_length
,
181 const char *value
, size_t value_length
,
186 LIBMEMCACHED_MEMCACHED_REPLACE_START();
187 rc
= memcached_send(ptr
, key
, key_length
,
188 key
, key_length
, value
, value_length
,
189 expiration
, flags
, 0, REPLACE_OP
);
190 LIBMEMCACHED_MEMCACHED_REPLACE_END();
194 memcached_return
memcached_prepend(memcached_st
*ptr
,
195 const char *key
, size_t key_length
,
196 const char *value
, size_t value_length
,
201 rc
= memcached_send(ptr
, key
, key_length
,
202 key
, key_length
, value
, value_length
,
203 expiration
, flags
, 0, PREPEND_OP
);
207 memcached_return
memcached_append(memcached_st
*ptr
,
208 const char *key
, size_t key_length
,
209 const char *value
, size_t value_length
,
214 rc
= memcached_send(ptr
, key
, key_length
,
215 key
, key_length
, value
, value_length
,
216 expiration
, flags
, 0, APPEND_OP
);
220 memcached_return
memcached_cas(memcached_st
*ptr
,
221 const char *key
, size_t key_length
,
222 const char *value
, size_t value_length
,
228 rc
= memcached_send(ptr
, key
, key_length
,
229 key
, key_length
, value
, value_length
,
230 expiration
, flags
, cas
, CAS_OP
);
234 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
235 const char *master_key
__attribute__((unused
)),
236 size_t master_key_length
__attribute__((unused
)),
237 const char *key
, size_t key_length
,
238 const char *value
, size_t value_length
,
243 LIBMEMCACHED_MEMCACHED_SET_START();
244 rc
= memcached_send(ptr
, master_key
, master_key_length
,
245 key
, key_length
, value
, value_length
,
246 expiration
, flags
, 0, SET_OP
);
247 LIBMEMCACHED_MEMCACHED_SET_END();
251 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
252 const char *master_key
, size_t master_key_length
,
253 const char *key
, size_t key_length
,
254 const char *value
, size_t value_length
,
259 LIBMEMCACHED_MEMCACHED_ADD_START();
260 rc
= memcached_send(ptr
, master_key
, master_key_length
,
261 key
, key_length
, value
, value_length
,
262 expiration
, flags
, 0, ADD_OP
);
263 LIBMEMCACHED_MEMCACHED_ADD_END();
267 memcached_return
memcached_replace_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_REPLACE_START();
276 rc
= memcached_send(ptr
, master_key
, master_key_length
,
277 key
, key_length
, value
, value_length
,
278 expiration
, flags
, 0, REPLACE_OP
);
279 LIBMEMCACHED_MEMCACHED_REPLACE_END();
283 memcached_return
memcached_prepend_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 rc
= memcached_send(ptr
, master_key
, master_key_length
,
292 key
, key_length
, value
, value_length
,
293 expiration
, flags
, 0, PREPEND_OP
);
297 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
298 const char *master_key
, size_t master_key_length
,
299 const char *key
, size_t key_length
,
300 const char *value
, size_t value_length
,
305 rc
= memcached_send(ptr
, master_key
, master_key_length
,
306 key
, key_length
, value
, value_length
,
307 expiration
, flags
, 0, APPEND_OP
);
311 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
312 const char *master_key
, size_t master_key_length
,
313 const char *key
, size_t key_length
,
314 const char *value
, size_t value_length
,
320 rc
= memcached_send(ptr
, master_key
, master_key_length
,
321 key
, key_length
, value
, value_length
,
322 expiration
, flags
, cas
, CAS_OP
);
326 static memcached_return
memcached_send_binary(memcached_server_st
* server
,
334 memcached_storage_action verb
)
337 protocol_binary_request_set request
= {.bytes
= {0}};
338 size_t send_length
= sizeof(request
.bytes
);
340 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
344 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
347 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_ADD
;
350 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
353 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_APPEND
;
356 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_PREPEND
;
359 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
363 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
364 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
365 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
366 send_length
-= 8; /* append & prepend does not contain extras! */
369 request
.message
.header
.request
.extlen
= 8;
370 request
.message
.body
.flags
= htonl(flags
);
371 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
374 request
.message
.header
.request
.bodylen
= htonl(key_length
+ value_length
+
375 request
.message
.header
.request
.extlen
);
378 request
.message
.header
.request
.cas
= htonll(cas
);
380 flush
= ((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1;
381 /* write the header */
382 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
383 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
384 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
386 memcached_io_reset(server
);
387 return MEMCACHED_WRITE_FAILURE
;
391 return MEMCACHED_BUFFERED
;
393 return memcached_response(server
, NULL
, 0, NULL
);