1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <libmemcached/common.h>
40 static memcached_return_t
text_incr_decr(memcached_st
*ptr
,
42 const char *group_key
, size_t group_key_length
,
43 const char *key
, size_t key_length
,
47 memcached_return_t rc
;
48 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
50 memcached_server_write_instance_st instance
;
51 bool no_reply
= ptr
->flags
.no_reply
;
53 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
54 return memcached_set_error(ptr
, MEMCACHED_BAD_KEY_PROVIDED
);
56 server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
57 instance
= memcached_server_instance_fetch(ptr
, server_key
);
60 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
61 "%s %.*s%.*s %" PRIu64
"%s\r\n", verb
,
62 memcached_print_array(ptr
->prefix_key
),
64 offset
, no_reply
? " noreply" : "");
65 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
66 return memcached_set_error(ptr
, MEMCACHED_WRITE_FAILURE
);
68 rc
= memcached_do(instance
, buffer
, (size_t)send_length
, true);
69 if (no_reply
|| rc
!= MEMCACHED_SUCCESS
)
72 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
75 So why recheck responce? Because the protocol is brain dead :)
76 The number returned might end up equaling one of the string
77 values. Less chance of a mistake with strncmp() so we will
78 use it. We still called memcached_response() though since it
79 worked its magic for non-blocking IO.
81 if (! strncmp(buffer
, "ERROR\r\n", 7))
84 rc
= MEMCACHED_PROTOCOL_ERROR
;
86 else if (! strncmp(buffer
, "CLIENT_ERROR\r\n", 14))
89 rc
= MEMCACHED_PROTOCOL_ERROR
;
91 else if (!strncmp(buffer
, "NOT_FOUND\r\n", 11))
94 rc
= MEMCACHED_NOTFOUND
;
98 *value
= strtoull(buffer
, (char **)NULL
, 10);
99 rc
= MEMCACHED_SUCCESS
;
105 static memcached_return_t
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
106 const char *group_key
, size_t group_key_length
,
107 const char *key
, size_t key_length
,
108 uint64_t offset
, uint64_t initial
,
113 memcached_server_write_instance_st instance
;
114 bool no_reply
= ptr
->flags
.no_reply
;
116 if (memcached_server_count(ptr
) == 0)
117 return memcached_set_error(ptr
, MEMCACHED_NO_SERVERS
);
119 server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
120 instance
= memcached_server_instance_fetch(ptr
, server_key
);
124 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
125 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
126 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
127 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
129 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
131 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
132 request
.message
.header
.request
.opcode
= cmd
;
133 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->prefix_key
)));
134 request
.message
.header
.request
.extlen
= 20;
135 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
136 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->prefix_key
) +request
.message
.header
.request
.extlen
));
137 request
.message
.body
.delta
= htonll(offset
);
138 request
.message
.body
.initial
= htonll(initial
);
139 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
141 struct libmemcached_io_vector_st vector
[]=
143 { sizeof(request
.bytes
), request
.bytes
},
144 { memcached_array_size(ptr
->prefix_key
), ptr
->prefix_key
},
148 memcached_return_t rc
;
149 if ((rc
= memcached_vdo(instance
, vector
, 3, true)) != MEMCACHED_SUCCESS
)
151 memcached_io_reset(instance
);
152 return (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
156 return MEMCACHED_SUCCESS
;
158 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
161 memcached_return_t
memcached_increment(memcached_st
*ptr
,
162 const char *key
, size_t key_length
,
166 uint64_t local_value
;
170 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
173 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
174 const char *key
, size_t key_length
,
178 uint64_t local_value
;
182 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
185 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
186 const char *group_key
, size_t group_key_length
,
187 const char *key
, size_t key_length
,
191 memcached_return_t rc
;
192 uint64_t local_value
;
196 if (memcached_failed(rc
= initialize_query(ptr
)))
201 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
206 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
207 if (ptr
->flags
.binary_protocol
)
209 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
210 group_key
, group_key_length
, key
, key_length
,
211 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
216 rc
= text_incr_decr(ptr
, "incr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
219 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
224 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
225 const char *group_key
, size_t group_key_length
,
226 const char *key
, size_t key_length
,
230 uint64_t local_value
;
234 memcached_return_t rc
;
235 if (memcached_failed(rc
= initialize_query(ptr
)))
240 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
246 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
247 if (ptr
->flags
.binary_protocol
)
249 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
250 group_key
, group_key_length
, key
, key_length
,
251 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
256 rc
= text_incr_decr(ptr
, "decr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
259 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
264 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
272 uint64_t local_value
;
276 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
278 offset
, initial
, expiration
, value
);
281 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
282 const char *group_key
,
283 size_t group_key_length
,
291 uint64_t local_value
;
295 memcached_return_t rc
;
296 if (memcached_failed(rc
= initialize_query(ptr
)))
301 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
306 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
307 if (ptr
->flags
.binary_protocol
)
308 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
309 group_key
, group_key_length
, key
, key_length
,
310 offset
, initial
, (uint32_t)expiration
,
313 rc
= MEMCACHED_PROTOCOL_ERROR
;
315 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
320 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
328 uint64_t local_value
;
332 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
334 offset
, initial
, expiration
, value
);
337 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
338 const char *group_key
,
339 size_t group_key_length
,
347 uint64_t local_value
;
351 memcached_return_t rc
;
352 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
357 if (memcached_failed(rc
= initialize_query(ptr
)))
363 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
364 if (ptr
->flags
.binary_protocol
)
366 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
367 group_key
, group_key_length
, key
, key_length
,
368 offset
, initial
, (uint32_t)expiration
,
373 rc
= MEMCACHED_PROTOCOL_ERROR
;
376 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();