Fix for solaris.
[m6w6/libmemcached] / libmemcached / storage.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: Storage related functions, aka set, replace,..
9 *
10 */
11
12 #include "common.h"
13
14 typedef enum {
15 SET_OP,
16 REPLACE_OP,
17 ADD_OP,
18 PREPEND_OP,
19 APPEND_OP,
20 CAS_OP,
21 } memcached_storage_action_t;
22
23 /* Inline this */
24 static inline const char *storage_op_string(memcached_storage_action_t verb)
25 {
26 switch (verb)
27 {
28 case SET_OP:
29 return "set ";
30 case REPLACE_OP:
31 return "replace ";
32 case ADD_OP:
33 return "add ";
34 case PREPEND_OP:
35 return "prepend ";
36 case APPEND_OP:
37 return "append ";
38 case CAS_OP:
39 return "cas ";
40 default:
41 return "tosserror"; /* This is impossible, fixes issue for compiler warning in VisualStudio */
42 }
43
44 /* NOTREACHED */
45 }
46
47 static memcached_return_t memcached_send_binary(memcached_st *ptr,
48 const char *master_key,
49 size_t master_key_length,
50 const char *key,
51 size_t key_length,
52 const char *value,
53 size_t value_length,
54 time_t expiration,
55 uint32_t flags,
56 uint64_t cas,
57 memcached_storage_action_t verb);
58
59 static inline memcached_return_t memcached_send(memcached_st *ptr,
60 const char *master_key, size_t master_key_length,
61 const char *key, size_t key_length,
62 const char *value, size_t value_length,
63 time_t expiration,
64 uint32_t flags,
65 uint64_t cas,
66 memcached_storage_action_t verb)
67 {
68 char to_write;
69 size_t write_length;
70 ssize_t sent_length;
71 memcached_return_t rc;
72 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
73 unsigned int server_key;
74
75 WATCHPOINT_ASSERT(!(value == NULL && value_length > 0));
76
77 rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
78 unlikely (rc != MEMCACHED_SUCCESS)
79 return rc;
80
81 unlikely (ptr->number_of_hosts == 0)
82 return MEMCACHED_NO_SERVERS;
83
84 if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
85 return MEMCACHED_BAD_KEY_PROVIDED;
86
87 if (ptr->flags.binary_protocol)
88 {
89 return memcached_send_binary(ptr, master_key, master_key_length,
90 key, key_length,
91 value, value_length, expiration,
92 flags, cas, verb);
93 }
94
95 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
96
97 if (cas)
98 {
99 write_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
100 "%s %s%.*s %u %llu %zu %llu%s\r\n",
101 storage_op_string(verb),
102 ptr->prefix_key,
103 (int)key_length, key, flags,
104 (unsigned long long)expiration, value_length,
105 (unsigned long long)cas,
106 (ptr->flags.no_reply) ? " noreply" : "");
107 }
108 else
109 {
110 char *buffer_ptr= buffer;
111 const char *command= storage_op_string(verb);
112
113 /* Copy in the command, no space needed, we handle that in the command function*/
114 memcpy(buffer_ptr, command, strlen(command));
115
116 /* Copy in the key prefix, switch to the buffer_ptr */
117 buffer_ptr= memcpy(buffer_ptr + strlen(command) , ptr->prefix_key, strlen(ptr->prefix_key));
118
119 /* Copy in the key, adjust point if a key prefix was used. */
120 buffer_ptr= memcpy(buffer_ptr + (ptr->prefix_key ? strlen(ptr->prefix_key) : 0),
121 key, key_length);
122 buffer_ptr+= key_length;
123 buffer_ptr[0]= ' ';
124 buffer_ptr++;
125
126 write_length= (size_t)(buffer_ptr - buffer);
127 write_length+= (size_t) snprintf(buffer_ptr, MEMCACHED_DEFAULT_COMMAND_SIZE,
128 "%u %llu %zu%s\r\n",
129 flags,
130 (unsigned long long)expiration, value_length,
131 ptr->flags.no_reply ? " noreply" : "");
132 }
133
134 if (ptr->flags.use_udp && ptr->flags.buffer_requests)
135 {
136 size_t cmd_size= write_length + value_length + 2;
137 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
138 return MEMCACHED_WRITE_FAILURE;
139 if (cmd_size + ptr->hosts[server_key].write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
140 memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1);
141 }
142
143 if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
144 {
145 rc= MEMCACHED_WRITE_FAILURE;
146 goto error;
147 }
148
149 /* Send command header */
150 rc= memcached_do(&ptr->hosts[server_key], buffer, write_length, 0);
151 if (rc != MEMCACHED_SUCCESS)
152 goto error;
153
154 /* Send command body */
155 if ((sent_length= memcached_io_write(&ptr->hosts[server_key], value, value_length, 0)) == -1)
156 {
157 rc= MEMCACHED_WRITE_FAILURE;
158 goto error;
159 }
160
161 if (ptr->flags.buffer_requests && verb == SET_OP)
162 {
163 to_write= 0;
164 }
165 else
166 {
167 to_write= 1;
168 }
169
170 if ((sent_length= memcached_io_write(&ptr->hosts[server_key], "\r\n", 2, to_write)) == -1)
171 {
172 rc= MEMCACHED_WRITE_FAILURE;
173 goto error;
174 }
175
176 if (ptr->flags.no_reply)
177 return (to_write == 0) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS;
178
179 if (to_write == 0)
180 return MEMCACHED_BUFFERED;
181
182 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
183
184 if (rc == MEMCACHED_STORED)
185 return MEMCACHED_SUCCESS;
186 else
187 return rc;
188
189 error:
190 memcached_io_reset(&ptr->hosts[server_key]);
191
192 return rc;
193 }
194
195
196 memcached_return_t memcached_set(memcached_st *ptr, const char *key, size_t key_length,
197 const char *value, size_t value_length,
198 time_t expiration,
199 uint32_t flags)
200 {
201 memcached_return_t rc;
202 LIBMEMCACHED_MEMCACHED_SET_START();
203 rc= memcached_send(ptr, key, key_length,
204 key, key_length, value, value_length,
205 expiration, flags, 0, SET_OP);
206 LIBMEMCACHED_MEMCACHED_SET_END();
207 return rc;
208 }
209
210 memcached_return_t memcached_add(memcached_st *ptr,
211 const char *key, size_t key_length,
212 const char *value, size_t value_length,
213 time_t expiration,
214 uint32_t flags)
215 {
216 memcached_return_t rc;
217 LIBMEMCACHED_MEMCACHED_ADD_START();
218 rc= memcached_send(ptr, key, key_length,
219 key, key_length, value, value_length,
220 expiration, flags, 0, ADD_OP);
221 LIBMEMCACHED_MEMCACHED_ADD_END();
222 return rc;
223 }
224
225 memcached_return_t memcached_replace(memcached_st *ptr,
226 const char *key, size_t key_length,
227 const char *value, size_t value_length,
228 time_t expiration,
229 uint32_t flags)
230 {
231 memcached_return_t rc;
232 LIBMEMCACHED_MEMCACHED_REPLACE_START();
233 rc= memcached_send(ptr, key, key_length,
234 key, key_length, value, value_length,
235 expiration, flags, 0, REPLACE_OP);
236 LIBMEMCACHED_MEMCACHED_REPLACE_END();
237 return rc;
238 }
239
240 memcached_return_t memcached_prepend(memcached_st *ptr,
241 const char *key, size_t key_length,
242 const char *value, size_t value_length,
243 time_t expiration,
244 uint32_t flags)
245 {
246 memcached_return_t rc;
247 rc= memcached_send(ptr, key, key_length,
248 key, key_length, value, value_length,
249 expiration, flags, 0, PREPEND_OP);
250 return rc;
251 }
252
253 memcached_return_t memcached_append(memcached_st *ptr,
254 const char *key, size_t key_length,
255 const char *value, size_t value_length,
256 time_t expiration,
257 uint32_t flags)
258 {
259 memcached_return_t rc;
260 rc= memcached_send(ptr, key, key_length,
261 key, key_length, value, value_length,
262 expiration, flags, 0, APPEND_OP);
263 return rc;
264 }
265
266 memcached_return_t memcached_cas(memcached_st *ptr,
267 const char *key, size_t key_length,
268 const char *value, size_t value_length,
269 time_t expiration,
270 uint32_t flags,
271 uint64_t cas)
272 {
273 memcached_return_t rc;
274 rc= memcached_send(ptr, key, key_length,
275 key, key_length, value, value_length,
276 expiration, flags, cas, CAS_OP);
277 return rc;
278 }
279
280 memcached_return_t memcached_set_by_key(memcached_st *ptr,
281 const char *master_key __attribute__((unused)),
282 size_t master_key_length __attribute__((unused)),
283 const char *key, size_t key_length,
284 const char *value, size_t value_length,
285 time_t expiration,
286 uint32_t flags)
287 {
288 memcached_return_t rc;
289 LIBMEMCACHED_MEMCACHED_SET_START();
290 rc= memcached_send(ptr, master_key, master_key_length,
291 key, key_length, value, value_length,
292 expiration, flags, 0, SET_OP);
293 LIBMEMCACHED_MEMCACHED_SET_END();
294 return rc;
295 }
296
297 memcached_return_t memcached_add_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,
301 time_t expiration,
302 uint32_t flags)
303 {
304 memcached_return_t rc;
305 LIBMEMCACHED_MEMCACHED_ADD_START();
306 rc= memcached_send(ptr, master_key, master_key_length,
307 key, key_length, value, value_length,
308 expiration, flags, 0, ADD_OP);
309 LIBMEMCACHED_MEMCACHED_ADD_END();
310 return rc;
311 }
312
313 memcached_return_t memcached_replace_by_key(memcached_st *ptr,
314 const char *master_key, size_t master_key_length,
315 const char *key, size_t key_length,
316 const char *value, size_t value_length,
317 time_t expiration,
318 uint32_t flags)
319 {
320 memcached_return_t rc;
321 LIBMEMCACHED_MEMCACHED_REPLACE_START();
322 rc= memcached_send(ptr, master_key, master_key_length,
323 key, key_length, value, value_length,
324 expiration, flags, 0, REPLACE_OP);
325 LIBMEMCACHED_MEMCACHED_REPLACE_END();
326 return rc;
327 }
328
329 memcached_return_t memcached_prepend_by_key(memcached_st *ptr,
330 const char *master_key, size_t master_key_length,
331 const char *key, size_t key_length,
332 const char *value, size_t value_length,
333 time_t expiration,
334 uint32_t flags)
335 {
336 memcached_return_t rc;
337 rc= memcached_send(ptr, master_key, master_key_length,
338 key, key_length, value, value_length,
339 expiration, flags, 0, PREPEND_OP);
340 return rc;
341 }
342
343 memcached_return_t memcached_append_by_key(memcached_st *ptr,
344 const char *master_key, size_t master_key_length,
345 const char *key, size_t key_length,
346 const char *value, size_t value_length,
347 time_t expiration,
348 uint32_t flags)
349 {
350 memcached_return_t rc;
351 rc= memcached_send(ptr, master_key, master_key_length,
352 key, key_length, value, value_length,
353 expiration, flags, 0, APPEND_OP);
354 return rc;
355 }
356
357 memcached_return_t memcached_cas_by_key(memcached_st *ptr,
358 const char *master_key, size_t master_key_length,
359 const char *key, size_t key_length,
360 const char *value, size_t value_length,
361 time_t expiration,
362 uint32_t flags,
363 uint64_t cas)
364 {
365 memcached_return_t rc;
366 rc= memcached_send(ptr, master_key, master_key_length,
367 key, key_length, value, value_length,
368 expiration, flags, cas, CAS_OP);
369 return rc;
370 }
371
372 static inline uint8_t get_com_code(memcached_storage_action_t verb, bool noreply)
373 {
374 /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
375 * be used uninitialized in this function. FAIL */
376 uint8_t ret= 0;
377
378 if (noreply)
379 switch (verb)
380 {
381 case SET_OP:
382 ret=PROTOCOL_BINARY_CMD_SETQ;
383 break;
384 case ADD_OP:
385 ret=PROTOCOL_BINARY_CMD_ADDQ;
386 break;
387 case CAS_OP: /* FALLTHROUGH */
388 case REPLACE_OP:
389 ret=PROTOCOL_BINARY_CMD_REPLACEQ;
390 break;
391 case APPEND_OP:
392 ret=PROTOCOL_BINARY_CMD_APPENDQ;
393 break;
394 case PREPEND_OP:
395 ret=PROTOCOL_BINARY_CMD_PREPENDQ;
396 break;
397 default:
398 WATCHPOINT_ASSERT(verb);
399 break;
400 }
401 else
402 switch (verb)
403 {
404 case SET_OP:
405 ret=PROTOCOL_BINARY_CMD_SET;
406 break;
407 case ADD_OP:
408 ret=PROTOCOL_BINARY_CMD_ADD;
409 break;
410 case CAS_OP: /* FALLTHROUGH */
411 case REPLACE_OP:
412 ret=PROTOCOL_BINARY_CMD_REPLACE;
413 break;
414 case APPEND_OP:
415 ret=PROTOCOL_BINARY_CMD_APPEND;
416 break;
417 case PREPEND_OP:
418 ret=PROTOCOL_BINARY_CMD_PREPEND;
419 break;
420 default:
421 WATCHPOINT_ASSERT(verb);
422 break;
423 }
424
425 return ret;
426 }
427
428
429
430 static memcached_return_t memcached_send_binary(memcached_st *ptr,
431 const char *master_key,
432 size_t master_key_length,
433 const char *key,
434 size_t key_length,
435 const char *value,
436 size_t value_length,
437 time_t expiration,
438 uint32_t flags,
439 uint64_t cas,
440 memcached_storage_action_t verb)
441 {
442 uint8_t flush;
443 protocol_binary_request_set request= {.bytes= {0}};
444 size_t send_length= sizeof(request.bytes);
445 uint32_t server_key= memcached_generate_hash(ptr, master_key,
446 master_key_length);
447 memcached_server_st *server= &ptr->hosts[server_key];
448 bool noreply= server->root->flags.no_reply;
449
450 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
451 request.message.header.request.opcode= get_com_code(verb, noreply);
452 request.message.header.request.keylen= htons((uint16_t)key_length);
453 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
454 if (verb == APPEND_OP || verb == PREPEND_OP)
455 send_length -= 8; /* append & prepend does not contain extras! */
456 else
457 {
458 request.message.header.request.extlen= 8;
459 request.message.body.flags= htonl(flags);
460 request.message.body.expiration= htonl((uint32_t)expiration);
461 }
462
463 request.message.header.request.bodylen= htonl((uint32_t) (key_length + value_length +
464 request.message.header.request.extlen));
465
466 if (cas)
467 request.message.header.request.cas= htonll(cas);
468
469 flush= (uint8_t) ((server->root->flags.buffer_requests && verb == SET_OP) ? 0 : 1);
470
471 if (server->root->flags.use_udp && !flush)
472 {
473 size_t cmd_size= send_length + key_length + value_length;
474
475 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
476 return MEMCACHED_WRITE_FAILURE;
477 if (cmd_size + server->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
478 memcached_io_write(server,NULL,0, 1);
479 }
480
481 /* write the header */
482 if ((memcached_do(server, (const char*)request.bytes, send_length, 0) != MEMCACHED_SUCCESS) ||
483 (memcached_io_write(server, key, key_length, 0) == -1) ||
484 (memcached_io_write(server, value, value_length, (char) flush) == -1))
485 {
486 memcached_io_reset(server);
487 return MEMCACHED_WRITE_FAILURE;
488 }
489
490 unlikely (verb == SET_OP && ptr->number_of_replicas > 0)
491 {
492 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
493
494 for (uint32_t x= 0; x < ptr->number_of_replicas; x++)
495 {
496 ++server_key;
497 if (server_key == ptr->number_of_hosts)
498 server_key= 0;
499
500 memcached_server_st *srv= &ptr->hosts[server_key];
501 if ((memcached_do(srv, (const char*)request.bytes,
502 send_length, 0) != MEMCACHED_SUCCESS) ||
503 (memcached_io_write(srv, key, key_length, 0) == -1) ||
504 (memcached_io_write(srv, value, value_length, (char) flush) == -1))
505 memcached_io_reset(srv);
506 else
507 memcached_server_response_decrement(srv);
508 }
509 }
510
511 if (flush == 0)
512 return MEMCACHED_BUFFERED;
513
514 if (noreply)
515 return MEMCACHED_SUCCESS;
516
517 return memcached_response(server, NULL, 0, NULL);
518 }
519