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
;
50 bool no_reply
= ptr
->flags
.no_reply
;
52 if (memcached_failed(memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
54 return memcached_set_error(*ptr
, MEMCACHED_BAD_KEY_PROVIDED
, MEMCACHED_AT
);
57 server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
58 instance
= memcached_server_instance_fetch(ptr
, server_key
);
61 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
62 "%s %.*s%.*s %" PRIu64
"%s\r\n", verb
,
63 memcached_print_array(ptr
->_namespace
),
65 offset
, no_reply
? " noreply" : "");
66 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
68 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
69 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
72 memcached_return_t rc
= memcached_do(instance
, buffer
, (size_t)send_length
, true);
73 if (no_reply
or memcached_failed(rc
))
76 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
79 So why recheck responce? Because the protocol is brain dead :)
80 The number returned might end up equaling one of the string
81 values. Less chance of a mistake with strncmp() so we will
82 use it. We still called memcached_response() though since it
83 worked its magic for non-blocking IO.
85 if (not strncmp(buffer
, memcached_literal_param("ERROR\r\n")))
88 rc
= MEMCACHED_PROTOCOL_ERROR
;
90 else if (not strncmp(buffer
, memcached_literal_param("CLIENT_ERROR\r\n")))
93 rc
= MEMCACHED_PROTOCOL_ERROR
;
95 else if (not strncmp(buffer
, memcached_literal_param("NOT_FOUND\r\n")))
98 rc
= MEMCACHED_NOTFOUND
;
102 *value
= strtoull(buffer
, (char **)NULL
, 10);
103 rc
= MEMCACHED_SUCCESS
;
106 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
109 static memcached_return_t
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
110 const char *group_key
, size_t group_key_length
,
111 const char *key
, size_t key_length
,
112 uint64_t offset
, uint64_t initial
,
116 bool no_reply
= ptr
->flags
.no_reply
;
118 if (memcached_server_count(ptr
) == 0)
119 return memcached_set_error(*ptr
, MEMCACHED_NO_SERVERS
, MEMCACHED_AT
);
121 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
122 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
126 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
127 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
129 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
130 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
132 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
134 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
135 request
.message
.header
.request
.opcode
= cmd
;
136 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->_namespace
)));
137 request
.message
.header
.request
.extlen
= 20;
138 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
139 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->_namespace
) +request
.message
.header
.request
.extlen
));
140 request
.message
.body
.delta
= memcached_htonll(offset
);
141 request
.message
.body
.initial
= memcached_htonll(initial
);
142 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
144 struct libmemcached_io_vector_st vector
[]=
146 { sizeof(request
.bytes
), request
.bytes
},
147 { memcached_array_size(ptr
->_namespace
), memcached_array_string(ptr
->_namespace
) },
151 memcached_return_t rc
;
152 if (memcached_failed(rc
= memcached_vdo(instance
, vector
, 3, true)))
154 memcached_io_reset(instance
);
155 return (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
159 return MEMCACHED_SUCCESS
;
161 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
164 memcached_return_t
memcached_increment(memcached_st
*ptr
,
165 const char *key
, size_t key_length
,
169 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
172 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
173 const char *key
, size_t key_length
,
177 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
180 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
181 const char *group_key
, size_t group_key_length
,
182 const char *key
, size_t key_length
,
186 memcached_return_t rc
;
187 uint64_t local_value
;
191 if (memcached_failed(rc
= initialize_query(ptr
)))
196 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
201 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
202 if (ptr
->flags
.binary_protocol
)
204 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
205 group_key
, group_key_length
, key
, key_length
,
206 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
211 rc
= text_incr_decr(ptr
, "incr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
214 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
219 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
220 const char *group_key
, size_t group_key_length
,
221 const char *key
, size_t key_length
,
225 uint64_t local_value
;
229 memcached_return_t rc
;
230 if (memcached_failed(rc
= initialize_query(ptr
)))
235 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
241 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
242 if (ptr
->flags
.binary_protocol
)
244 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
245 group_key
, group_key_length
, key
, key_length
,
246 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
251 rc
= text_incr_decr(ptr
, "decr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
254 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
259 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
267 uint64_t local_value
;
271 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
273 offset
, initial
, expiration
, value
);
276 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
277 const char *group_key
,
278 size_t group_key_length
,
286 uint64_t local_value
;
290 memcached_return_t rc
;
291 if (memcached_failed(rc
= initialize_query(ptr
)))
296 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
301 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
302 if (ptr
->flags
.binary_protocol
)
303 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
304 group_key
, group_key_length
, key
, key_length
,
305 offset
, initial
, (uint32_t)expiration
,
308 rc
= MEMCACHED_PROTOCOL_ERROR
;
310 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
315 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
323 uint64_t local_value
;
327 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
329 offset
, initial
, expiration
, value
);
332 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
333 const char *group_key
,
334 size_t group_key_length
,
342 uint64_t local_value
;
346 memcached_return_t rc
;
347 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
352 if (memcached_failed(rc
= initialize_query(ptr
)))
358 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
359 if (ptr
->flags
.binary_protocol
)
361 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
362 group_key
, group_key_length
, key
, key_length
,
363 offset
, initial
, (uint32_t)expiration
,
368 rc
= MEMCACHED_PROTOCOL_ERROR
;
371 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();