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
))
78 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
80 if (rc
!= MEMCACHED_SUCCESS
)
82 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
86 So why recheck responce? Because the protocol is brain dead :)
87 The number returned might end up equaling one of the string
88 values. Less chance of a mistake with strncmp() so we will
89 use it. We still called memcached_response() though since it
90 worked its magic for non-blocking IO.
92 if (not strncmp(buffer
, memcached_literal_param("ERROR\r\n")))
95 rc
= MEMCACHED_PROTOCOL_ERROR
;
97 else if (not strncmp(buffer
, memcached_literal_param("CLIENT_ERROR\r\n")))
100 rc
= MEMCACHED_PROTOCOL_ERROR
;
102 else if (not strncmp(buffer
, memcached_literal_param("NOT_FOUND\r\n")))
105 rc
= MEMCACHED_NOTFOUND
;
109 *value
= strtoull(buffer
, (char **)NULL
, 10);
110 rc
= MEMCACHED_SUCCESS
;
113 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
116 static memcached_return_t
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
117 const char *group_key
, size_t group_key_length
,
118 const char *key
, size_t key_length
,
119 uint64_t offset
, uint64_t initial
,
123 bool no_reply
= ptr
->flags
.no_reply
;
125 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
126 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
130 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
131 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
133 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
134 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
136 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
138 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
139 request
.message
.header
.request
.opcode
= cmd
;
140 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->_namespace
)));
141 request
.message
.header
.request
.extlen
= 20;
142 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
143 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->_namespace
) +request
.message
.header
.request
.extlen
));
144 request
.message
.body
.delta
= memcached_htonll(offset
);
145 request
.message
.body
.initial
= memcached_htonll(initial
);
146 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
148 struct libmemcached_io_vector_st vector
[]=
150 { request
.bytes
, sizeof(request
.bytes
) },
151 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
155 memcached_return_t rc
;
156 if (memcached_failed(rc
= memcached_vdo(instance
, vector
, 3, true)))
158 memcached_io_reset(instance
);
159 return (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
164 return MEMCACHED_SUCCESS
;
167 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
170 memcached_return_t
memcached_increment(memcached_st
*ptr
,
171 const char *key
, size_t key_length
,
175 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
178 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
179 const char *key
, size_t key_length
,
183 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
186 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
187 const char *group_key
, size_t group_key_length
,
188 const char *key
, size_t key_length
,
192 memcached_return_t rc
;
193 uint64_t local_value
;
199 if (memcached_failed(rc
= initialize_query(ptr
)))
204 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
209 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
210 if (ptr
->flags
.binary_protocol
)
212 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
213 group_key
, group_key_length
, key
, key_length
,
214 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
219 rc
= text_incr_decr(ptr
, "incr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
222 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
227 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
228 const char *group_key
, size_t group_key_length
,
229 const char *key
, size_t key_length
,
233 uint64_t local_value
;
239 memcached_return_t rc
;
240 if (memcached_failed(rc
= initialize_query(ptr
)))
245 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
251 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
252 if (ptr
->flags
.binary_protocol
)
254 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
255 group_key
, group_key_length
, key
, key_length
,
256 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
261 rc
= text_incr_decr(ptr
, "decr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
264 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
269 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
277 uint64_t local_value
;
283 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
285 offset
, initial
, expiration
, value
);
288 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
289 const char *group_key
,
290 size_t group_key_length
,
298 uint64_t local_value
;
304 memcached_return_t rc
;
305 if (memcached_failed(rc
= initialize_query(ptr
)))
310 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
315 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
316 if (ptr
->flags
.binary_protocol
)
318 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
319 group_key
, group_key_length
, key
, key_length
,
320 offset
, initial
, (uint32_t)expiration
,
325 rc
= MEMCACHED_PROTOCOL_ERROR
;
328 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
333 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
341 uint64_t local_value
;
347 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
349 offset
, initial
, expiration
, value
);
352 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
353 const char *group_key
,
354 size_t group_key_length
,
362 uint64_t local_value
;
368 memcached_return_t rc
;
369 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
374 if (memcached_failed(rc
= initialize_query(ptr
)))
380 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
381 if (ptr
->flags
.binary_protocol
)
383 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
384 group_key
, group_key_length
, key
, key_length
,
385 offset
, initial
, (uint32_t)expiration
,
390 rc
= MEMCACHED_PROTOCOL_ERROR
;
393 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();