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 void auto_response(org::libmemcached::Instance
* instance
, const bool reply
, memcached_return_t
& rc
, uint64_t* value
)
42 // If the message was successfully sent, then get the response, otherwise
44 if (memcached_success(rc
))
52 rc
= memcached_response(instance
, &instance
->root
->result
);
55 if (memcached_fatal(rc
))
57 assert(memcached_last_error(instance
->root
) != MEMCACHED_SUCCESS
);
62 *value
= instance
->root
->result
.numeric_value
;
66 static memcached_return_t
text_incr_decr(org::libmemcached::Instance
* instance
,
68 const char *key
, size_t key_length
,
69 const uint64_t offset
,
72 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
74 int send_length
= snprintf(buffer
, sizeof(buffer
), " %" PRIu64
, offset
);
75 if (size_t(send_length
) >= sizeof(buffer
) or send_length
< 0)
77 return memcached_set_error(*instance
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
78 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
81 libmemcached_io_vector_st vector
[]=
84 { memcached_literal_param("incr ") },
85 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
87 { buffer
, size_t(send_length
) },
88 { " noreply", reply
? 0 : memcached_literal_param_size(" noreply") },
89 { memcached_literal_param("\r\n") }
94 vector
[1].buffer
= "decr ";
97 return memcached_vdo(instance
, vector
, 7, true);
100 static memcached_return_t
binary_incr_decr(org::libmemcached::Instance
* instance
,
101 protocol_binary_command cmd
,
102 const char *key
, const size_t key_length
,
103 const uint64_t offset
,
104 const uint64_t initial
,
105 const uint32_t expiration
,
110 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
112 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
115 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
117 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
120 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
122 initialize_binary_request(instance
, request
.message
.header
);
124 request
.message
.header
.request
.opcode
= cmd
;
125 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(instance
->root
->_namespace
)));
126 request
.message
.header
.request
.extlen
= 20;
127 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
128 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(instance
->root
->_namespace
) +request
.message
.header
.request
.extlen
));
129 request
.message
.body
.delta
= memcached_htonll(offset
);
130 request
.message
.body
.initial
= memcached_htonll(initial
);
131 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
133 libmemcached_io_vector_st vector
[]=
136 { request
.bytes
, sizeof(request
.bytes
) },
137 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
141 return memcached_vdo(instance
, vector
, 4, true);
144 memcached_return_t
memcached_increment(memcached_st
*memc
,
145 const char *key
, size_t key_length
,
149 return memcached_increment_by_key(memc
, key
, key_length
, key
, key_length
, offset
, value
);
152 static memcached_return_t
increment_decrement_by_key(const protocol_binary_command command
,
154 const char *group_key
, size_t group_key_length
,
155 const char *key
, size_t key_length
,
159 uint64_t local_value
;
165 memcached_return_t rc
;
166 if (memcached_failed(rc
= initialize_query(memc
, true)))
171 if (memcached_is_encrypted(memc
))
173 return memcached_set_error(*memc
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
,
174 memcached_literal_param("Operation not allowed while encyrption is enabled"));
177 if (memcached_failed(rc
= memcached_key_test(*memc
, (const char **)&key
, &key_length
, 1)))
179 return memcached_last_error(memc
);
182 uint32_t server_key
= memcached_generate_hash_with_redistribution(memc
, group_key
, group_key_length
);
183 org::libmemcached::Instance
* instance
= memcached_instance_fetch(memc
, server_key
);
185 bool reply
= memcached_is_replying(instance
->root
);
187 if (memcached_is_binary(memc
))
189 rc
= binary_incr_decr(instance
, command
,
191 uint64_t(offset
), 0, MEMCACHED_EXPIRATION_NOT_ADD
,
196 rc
= text_incr_decr(instance
,
197 command
== PROTOCOL_BINARY_CMD_INCREMENT
? true : false,
202 auto_response(instance
, reply
, rc
, value
);
207 static memcached_return_t
increment_decrement_with_initial_by_key(const protocol_binary_command command
,
209 const char *group_key
,
210 size_t group_key_length
,
218 uint64_t local_value
;
224 memcached_return_t rc
;
225 if (memcached_failed(rc
= initialize_query(memc
, true)))
230 if (memcached_is_encrypted(memc
))
232 return memcached_set_error(*memc
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
,
233 memcached_literal_param("Operation not allowed while encyrption is enabled"));
236 if (memcached_failed(rc
= memcached_key_test(*memc
, (const char **)&key
, &key_length
, 1)))
238 return memcached_last_error(memc
);
241 uint32_t server_key
= memcached_generate_hash_with_redistribution(memc
, group_key
, group_key_length
);
242 org::libmemcached::Instance
* instance
= memcached_instance_fetch(memc
, server_key
);
244 bool reply
= memcached_is_replying(instance
->root
);
246 if (memcached_is_binary(memc
))
248 rc
= binary_incr_decr(instance
, command
,
250 offset
, initial
, uint32_t(expiration
),
256 rc
= memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
257 memcached_literal_param("memcached_increment_with_initial_by_key() is not supported via the ASCII protocol"));
260 auto_response(instance
, reply
, rc
, value
);
265 memcached_return_t
memcached_decrement(memcached_st
*memc
,
266 const char *key
, size_t key_length
,
270 return memcached_decrement_by_key(memc
, key
, key_length
, key
, key_length
, offset
, value
);
274 memcached_return_t
memcached_increment_by_key(memcached_st
*shell
,
275 const char *group_key
, size_t group_key_length
,
276 const char *key
, size_t key_length
,
280 Memcached
* memc
= memcached2Memcached(shell
);
281 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
282 memcached_return_t rc
= increment_decrement_by_key(PROTOCOL_BINARY_CMD_INCREMENT
,
284 group_key
, group_key_length
,
288 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
293 memcached_return_t
memcached_decrement_by_key(memcached_st
*shell
,
294 const char *group_key
, size_t group_key_length
,
295 const char *key
, size_t key_length
,
299 Memcached
* memc
= memcached2Memcached(shell
);
300 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
301 memcached_return_t rc
= increment_decrement_by_key(PROTOCOL_BINARY_CMD_DECREMENT
,
303 group_key
, group_key_length
,
306 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
311 memcached_return_t
memcached_increment_with_initial(memcached_st
*memc
,
319 return memcached_increment_with_initial_by_key(memc
, key
, key_length
,
321 offset
, initial
, expiration
, value
);
324 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*shell
,
325 const char *group_key
,
326 size_t group_key_length
,
334 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
335 Memcached
* memc
= memcached2Memcached(shell
);
336 memcached_return_t rc
= increment_decrement_with_initial_by_key(PROTOCOL_BINARY_CMD_INCREMENT
,
338 group_key
, group_key_length
,
340 offset
, initial
, expiration
, value
);
341 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
346 memcached_return_t
memcached_decrement_with_initial(memcached_st
*memc
,
354 return memcached_decrement_with_initial_by_key(memc
, key
, key_length
,
356 offset
, initial
, expiration
, value
);
359 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*shell
,
360 const char *group_key
,
361 size_t group_key_length
,
369 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
370 Memcached
* memc
= memcached2Memcached(shell
);
371 memcached_return_t rc
= increment_decrement_with_initial_by_key(PROTOCOL_BINARY_CMD_DECREMENT
,
373 group_key
, group_key_length
,
375 offset
, initial
, expiration
, value
);
377 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();