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
);
78 if (rc
!= MEMCACHED_SUCCESS
)
80 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
84 So why recheck responce? Because the protocol is brain dead :)
85 The number returned might end up equaling one of the string
86 values. Less chance of a mistake with strncmp() so we will
87 use it. We still called memcached_response() though since it
88 worked its magic for non-blocking IO.
90 if (not strncmp(buffer
, memcached_literal_param("ERROR\r\n")))
93 rc
= MEMCACHED_PROTOCOL_ERROR
;
95 else if (not strncmp(buffer
, memcached_literal_param("CLIENT_ERROR\r\n")))
98 rc
= MEMCACHED_PROTOCOL_ERROR
;
100 else if (not strncmp(buffer
, memcached_literal_param("NOT_FOUND\r\n")))
103 rc
= MEMCACHED_NOTFOUND
;
107 *value
= strtoull(buffer
, (char **)NULL
, 10);
108 rc
= MEMCACHED_SUCCESS
;
111 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
114 static memcached_return_t
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
115 const char *group_key
, size_t group_key_length
,
116 const char *key
, size_t key_length
,
117 uint64_t offset
, uint64_t initial
,
121 bool no_reply
= ptr
->flags
.no_reply
;
123 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
124 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
128 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
129 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
131 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
132 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
134 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
136 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
137 request
.message
.header
.request
.opcode
= cmd
;
138 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->_namespace
)));
139 request
.message
.header
.request
.extlen
= 20;
140 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
141 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->_namespace
) +request
.message
.header
.request
.extlen
));
142 request
.message
.body
.delta
= memcached_htonll(offset
);
143 request
.message
.body
.initial
= memcached_htonll(initial
);
144 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
146 struct libmemcached_io_vector_st vector
[]=
148 { request
.bytes
, sizeof(request
.bytes
) },
149 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
153 memcached_return_t rc
;
154 if (memcached_failed(rc
= memcached_vdo(instance
, vector
, 3, true)))
156 memcached_io_reset(instance
);
157 return (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
162 return MEMCACHED_SUCCESS
;
165 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
168 memcached_return_t
memcached_increment(memcached_st
*ptr
,
169 const char *key
, size_t key_length
,
173 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
176 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
177 const char *key
, size_t key_length
,
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
;
197 if (memcached_failed(rc
= initialize_query(ptr
)))
202 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
207 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
208 if (ptr
->flags
.binary_protocol
)
210 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
211 group_key
, group_key_length
, key
, key_length
,
212 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
217 rc
= text_incr_decr(ptr
, "incr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
220 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
225 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
226 const char *group_key
, size_t group_key_length
,
227 const char *key
, size_t key_length
,
231 uint64_t local_value
;
237 memcached_return_t rc
;
238 if (memcached_failed(rc
= initialize_query(ptr
)))
243 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
249 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
250 if (ptr
->flags
.binary_protocol
)
252 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
253 group_key
, group_key_length
, key
, key_length
,
254 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
259 rc
= text_incr_decr(ptr
, "decr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
262 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
267 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
275 uint64_t local_value
;
281 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
283 offset
, initial
, expiration
, value
);
286 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
287 const char *group_key
,
288 size_t group_key_length
,
296 uint64_t local_value
;
302 memcached_return_t rc
;
303 if (memcached_failed(rc
= initialize_query(ptr
)))
308 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
313 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
314 if (ptr
->flags
.binary_protocol
)
316 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
317 group_key
, group_key_length
, key
, key_length
,
318 offset
, initial
, (uint32_t)expiration
,
323 rc
= MEMCACHED_PROTOCOL_ERROR
;
326 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
331 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
339 uint64_t local_value
;
345 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
347 offset
, initial
, expiration
, value
);
350 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
351 const char *group_key
,
352 size_t group_key_length
,
360 uint64_t local_value
;
366 memcached_return_t rc
;
367 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
372 if (memcached_failed(rc
= initialize_query(ptr
)))
378 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
379 if (ptr
->flags
.binary_protocol
)
381 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
382 group_key
, group_key_length
, key
, key_length
,
383 offset
, initial
, (uint32_t)expiration
,
388 rc
= MEMCACHED_PROTOCOL_ERROR
;
391 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();