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
,
45 uint64_t& numeric_value
)
47 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
49 memcached_server_write_instance_st instance
;
51 // Invert the logic to make it simpler to read the code
52 bool reply
= (ptr
->flags
.no_reply
) ? false : true;
54 if (memcached_failed(memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1)))
56 return memcached_set_error(*ptr
, MEMCACHED_BAD_KEY_PROVIDED
, MEMCACHED_AT
);
59 server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
60 instance
= memcached_server_instance_fetch(ptr
, server_key
);
62 int send_length
= snprintf(buffer
, sizeof(buffer
), " %" PRIu64
, offset
);
63 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
65 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
66 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
69 struct libmemcached_io_vector_st vector
[]=
71 { memcached_literal_param("incr ") },
72 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
74 { buffer
, send_length
},
75 { " noreply", reply
? 0 : memcached_literal_param_size(" noreply") },
76 { memcached_literal_param("\r\n") }
81 vector
[0].buffer
= "decr ";
84 memcached_return_t rc
= memcached_vdo(instance
, vector
, 6, true);
85 if (reply
== false or memcached_failed(rc
))
87 numeric_value
= UINT64_MAX
;
91 rc
= memcached_response(instance
, buffer
, sizeof(buffer
), NULL
, numeric_value
);
93 return memcached_set_error(*instance
, rc
, MEMCACHED_AT
);
96 static memcached_return_t
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
97 const char *group_key
, size_t group_key_length
,
98 const char *key
, size_t key_length
,
99 uint64_t offset
, uint64_t initial
,
103 bool no_reply
= ptr
->flags
.no_reply
;
105 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
106 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
110 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
111 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
113 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
114 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
116 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
118 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
119 request
.message
.header
.request
.opcode
= cmd
;
120 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->_namespace
)));
121 request
.message
.header
.request
.extlen
= 20;
122 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
123 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->_namespace
) +request
.message
.header
.request
.extlen
));
124 request
.message
.body
.delta
= memcached_htonll(offset
);
125 request
.message
.body
.initial
= memcached_htonll(initial
);
126 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
128 struct libmemcached_io_vector_st vector
[]=
130 { request
.bytes
, sizeof(request
.bytes
) },
131 { memcached_array_string(ptr
->_namespace
), memcached_array_size(ptr
->_namespace
) },
135 memcached_return_t rc
;
136 if (memcached_failed(rc
= memcached_vdo(instance
, vector
, 3, true)))
138 memcached_io_reset(instance
);
139 return (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
144 return MEMCACHED_SUCCESS
;
147 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
150 memcached_return_t
memcached_increment(memcached_st
*ptr
,
151 const char *key
, size_t key_length
,
155 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
158 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
159 const char *key
, size_t key_length
,
163 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
166 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
167 const char *group_key
, size_t group_key_length
,
168 const char *key
, size_t key_length
,
172 memcached_return_t rc
;
173 uint64_t local_value
;
179 if (memcached_failed(rc
= initialize_query(ptr
)))
184 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
189 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
190 if (ptr
->flags
.binary_protocol
)
192 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
193 group_key
, group_key_length
, key
, key_length
,
194 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
199 rc
= text_incr_decr(ptr
, true, group_key
, group_key_length
, key
, key_length
, offset
, *value
);
202 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
207 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
208 const char *group_key
, size_t group_key_length
,
209 const char *key
, size_t key_length
,
213 uint64_t local_value
;
219 memcached_return_t rc
;
220 if (memcached_failed(rc
= initialize_query(ptr
)))
225 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
231 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
232 if (ptr
->flags
.binary_protocol
)
234 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
235 group_key
, group_key_length
, key
, key_length
,
236 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
241 rc
= text_incr_decr(ptr
, false, group_key
, group_key_length
, key
, key_length
, offset
, *value
);
244 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
249 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
257 uint64_t local_value
;
263 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
265 offset
, initial
, expiration
, value
);
268 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
269 const char *group_key
,
270 size_t group_key_length
,
278 uint64_t local_value
;
284 memcached_return_t rc
;
285 if (memcached_failed(rc
= initialize_query(ptr
)))
290 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
295 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
296 if (ptr
->flags
.binary_protocol
)
298 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
299 group_key
, group_key_length
, key
, key_length
,
300 offset
, initial
, (uint32_t)expiration
,
305 rc
= MEMCACHED_PROTOCOL_ERROR
;
308 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
313 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
321 uint64_t local_value
;
327 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
329 offset
, initial
, expiration
, value
);
332 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
333 const char *group_key
,
334 size_t group_key_length
,
342 uint64_t local_value
;
348 memcached_return_t rc
;
349 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
354 if (memcached_failed(rc
= initialize_query(ptr
)))
360 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
361 if (ptr
->flags
.binary_protocol
)
363 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
364 group_key
, group_key_length
, key
, key_length
,
365 offset
, initial
, (uint32_t)expiration
,
370 rc
= MEMCACHED_PROTOCOL_ERROR
;
373 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();