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 (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
53 return memcached_set_error(ptr
, MEMCACHED_BAD_KEY_PROVIDED
);
55 server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
56 instance
= memcached_server_instance_fetch(ptr
, server_key
);
59 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
60 "%s %.*s%.*s %" PRIu64
"%s\r\n", verb
,
61 memcached_print_array(ptr
->prefix_key
),
63 offset
, no_reply
? " noreply" : "");
64 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
65 return memcached_set_error_string(ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
67 memcached_return_t rc
= memcached_do(instance
, buffer
, (size_t)send_length
, true);
68 if (no_reply
or memcached_failed(rc
))
71 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
74 So why recheck responce? Because the protocol is brain dead :)
75 The number returned might end up equaling one of the string
76 values. Less chance of a mistake with strncmp() so we will
77 use it. We still called memcached_response() though since it
78 worked its magic for non-blocking IO.
80 if (not strncmp(buffer
, memcached_literal_param("ERROR\r\n")))
83 rc
= MEMCACHED_PROTOCOL_ERROR
;
85 else if (not strncmp(buffer
, memcached_literal_param("CLIENT_ERROR\r\n")))
88 rc
= MEMCACHED_PROTOCOL_ERROR
;
90 else if (not strncmp(buffer
, memcached_literal_param("NOT_FOUND\r\n")))
93 rc
= MEMCACHED_NOTFOUND
;
97 *value
= strtoull(buffer
, (char **)NULL
, 10);
98 rc
= MEMCACHED_SUCCESS
;
101 return memcached_set_error(*instance
, rc
);
104 static memcached_return_t
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
105 const char *group_key
, size_t group_key_length
,
106 const char *key
, size_t key_length
,
107 uint64_t offset
, uint64_t initial
,
112 memcached_server_write_instance_st instance
;
113 bool no_reply
= ptr
->flags
.no_reply
;
115 if (memcached_server_count(ptr
) == 0)
116 return memcached_set_error(ptr
, MEMCACHED_NO_SERVERS
);
118 server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
119 instance
= memcached_server_instance_fetch(ptr
, server_key
);
123 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
124 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
125 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
126 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
128 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
130 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
131 request
.message
.header
.request
.opcode
= cmd
;
132 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->prefix_key
)));
133 request
.message
.header
.request
.extlen
= 20;
134 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
135 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->prefix_key
) +request
.message
.header
.request
.extlen
));
136 request
.message
.body
.delta
= htonll(offset
);
137 request
.message
.body
.initial
= htonll(initial
);
138 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
140 struct libmemcached_io_vector_st vector
[]=
142 { sizeof(request
.bytes
), request
.bytes
},
143 { memcached_array_size(ptr
->prefix_key
), ptr
->prefix_key
},
147 memcached_return_t rc
;
148 if ((rc
= memcached_vdo(instance
, vector
, 3, true)) != MEMCACHED_SUCCESS
)
150 memcached_io_reset(instance
);
151 return (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
155 return MEMCACHED_SUCCESS
;
157 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
160 memcached_return_t
memcached_increment(memcached_st
*ptr
,
161 const char *key
, size_t key_length
,
165 uint64_t local_value
;
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 uint64_t local_value
;
181 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
184 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
185 const char *group_key
, size_t group_key_length
,
186 const char *key
, size_t key_length
,
190 memcached_return_t rc
;
191 uint64_t local_value
;
195 if (memcached_failed(rc
= initialize_query(ptr
)))
200 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
205 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
206 if (ptr
->flags
.binary_protocol
)
208 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
209 group_key
, group_key_length
, key
, key_length
,
210 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
215 rc
= text_incr_decr(ptr
, "incr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
218 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
223 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
224 const char *group_key
, size_t group_key_length
,
225 const char *key
, size_t key_length
,
229 uint64_t local_value
;
233 memcached_return_t rc
;
234 if (memcached_failed(rc
= initialize_query(ptr
)))
239 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
245 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
246 if (ptr
->flags
.binary_protocol
)
248 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
249 group_key
, group_key_length
, key
, key_length
,
250 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
255 rc
= text_incr_decr(ptr
, "decr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
258 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
263 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
271 uint64_t local_value
;
275 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
277 offset
, initial
, expiration
, value
);
280 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
281 const char *group_key
,
282 size_t group_key_length
,
290 uint64_t local_value
;
294 memcached_return_t rc
;
295 if (memcached_failed(rc
= initialize_query(ptr
)))
300 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
305 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
306 if (ptr
->flags
.binary_protocol
)
307 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
308 group_key
, group_key_length
, key
, key_length
,
309 offset
, initial
, (uint32_t)expiration
,
312 rc
= MEMCACHED_PROTOCOL_ERROR
;
314 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
319 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
327 uint64_t local_value
;
331 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
333 offset
, initial
, expiration
, value
);
336 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
337 const char *group_key
,
338 size_t group_key_length
,
346 uint64_t local_value
;
350 memcached_return_t rc
;
351 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
356 if (memcached_failed(rc
= initialize_query(ptr
)))
362 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
363 if (ptr
->flags
.binary_protocol
)
365 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
366 group_key
, group_key_length
, key
, key_length
,
367 offset
, initial
, (uint32_t)expiration
,
372 rc
= MEMCACHED_PROTOCOL_ERROR
;
375 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();