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_server_write_instance_st instance
,
42 const char *key
, size_t key_length
,
43 const uint64_t offset
,
45 uint64_t& numeric_value
)
47 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
49 int send_length
= snprintf(buffer
, sizeof(buffer
), " %" PRIu64
, offset
);
50 if (size_t(send_length
) >= sizeof(buffer
) or send_length
< 0)
52 return memcached_set_error(*instance
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
53 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
56 libmemcached_io_vector_st vector
[]=
59 { memcached_literal_param("incr ") },
60 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
62 { buffer
, send_length
},
63 { " noreply", reply
? 0 : memcached_literal_param_size(" noreply") },
64 { memcached_literal_param("\r\n") }
69 vector
[1].buffer
= "decr ";
72 memcached_return_t rc
= memcached_vdo(instance
, vector
, 7, true);
76 return MEMCACHED_SUCCESS
;
79 if (memcached_failed(rc
))
81 numeric_value
= UINT64_MAX
;
85 rc
= memcached_response(instance
, buffer
, sizeof(buffer
), NULL
, numeric_value
);
87 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
90 static memcached_return_t
binary_incr_decr(memcached_server_write_instance_st instance
,
91 protocol_binary_command cmd
,
92 const char *key
, const size_t key_length
,
93 const uint64_t offset
,
94 const uint64_t initial
,
95 const uint32_t expiration
,
101 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
103 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
106 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
108 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
111 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
113 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
114 request
.message
.header
.request
.opcode
= cmd
;
115 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(instance
->root
->_namespace
)));
116 request
.message
.header
.request
.extlen
= 20;
117 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
118 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(instance
->root
->_namespace
) +request
.message
.header
.request
.extlen
));
119 request
.message
.body
.delta
= memcached_htonll(offset
);
120 request
.message
.body
.initial
= memcached_htonll(initial
);
121 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
123 libmemcached_io_vector_st vector
[]=
126 { request
.bytes
, sizeof(request
.bytes
) },
127 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
131 memcached_return_t rc
;
132 if (memcached_failed(rc
= memcached_vdo(instance
, vector
, 4, true)))
134 memcached_io_reset(instance
);
135 return MEMCACHED_WRITE_FAILURE
;
140 return MEMCACHED_SUCCESS
;
143 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
146 memcached_return_t
memcached_increment(memcached_st
*ptr
,
147 const char *key
, size_t key_length
,
151 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
154 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
155 const char *key
, size_t key_length
,
159 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
162 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
163 const char *group_key
, size_t group_key_length
,
164 const char *key
, size_t key_length
,
168 memcached_return_t rc
;
169 uint64_t local_value
;
175 if (memcached_failed(rc
= initialize_query(ptr
, true)))
180 if (memcached_failed(rc
= memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
182 return memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
185 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
186 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
188 bool reply
= memcached_is_replying(instance
->root
);
190 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
191 if (memcached_is_binary(ptr
))
193 rc
= binary_incr_decr(instance
, PROTOCOL_BINARY_CMD_INCREMENT
,
195 uint64_t(offset
), 0, MEMCACHED_EXPIRATION_NOT_ADD
,
201 rc
= text_incr_decr(instance
, true, key
, key_length
, offset
, reply
, *value
);
204 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
209 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
210 const char *group_key
, size_t group_key_length
,
211 const char *key
, size_t key_length
,
215 uint64_t local_value
;
221 memcached_return_t rc
;
222 if (memcached_failed(rc
= initialize_query(ptr
, true)))
227 if (memcached_failed(rc
= memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
229 return memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
233 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
234 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
236 bool reply
= memcached_is_replying(instance
->root
);
238 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
239 if (memcached_is_binary(ptr
))
241 rc
= binary_incr_decr(instance
, PROTOCOL_BINARY_CMD_DECREMENT
,
243 offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
249 rc
= text_incr_decr(instance
, false, key
, key_length
, offset
, reply
, *value
);
252 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
257 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
265 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
267 offset
, initial
, expiration
, value
);
270 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
271 const char *group_key
,
272 size_t group_key_length
,
280 uint64_t local_value
;
286 memcached_return_t rc
;
287 if (memcached_failed(rc
= initialize_query(ptr
, true)))
292 if (memcached_failed(rc
= memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
294 return memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
297 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
298 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
300 bool reply
= memcached_is_replying(instance
->root
);
302 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
303 if (memcached_is_binary(ptr
))
305 rc
= binary_incr_decr(instance
, PROTOCOL_BINARY_CMD_INCREMENT
,
307 offset
, initial
, uint32_t(expiration
),
313 rc
= memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
314 memcached_literal_param("memcached_increment_with_initial_by_key() is not supported via the ASCII protocol"));
317 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
322 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
330 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
332 offset
, initial
, expiration
, value
);
335 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
336 const char *group_key
,
337 size_t group_key_length
,
345 uint64_t local_value
;
351 memcached_return_t rc
;
352 if (memcached_failed(rc
= initialize_query(ptr
, true)))
357 if (memcached_failed(rc
= memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
359 return memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
362 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
363 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
365 bool reply
= memcached_is_replying(instance
->root
);
368 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
369 if (memcached_is_binary(ptr
))
371 rc
= binary_incr_decr(instance
, PROTOCOL_BINARY_CMD_DECREMENT
,
373 offset
, initial
, uint32_t(expiration
),
379 rc
= memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
380 memcached_literal_param("memcached_decrement_with_initial_by_key() is not supported via the ASCII protocol"));
383 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();