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 /* Send command header */
110 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
111 if (rc
!= MEMCACHED_SUCCESS
)
114 /* Send command body */
115 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
117 rc
= MEMCACHED_WRITE_FAILURE
;
121 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
126 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
128 rc
= MEMCACHED_WRITE_FAILURE
;
133 return MEMCACHED_BUFFERED
;
135 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
137 if (rc
== MEMCACHED_STORED
)
138 return MEMCACHED_SUCCESS
;
143 memcached_io_reset(&ptr
->hosts
[server_key
]);
149 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
150 const char *value
, size_t value_length
,
155 LIBMEMCACHED_MEMCACHED_SET_START();
156 rc
= memcached_send(ptr
, key
, key_length
,
157 key
, key_length
, value
, value_length
,
158 expiration
, flags
, 0, SET_OP
);
159 LIBMEMCACHED_MEMCACHED_SET_END();
163 memcached_return
memcached_add(memcached_st
*ptr
,
164 const char *key
, size_t key_length
,
165 const char *value
, size_t value_length
,
170 LIBMEMCACHED_MEMCACHED_ADD_START();
171 rc
= memcached_send(ptr
, key
, key_length
,
172 key
, key_length
, value
, value_length
,
173 expiration
, flags
, 0, ADD_OP
);
174 LIBMEMCACHED_MEMCACHED_ADD_END();
178 memcached_return
memcached_replace(memcached_st
*ptr
,
179 const char *key
, size_t key_length
,
180 const char *value
, size_t value_length
,
185 LIBMEMCACHED_MEMCACHED_REPLACE_START();
186 rc
= memcached_send(ptr
, key
, key_length
,
187 key
, key_length
, value
, value_length
,
188 expiration
, flags
, 0, REPLACE_OP
);
189 LIBMEMCACHED_MEMCACHED_REPLACE_END();
193 memcached_return
memcached_prepend(memcached_st
*ptr
,
194 const char *key
, size_t key_length
,
195 const char *value
, size_t value_length
,
200 rc
= memcached_send(ptr
, key
, key_length
,
201 key
, key_length
, value
, value_length
,
202 expiration
, flags
, 0, PREPEND_OP
);
206 memcached_return
memcached_append(memcached_st
*ptr
,
207 const char *key
, size_t key_length
,
208 const char *value
, size_t value_length
,
213 rc
= memcached_send(ptr
, key
, key_length
,
214 key
, key_length
, value
, value_length
,
215 expiration
, flags
, 0, APPEND_OP
);
219 memcached_return
memcached_cas(memcached_st
*ptr
,
220 const char *key
, size_t key_length
,
221 const char *value
, size_t value_length
,
227 rc
= memcached_send(ptr
, key
, key_length
,
228 key
, key_length
, value
, value_length
,
229 expiration
, flags
, cas
, CAS_OP
);
233 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
234 const char *master_key
__attribute__((unused
)),
235 size_t master_key_length
__attribute__((unused
)),
236 const char *key
, size_t key_length
,
237 const char *value
, size_t value_length
,
242 LIBMEMCACHED_MEMCACHED_SET_START();
243 rc
= memcached_send(ptr
, master_key
, master_key_length
,
244 key
, key_length
, value
, value_length
,
245 expiration
, flags
, 0, SET_OP
);
246 LIBMEMCACHED_MEMCACHED_SET_END();
250 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
251 const char *master_key
, size_t master_key_length
,
252 const char *key
, size_t key_length
,
253 const char *value
, size_t value_length
,
258 LIBMEMCACHED_MEMCACHED_ADD_START();
259 rc
= memcached_send(ptr
, master_key
, master_key_length
,
260 key
, key_length
, value
, value_length
,
261 expiration
, flags
, 0, ADD_OP
);
262 LIBMEMCACHED_MEMCACHED_ADD_END();
266 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
267 const char *master_key
, size_t master_key_length
,
268 const char *key
, size_t key_length
,
269 const char *value
, size_t value_length
,
274 LIBMEMCACHED_MEMCACHED_REPLACE_START();
275 rc
= memcached_send(ptr
, master_key
, master_key_length
,
276 key
, key_length
, value
, value_length
,
277 expiration
, flags
, 0, REPLACE_OP
);
278 LIBMEMCACHED_MEMCACHED_REPLACE_END();
282 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
283 const char *master_key
, size_t master_key_length
,
284 const char *key
, size_t key_length
,
285 const char *value
, size_t value_length
,
290 rc
= memcached_send(ptr
, master_key
, master_key_length
,
291 key
, key_length
, value
, value_length
,
292 expiration
, flags
, 0, PREPEND_OP
);
296 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
297 const char *master_key
, size_t master_key_length
,
298 const char *key
, size_t key_length
,
299 const char *value
, size_t value_length
,
304 rc
= memcached_send(ptr
, master_key
, master_key_length
,
305 key
, key_length
, value
, value_length
,
306 expiration
, flags
, 0, APPEND_OP
);
310 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
311 const char *master_key
, size_t master_key_length
,
312 const char *key
, size_t key_length
,
313 const char *value
, size_t value_length
,
319 rc
= memcached_send(ptr
, master_key
, master_key_length
,
320 key
, key_length
, value
, value_length
,
321 expiration
, flags
, cas
, CAS_OP
);
325 static memcached_return
memcached_send_binary(memcached_server_st
* server
,
333 memcached_storage_action verb
)
336 protocol_binary_request_set request
= {.bytes
= {0}};
337 size_t send_length
= sizeof(request
.bytes
);
339 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
343 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SET
;
346 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_ADD
;
349 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
352 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_APPEND
;
355 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_PREPEND
;
358 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_REPLACE
;
362 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
363 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
364 if (verb
== APPEND_OP
|| verb
== PREPEND_OP
)
365 send_length
-= 8; /* append & prepend does not contain extras! */
368 request
.message
.header
.request
.extlen
= 8;
369 request
.message
.body
.flags
= htonl(flags
);
370 request
.message
.body
.expiration
= htonl((uint32_t)expiration
);
373 request
.message
.header
.request
.bodylen
= htonl(key_length
+ value_length
+
374 request
.message
.header
.request
.extlen
);
377 request
.message
.header
.request
.cas
= htonll(cas
);
379 flush
= ((server
->root
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
) ? 0 : 1;
380 /* write the header */
381 if ((memcached_do(server
, (const char*)request
.bytes
, send_length
, 0) != MEMCACHED_SUCCESS
) ||
382 (memcached_io_write(server
, key
, key_length
, 0) == -1) ||
383 (memcached_io_write(server
, value
, value_length
, flush
) == -1))
385 memcached_io_reset(server
);
386 return MEMCACHED_WRITE_FAILURE
;
390 return MEMCACHED_BUFFERED
;
392 return memcached_response(server
, NULL
, 0, NULL
);