Merge in all of trunk.
[m6w6/libmemcached] / libmemcached / auto.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
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.
35 *
36 */
37
38 #include <libmemcached/common.h>
39
40 static memcached_return_t text_incr_decr(memcached_st *ptr,
41 const char *verb,
42 const char *group_key, size_t group_key_length,
43 const char *key, size_t key_length,
44 uint64_t offset,
45 uint64_t *value)
46 {
47 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
48 uint32_t server_key;
49 memcached_server_write_instance_st instance;
50 bool no_reply= ptr->flags.no_reply;
51
52 if (memcached_failed(memcached_key_test(*ptr, (const char **)&key, &key_length, 1)))
53 {
54 return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT);
55 }
56
57 server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
58 instance= memcached_server_instance_fetch(ptr, server_key);
59
60 int send_length;
61 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
62 "%s %.*s%.*s %" PRIu64 "%s\r\n", verb,
63 memcached_print_array(ptr->_namespace),
64 (int)key_length, key,
65 offset, no_reply ? " noreply" : "");
66 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
67 {
68 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
69 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
70 }
71
72 memcached_return_t rc= memcached_do(instance, buffer, (size_t)send_length, true);
73 if (no_reply or memcached_failed(rc))
74 return rc;
75
76 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
77
78 if (rc != MEMCACHED_SUCCESS)
79 {
80 return memcached_set_error(*instance, rc, MEMCACHED_AT);
81 }
82
83 /*
84 So why recheck responce? Because the protocol is brain dead :)
85 The number returned might end up equaling one of the string
86 values. Less chance of a mistake with strncmp() so we will
87 use it. We still called memcached_response() though since it
88 worked its magic for non-blocking IO.
89 */
90 if (not strncmp(buffer, memcached_literal_param("ERROR\r\n")))
91 {
92 *value= 0;
93 rc= MEMCACHED_PROTOCOL_ERROR;
94 }
95 else if (not strncmp(buffer, memcached_literal_param("CLIENT_ERROR\r\n")))
96 {
97 *value= 0;
98 rc= MEMCACHED_PROTOCOL_ERROR;
99 }
100 else if (not strncmp(buffer, memcached_literal_param("NOT_FOUND\r\n")))
101 {
102 *value= 0;
103 rc= MEMCACHED_NOTFOUND;
104 }
105 else
106 {
107 *value= strtoull(buffer, (char **)NULL, 10);
108 rc= MEMCACHED_SUCCESS;
109 }
110
111 return memcached_set_error(*instance, rc, MEMCACHED_AT);
112 }
113
114 static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd,
115 const char *group_key, size_t group_key_length,
116 const char *key, size_t key_length,
117 uint64_t offset, uint64_t initial,
118 uint32_t expiration,
119 uint64_t *value)
120 {
121 bool no_reply= ptr->flags.no_reply;
122
123 if (memcached_server_count(ptr) == 0)
124 return memcached_set_error(*ptr, MEMCACHED_NO_SERVERS, MEMCACHED_AT);
125
126 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
127 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
128
129 if (no_reply)
130 {
131 if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
132 cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
133
134 if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
135 cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
136 }
137 protocol_binary_request_incr request= {}; // = {.bytes= {0}};
138
139 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
140 request.message.header.request.opcode= cmd;
141 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
142 request.message.header.request.extlen= 20;
143 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
144 request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(ptr->_namespace) +request.message.header.request.extlen));
145 request.message.body.delta= memcached_htonll(offset);
146 request.message.body.initial= memcached_htonll(initial);
147 request.message.body.expiration= htonl((uint32_t) expiration);
148
149 struct libmemcached_io_vector_st vector[]=
150 {
151 { sizeof(request.bytes), request.bytes },
152 { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
153 { key_length, key }
154 };
155
156 memcached_return_t rc;
157 if (memcached_failed(rc= memcached_vdo(instance, vector, 3, true)))
158 {
159 memcached_io_reset(instance);
160 return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
161 }
162
163 if (no_reply)
164 return MEMCACHED_SUCCESS;
165
166 return memcached_response(instance, (char*)value, sizeof(*value), NULL);
167 }
168
169 memcached_return_t memcached_increment(memcached_st *ptr,
170 const char *key, size_t key_length,
171 uint32_t offset,
172 uint64_t *value)
173 {
174 return memcached_increment_by_key(ptr, key, key_length, key, key_length, offset, value);
175 }
176
177 memcached_return_t memcached_decrement(memcached_st *ptr,
178 const char *key, size_t key_length,
179 uint32_t offset,
180 uint64_t *value)
181 {
182 return memcached_decrement_by_key(ptr, key, key_length, key, key_length, offset, value);
183 }
184
185 memcached_return_t memcached_increment_by_key(memcached_st *ptr,
186 const char *group_key, size_t group_key_length,
187 const char *key, size_t key_length,
188 uint64_t offset,
189 uint64_t *value)
190 {
191 memcached_return_t rc;
192 uint64_t local_value;
193 if (not value)
194 value= &local_value;
195
196 if (memcached_failed(rc= initialize_query(ptr)))
197 {
198 return rc;
199 }
200
201 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
202 {
203 return rc;
204 }
205
206 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
207 if (ptr->flags.binary_protocol)
208 {
209 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
210 group_key, group_key_length, key, key_length,
211 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
212 value);
213 }
214 else
215 {
216 rc= text_incr_decr(ptr, "incr", group_key, group_key_length, key, key_length, offset, value);
217 }
218
219 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
220
221 return rc;
222 }
223
224 memcached_return_t memcached_decrement_by_key(memcached_st *ptr,
225 const char *group_key, size_t group_key_length,
226 const char *key, size_t key_length,
227 uint64_t offset,
228 uint64_t *value)
229 {
230 uint64_t local_value;
231 if (not value)
232 value= &local_value;
233
234 memcached_return_t rc;
235 if (memcached_failed(rc= initialize_query(ptr)))
236 {
237 return rc;
238 }
239
240 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
241 {
242 return rc;
243 }
244
245
246 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
247 if (ptr->flags.binary_protocol)
248 {
249 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
250 group_key, group_key_length, key, key_length,
251 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
252 value);
253 }
254 else
255 {
256 rc= text_incr_decr(ptr, "decr", group_key, group_key_length, key, key_length, offset, value);
257 }
258
259 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
260
261 return rc;
262 }
263
264 memcached_return_t memcached_increment_with_initial(memcached_st *ptr,
265 const char *key,
266 size_t key_length,
267 uint64_t offset,
268 uint64_t initial,
269 time_t expiration,
270 uint64_t *value)
271 {
272 uint64_t local_value;
273 if (! value)
274 value= &local_value;
275
276 return memcached_increment_with_initial_by_key(ptr, key, key_length,
277 key, key_length,
278 offset, initial, expiration, value);
279 }
280
281 memcached_return_t memcached_increment_with_initial_by_key(memcached_st *ptr,
282 const char *group_key,
283 size_t group_key_length,
284 const char *key,
285 size_t key_length,
286 uint64_t offset,
287 uint64_t initial,
288 time_t expiration,
289 uint64_t *value)
290 {
291 uint64_t local_value;
292 if (not value)
293 value= &local_value;
294
295 memcached_return_t rc;
296 if (memcached_failed(rc= initialize_query(ptr)))
297 {
298 return rc;
299 }
300
301 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
302 {
303 return rc;
304 }
305
306 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
307 if (ptr->flags.binary_protocol)
308 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
309 group_key, group_key_length, key, key_length,
310 offset, initial, (uint32_t)expiration,
311 value);
312 else
313 rc= MEMCACHED_PROTOCOL_ERROR;
314
315 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
316
317 return rc;
318 }
319
320 memcached_return_t memcached_decrement_with_initial(memcached_st *ptr,
321 const char *key,
322 size_t key_length,
323 uint64_t offset,
324 uint64_t initial,
325 time_t expiration,
326 uint64_t *value)
327 {
328 uint64_t local_value;
329 if (! value)
330 value= &local_value;
331
332 return memcached_decrement_with_initial_by_key(ptr, key, key_length,
333 key, key_length,
334 offset, initial, expiration, value);
335 }
336
337 memcached_return_t memcached_decrement_with_initial_by_key(memcached_st *ptr,
338 const char *group_key,
339 size_t group_key_length,
340 const char *key,
341 size_t key_length,
342 uint64_t offset,
343 uint64_t initial,
344 time_t expiration,
345 uint64_t *value)
346 {
347 uint64_t local_value;
348 if (not value)
349 value= &local_value;
350
351 memcached_return_t rc;
352 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
353 {
354 return rc;
355 }
356
357 if (memcached_failed(rc= initialize_query(ptr)))
358 {
359 return rc;
360 }
361
362
363 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
364 if (ptr->flags.binary_protocol)
365 {
366 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
367 group_key, group_key_length, key, key_length,
368 offset, initial, (uint32_t)expiration,
369 value);
370 }
371 else
372 {
373 rc= MEMCACHED_PROTOCOL_ERROR;
374 }
375
376 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
377
378 return rc;
379 }
380