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 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
49 memcached_server_write_instance_st instance
;
51 // Invert the logic to make it simpler to read the code
52 bool reply
= (ptr
->flags
.no_reply
) ? false : true;
54 if (memcached_failed(memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
56 return memcached_set_error(*ptr
, MEMCACHED_BAD_KEY_PROVIDED
, MEMCACHED_AT
);
59 server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
60 instance
= memcached_server_instance_fetch(ptr
, server_key
);
62 int send_length
= snprintf(buffer
, sizeof(buffer
), " %" PRIu64
, offset
);
63 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
65 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
66 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
69 struct libmemcached_io_vector_st vector
[]=
71 { memcached_literal_param("incr ") },
72 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
74 { buffer
, send_length
},
75 { " noreply", reply
? 0 : memcached_literal_param_size(" noreply") },
76 { memcached_literal_param("\r\n") }
81 vector
[0].buffer
= "decr ";
84 memcached_return_t rc
= memcached_vdo(instance
, vector
, 6, true);
85 if (reply
== false or memcached_failed(rc
))
90 rc
= memcached_response(instance
, buffer
, sizeof(buffer
), NULL
);
92 if (rc
!= MEMCACHED_SUCCESS
)
94 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
98 So why recheck responce? Because the protocol is brain dead :)
99 The number returned might end up equaling one of the string
100 values. Less chance of a mistake with strncmp() so we will
101 use it. We still called memcached_response() though since it
102 worked its magic for non-blocking IO.
104 if (not strncmp(buffer
, memcached_literal_param("ERROR\r\n")))
107 rc
= MEMCACHED_PROTOCOL_ERROR
;
109 else if (not strncmp(buffer
, memcached_literal_param("CLIENT_ERROR\r\n")))
112 rc
= MEMCACHED_PROTOCOL_ERROR
;
114 else if (not strncmp(buffer
, memcached_literal_param("NOT_FOUND\r\n")))
117 rc
= MEMCACHED_NOTFOUND
;
121 *value
= strtoull(buffer
, (char **)NULL
, 10);
122 rc
= MEMCACHED_SUCCESS
;
125 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
128 static memcached_return_t
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
129 const char *group_key
, size_t group_key_length
,
130 const char *key
, size_t key_length
,
131 uint64_t offset
, uint64_t initial
,
135 bool no_reply
= ptr
->flags
.no_reply
;
137 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
138 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
142 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
143 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
145 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
146 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
148 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
150 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
151 request
.message
.header
.request
.opcode
= cmd
;
152 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->_namespace
)));
153 request
.message
.header
.request
.extlen
= 20;
154 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
155 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->_namespace
) +request
.message
.header
.request
.extlen
));
156 request
.message
.body
.delta
= memcached_htonll(offset
);
157 request
.message
.body
.initial
= memcached_htonll(initial
);
158 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
160 struct libmemcached_io_vector_st vector
[]=
162 { request
.bytes
, sizeof(request
.bytes
) },
163 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
167 memcached_return_t rc
;
168 if (memcached_failed(rc
= memcached_vdo(instance
, vector
, 3, true)))
170 memcached_io_reset(instance
);
171 return (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
176 return MEMCACHED_SUCCESS
;
179 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
182 memcached_return_t
memcached_increment(memcached_st
*ptr
,
183 const char *key
, size_t key_length
,
187 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
190 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
191 const char *key
, size_t key_length
,
195 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
198 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
199 const char *group_key
, size_t group_key_length
,
200 const char *key
, size_t key_length
,
204 memcached_return_t rc
;
205 uint64_t local_value
;
211 if (memcached_failed(rc
= initialize_query(ptr
)))
216 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
221 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
222 if (ptr
->flags
.binary_protocol
)
224 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
225 group_key
, group_key_length
, key
, key_length
,
226 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
231 rc
= text_incr_decr(ptr
, true, group_key
, group_key_length
, key
, key_length
, offset
, value
);
234 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
239 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
240 const char *group_key
, size_t group_key_length
,
241 const char *key
, size_t key_length
,
245 uint64_t local_value
;
251 memcached_return_t rc
;
252 if (memcached_failed(rc
= initialize_query(ptr
)))
257 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
263 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
264 if (ptr
->flags
.binary_protocol
)
266 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
267 group_key
, group_key_length
, key
, key_length
,
268 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
273 rc
= text_incr_decr(ptr
, false, group_key
, group_key_length
, key
, key_length
, offset
, value
);
276 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
281 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
289 uint64_t local_value
;
295 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
297 offset
, initial
, expiration
, value
);
300 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
301 const char *group_key
,
302 size_t group_key_length
,
310 uint64_t local_value
;
316 memcached_return_t rc
;
317 if (memcached_failed(rc
= initialize_query(ptr
)))
322 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
327 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
328 if (ptr
->flags
.binary_protocol
)
330 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
331 group_key
, group_key_length
, key
, key_length
,
332 offset
, initial
, (uint32_t)expiration
,
337 rc
= MEMCACHED_PROTOCOL_ERROR
;
340 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
345 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
353 uint64_t local_value
;
359 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
361 offset
, initial
, expiration
, value
);
364 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
365 const char *group_key
,
366 size_t group_key_length
,
374 uint64_t local_value
;
380 memcached_return_t rc
;
381 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
386 if (memcached_failed(rc
= initialize_query(ptr
)))
392 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
393 if (ptr
->flags
.binary_protocol
)
395 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
396 group_key
, group_key_length
, key
, key_length
,
397 offset
, initial
, (uint32_t)expiration
,
402 rc
= MEMCACHED_PROTOCOL_ERROR
;
405 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();