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(memcached_server_write_instance_st 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_success(rc
))
57 *value
= instance
->root
->result
.numeric_value
;
65 static memcached_return_t
text_incr_decr(memcached_server_write_instance_st instance
,
67 const char *key
, size_t key_length
,
68 const uint64_t offset
,
71 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
73 int send_length
= snprintf(buffer
, sizeof(buffer
), " %" PRIu64
, offset
);
74 if (size_t(send_length
) >= sizeof(buffer
) or send_length
< 0)
76 return memcached_set_error(*instance
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
77 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
80 libmemcached_io_vector_st vector
[]=
83 { memcached_literal_param("incr ") },
84 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
86 { buffer
, send_length
},
87 { " noreply", reply
? 0 : memcached_literal_param_size(" noreply") },
88 { memcached_literal_param("\r\n") }
93 vector
[1].buffer
= "decr ";
96 return memcached_vdo(instance
, vector
, 7, true);
99 static memcached_return_t
binary_incr_decr(memcached_server_write_instance_st instance
,
100 protocol_binary_command cmd
,
101 const char *key
, const size_t key_length
,
102 const uint64_t offset
,
103 const uint64_t initial
,
104 const uint32_t expiration
,
109 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
111 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
114 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
116 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
119 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
121 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
122 request
.message
.header
.request
.opcode
= cmd
;
123 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(instance
->root
->_namespace
)));
124 request
.message
.header
.request
.extlen
= 20;
125 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
126 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(instance
->root
->_namespace
) +request
.message
.header
.request
.extlen
));
127 request
.message
.body
.delta
= memcached_htonll(offset
);
128 request
.message
.body
.initial
= memcached_htonll(initial
);
129 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
131 libmemcached_io_vector_st vector
[]=
134 { request
.bytes
, sizeof(request
.bytes
) },
135 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
139 return memcached_vdo(instance
, vector
, 4, true);
142 memcached_return_t
memcached_increment(memcached_st
*memc
,
143 const char *key
, size_t key_length
,
147 return memcached_increment_by_key(memc
, key
, key_length
, key
, key_length
, offset
, value
);
150 static memcached_return_t
increment_decrement_by_key(const protocol_binary_command command
,
152 const char *group_key
, size_t group_key_length
,
153 const char *key
, size_t key_length
,
157 uint64_t local_value
;
163 memcached_return_t rc
;
164 if (memcached_failed(rc
= initialize_query(memc
, true)))
169 if (memcached_is_encrypted(memc
))
171 return memcached_set_error(*memc
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
,
172 memcached_literal_param("Operation not allowed while encyrption is enabled"));
175 if (memcached_failed(rc
= memcached_key_test(*memc
, (const char **)&key
, &key_length
, 1)))
177 return memcached_last_error(memc
);
180 uint32_t server_key
= memcached_generate_hash_with_redistribution(memc
, group_key
, group_key_length
);
181 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(memc
, server_key
);
183 bool reply
= memcached_is_replying(instance
->root
);
185 if (memcached_is_binary(memc
))
187 rc
= binary_incr_decr(instance
, command
,
189 uint64_t(offset
), 0, MEMCACHED_EXPIRATION_NOT_ADD
,
194 rc
= text_incr_decr(instance
,
195 command
== PROTOCOL_BINARY_CMD_INCREMENT
? true : false,
200 auto_response(instance
, reply
, rc
, value
);
205 static memcached_return_t
increment_decrement_with_initial_by_key(const protocol_binary_command command
,
207 const char *group_key
,
208 size_t group_key_length
,
216 uint64_t local_value
;
222 memcached_return_t rc
;
223 if (memcached_failed(rc
= initialize_query(memc
, true)))
228 if (memcached_is_encrypted(memc
))
230 return memcached_set_error(*memc
, MEMCACHED_NOT_SUPPORTED
, MEMCACHED_AT
,
231 memcached_literal_param("Operation not allowed while encyrption is enabled"));
234 if (memcached_failed(rc
= memcached_key_test(*memc
, (const char **)&key
, &key_length
, 1)))
236 return memcached_last_error(memc
);
239 uint32_t server_key
= memcached_generate_hash_with_redistribution(memc
, group_key
, group_key_length
);
240 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(memc
, server_key
);
242 bool reply
= memcached_is_replying(instance
->root
);
244 if (memcached_is_binary(memc
))
246 rc
= binary_incr_decr(instance
, command
,
248 offset
, initial
, uint32_t(expiration
),
254 rc
= memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
255 memcached_literal_param("memcached_increment_with_initial_by_key() is not supported via the ASCII protocol"));
258 auto_response(instance
, reply
, rc
, value
);
263 memcached_return_t
memcached_decrement(memcached_st
*memc
,
264 const char *key
, size_t key_length
,
268 return memcached_decrement_by_key(memc
, key
, key_length
, key
, key_length
, offset
, value
);
272 memcached_return_t
memcached_increment_by_key(memcached_st
*memc
,
273 const char *group_key
, size_t group_key_length
,
274 const char *key
, size_t key_length
,
278 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
279 memcached_return_t rc
= increment_decrement_by_key(PROTOCOL_BINARY_CMD_INCREMENT
,
281 group_key
, group_key_length
,
285 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
290 memcached_return_t
memcached_decrement_by_key(memcached_st
*memc
,
291 const char *group_key
, size_t group_key_length
,
292 const char *key
, size_t key_length
,
296 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
297 memcached_return_t rc
= increment_decrement_by_key(PROTOCOL_BINARY_CMD_DECREMENT
,
299 group_key
, group_key_length
,
302 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
307 memcached_return_t
memcached_increment_with_initial(memcached_st
*memc
,
315 return memcached_increment_with_initial_by_key(memc
, key
, key_length
,
317 offset
, initial
, expiration
, value
);
320 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*memc
,
321 const char *group_key
,
322 size_t group_key_length
,
330 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
331 memcached_return_t rc
= increment_decrement_with_initial_by_key(PROTOCOL_BINARY_CMD_INCREMENT
,
333 group_key
, group_key_length
,
335 offset
, initial
, expiration
, value
);
336 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
341 memcached_return_t
memcached_decrement_with_initial(memcached_st
*memc
,
349 return memcached_decrement_with_initial_by_key(memc
, key
, key_length
,
351 offset
, initial
, expiration
, value
);
354 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*memc
,
355 const char *group_key
,
356 size_t group_key_length
,
364 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
365 memcached_return_t rc
= increment_decrement_with_initial_by_key(PROTOCOL_BINARY_CMD_DECREMENT
,
367 group_key
, group_key_length
,
369 offset
, initial
, expiration
, value
);
371 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();