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
[]=
58 { memcached_literal_param("incr ") },
59 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
61 { buffer
, send_length
},
62 { " noreply", reply
? 0 : memcached_literal_param_size(" noreply") },
63 { memcached_literal_param("\r\n") }
68 vector
[0].buffer
= "decr ";
71 memcached_return_t rc
= memcached_vdo(instance
, vector
, 6, true);
75 return MEMCACHED_SUCCESS
;
78 if (memcached_failed(rc
))
80 numeric_value
= UINT64_MAX
;
84 rc
= memcached_response(instance
, buffer
, sizeof(buffer
), NULL
, numeric_value
);
86 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
89 static memcached_return_t
binary_incr_decr(memcached_server_write_instance_st instance
,
90 protocol_binary_command cmd
,
91 const char *key
, const size_t key_length
,
92 const uint64_t offset
,
93 const uint64_t initial
,
94 const uint32_t expiration
,
100 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
102 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
105 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
107 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
110 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
112 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
113 request
.message
.header
.request
.opcode
= cmd
;
114 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(instance
->root
->_namespace
)));
115 request
.message
.header
.request
.extlen
= 20;
116 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
117 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(instance
->root
->_namespace
) +request
.message
.header
.request
.extlen
));
118 request
.message
.body
.delta
= memcached_htonll(offset
);
119 request
.message
.body
.initial
= memcached_htonll(initial
);
120 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
122 libmemcached_io_vector_st vector
[]=
124 { request
.bytes
, sizeof(request
.bytes
) },
125 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
129 memcached_return_t rc
;
130 if (memcached_failed(rc
= memcached_vdo(instance
, vector
, 3, true)))
132 memcached_io_reset(instance
);
133 return MEMCACHED_WRITE_FAILURE
;
138 return MEMCACHED_SUCCESS
;
141 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
144 memcached_return_t
memcached_increment(memcached_st
*ptr
,
145 const char *key
, size_t key_length
,
149 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
152 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
153 const char *key
, size_t key_length
,
157 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
160 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
161 const char *group_key
, size_t group_key_length
,
162 const char *key
, size_t key_length
,
166 memcached_return_t rc
;
167 uint64_t local_value
;
173 if (memcached_failed(rc
= initialize_query(ptr
, true)))
178 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
183 if (memcached_failed(rc
= memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
185 return memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
188 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
189 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
191 bool reply
= memcached_is_replying(instance
->root
);
193 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
194 if (memcached_is_binary(ptr
))
196 rc
= binary_incr_decr(instance
, PROTOCOL_BINARY_CMD_INCREMENT
,
198 uint64_t(offset
), 0, MEMCACHED_EXPIRATION_NOT_ADD
,
204 rc
= text_incr_decr(instance
, true, key
, key_length
, offset
, reply
, *value
);
207 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
212 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
213 const char *group_key
, size_t group_key_length
,
214 const char *key
, size_t key_length
,
218 uint64_t local_value
;
224 memcached_return_t rc
;
225 if (memcached_failed(rc
= initialize_query(ptr
, true)))
230 if (memcached_failed(rc
= memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
232 return memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
236 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
237 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
239 bool reply
= memcached_is_replying(instance
->root
);
241 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
242 if (memcached_is_binary(ptr
))
244 rc
= binary_incr_decr(instance
, PROTOCOL_BINARY_CMD_DECREMENT
,
246 offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
252 rc
= text_incr_decr(instance
, false, key
, key_length
, offset
, reply
, *value
);
255 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
260 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
268 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
270 offset
, initial
, expiration
, value
);
273 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
274 const char *group_key
,
275 size_t group_key_length
,
283 uint64_t local_value
;
289 memcached_return_t rc
;
290 if (memcached_failed(rc
= initialize_query(ptr
, true)))
295 if (memcached_failed(rc
= memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
297 return memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
300 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
301 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
303 bool reply
= memcached_is_replying(instance
->root
);
305 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
306 if (memcached_is_binary(ptr
))
308 rc
= binary_incr_decr(instance
, PROTOCOL_BINARY_CMD_INCREMENT
,
310 offset
, initial
, uint32_t(expiration
),
316 rc
= MEMCACHED_PROTOCOL_ERROR
;
319 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
324 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
332 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
334 offset
, initial
, expiration
, value
);
337 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
338 const char *group_key
,
339 size_t group_key_length
,
347 uint64_t local_value
;
353 memcached_return_t rc
;
354 if (memcached_failed(rc
= initialize_query(ptr
, true)))
359 if (memcached_failed(rc
= memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
361 return memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
364 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
365 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
367 bool reply
= memcached_is_replying(instance
->root
);
370 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
371 if (memcached_is_binary(ptr
))
373 rc
= binary_incr_decr(instance
, PROTOCOL_BINARY_CMD_DECREMENT
,
375 offset
, initial
, uint32_t(expiration
),
381 rc
= MEMCACHED_PROTOCOL_ERROR
;
384 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();