Update dump code.
[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 bool is_incr,
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
51 // Invert the logic to make it simpler to read the code
52 bool reply= (ptr->flags.no_reply) ? false : true;
53
54 if (memcached_failed(memcached_key_test(*ptr, (const char **)&key, &key_length, 1)))
55 {
56 return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT);
57 }
58
59 server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
60 instance= memcached_server_instance_fetch(ptr, server_key);
61
62 int send_length= snprintf(buffer, sizeof(buffer), " %" PRIu64, offset);
63 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
64 {
65 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
66 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
67 }
68
69 struct libmemcached_io_vector_st vector[]=
70 {
71 { memcached_literal_param("incr ") },
72 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
73 { key, key_length },
74 { buffer, send_length },
75 { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") },
76 { memcached_literal_param("\r\n") }
77 };
78
79 if (is_incr == false)
80 {
81 vector[0].buffer= "decr ";
82 }
83
84 memcached_return_t rc= memcached_vdo(instance, vector, 6, true);
85 if (reply == false or memcached_failed(rc))
86 {
87 return rc;
88 }
89
90 rc= memcached_response(instance, buffer, sizeof(buffer), NULL);
91
92 if (rc != MEMCACHED_SUCCESS)
93 {
94 return memcached_set_error(*instance, rc, MEMCACHED_AT);
95 }
96
97 /*
98 So why recheck responce? Because the protocol is brain dead :)
99 The number returned might end up equaling one of the string
100 values. Less chance of a mistake with strncmp() so we will
101 use it. We still called memcached_response() though since it
102 worked its magic for non-blocking IO.
103 */
104 if (not strncmp(buffer, memcached_literal_param("ERROR\r\n")))
105 {
106 *value= 0;
107 rc= MEMCACHED_PROTOCOL_ERROR;
108 }
109 else if (not strncmp(buffer, memcached_literal_param("CLIENT_ERROR\r\n")))
110 {
111 *value= 0;
112 rc= MEMCACHED_PROTOCOL_ERROR;
113 }
114 else if (not strncmp(buffer, memcached_literal_param("NOT_FOUND\r\n")))
115 {
116 *value= 0;
117 rc= MEMCACHED_NOTFOUND;
118 }
119 else
120 {
121 *value= strtoull(buffer, (char **)NULL, 10);
122 rc= MEMCACHED_SUCCESS;
123 }
124
125 return memcached_set_error(*instance, rc, MEMCACHED_AT);
126 }
127
128 static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd,
129 const char *group_key, size_t group_key_length,
130 const char *key, size_t key_length,
131 uint64_t offset, uint64_t initial,
132 uint32_t expiration,
133 uint64_t *value)
134 {
135 bool no_reply= ptr->flags.no_reply;
136
137 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
138 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
139
140 if (no_reply)
141 {
142 if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
143 cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
144
145 if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
146 cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
147 }
148 protocol_binary_request_incr request= {}; // = {.bytes= {0}};
149
150 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
151 request.message.header.request.opcode= cmd;
152 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
153 request.message.header.request.extlen= 20;
154 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
155 request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(ptr->_namespace) +request.message.header.request.extlen));
156 request.message.body.delta= memcached_htonll(offset);
157 request.message.body.initial= memcached_htonll(initial);
158 request.message.body.expiration= htonl((uint32_t) expiration);
159
160 struct libmemcached_io_vector_st vector[]=
161 {
162 { request.bytes, sizeof(request.bytes) },
163 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
164 { key, key_length }
165 };
166
167 memcached_return_t rc;
168 if (memcached_failed(rc= memcached_vdo(instance, vector, 3, true)))
169 {
170 memcached_io_reset(instance);
171 return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
172 }
173
174 if (no_reply)
175 {
176 return MEMCACHED_SUCCESS;
177 }
178
179 return memcached_response(instance, (char*)value, sizeof(*value), NULL);
180 }
181
182 memcached_return_t memcached_increment(memcached_st *ptr,
183 const char *key, size_t key_length,
184 uint32_t offset,
185 uint64_t *value)
186 {
187 return memcached_increment_by_key(ptr, key, key_length, key, key_length, offset, value);
188 }
189
190 memcached_return_t memcached_decrement(memcached_st *ptr,
191 const char *key, size_t key_length,
192 uint32_t offset,
193 uint64_t *value)
194 {
195 return memcached_decrement_by_key(ptr, key, key_length, key, key_length, offset, value);
196 }
197
198 memcached_return_t memcached_increment_by_key(memcached_st *ptr,
199 const char *group_key, size_t group_key_length,
200 const char *key, size_t key_length,
201 uint64_t offset,
202 uint64_t *value)
203 {
204 memcached_return_t rc;
205 uint64_t local_value;
206 if (value == NULL)
207 {
208 value= &local_value;
209 }
210
211 if (memcached_failed(rc= initialize_query(ptr)))
212 {
213 return rc;
214 }
215
216 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
217 {
218 return rc;
219 }
220
221 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
222 if (ptr->flags.binary_protocol)
223 {
224 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
225 group_key, group_key_length, key, key_length,
226 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
227 value);
228 }
229 else
230 {
231 rc= text_incr_decr(ptr, true, group_key, group_key_length, key, key_length, offset, value);
232 }
233
234 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
235
236 return rc;
237 }
238
239 memcached_return_t memcached_decrement_by_key(memcached_st *ptr,
240 const char *group_key, size_t group_key_length,
241 const char *key, size_t key_length,
242 uint64_t offset,
243 uint64_t *value)
244 {
245 uint64_t local_value;
246 if (value == NULL)
247 {
248 value= &local_value;
249 }
250
251 memcached_return_t rc;
252 if (memcached_failed(rc= initialize_query(ptr)))
253 {
254 return rc;
255 }
256
257 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
258 {
259 return rc;
260 }
261
262
263 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
264 if (ptr->flags.binary_protocol)
265 {
266 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
267 group_key, group_key_length, key, key_length,
268 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
269 value);
270 }
271 else
272 {
273 rc= text_incr_decr(ptr, false, group_key, group_key_length, key, key_length, offset, value);
274 }
275
276 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
277
278 return rc;
279 }
280
281 memcached_return_t memcached_increment_with_initial(memcached_st *ptr,
282 const char *key,
283 size_t key_length,
284 uint64_t offset,
285 uint64_t initial,
286 time_t expiration,
287 uint64_t *value)
288 {
289 uint64_t local_value;
290 if (value == NULL)
291 {
292 value= &local_value;
293 }
294
295 return memcached_increment_with_initial_by_key(ptr, key, key_length,
296 key, key_length,
297 offset, initial, expiration, value);
298 }
299
300 memcached_return_t memcached_increment_with_initial_by_key(memcached_st *ptr,
301 const char *group_key,
302 size_t group_key_length,
303 const char *key,
304 size_t key_length,
305 uint64_t offset,
306 uint64_t initial,
307 time_t expiration,
308 uint64_t *value)
309 {
310 uint64_t local_value;
311 if (value == NULL)
312 {
313 value= &local_value;
314 }
315
316 memcached_return_t rc;
317 if (memcached_failed(rc= initialize_query(ptr)))
318 {
319 return rc;
320 }
321
322 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
323 {
324 return rc;
325 }
326
327 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
328 if (ptr->flags.binary_protocol)
329 {
330 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
331 group_key, group_key_length, key, key_length,
332 offset, initial, (uint32_t)expiration,
333 value);
334 }
335 else
336 {
337 rc= MEMCACHED_PROTOCOL_ERROR;
338 }
339
340 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
341
342 return rc;
343 }
344
345 memcached_return_t memcached_decrement_with_initial(memcached_st *ptr,
346 const char *key,
347 size_t key_length,
348 uint64_t offset,
349 uint64_t initial,
350 time_t expiration,
351 uint64_t *value)
352 {
353 uint64_t local_value;
354 if (value == NULL)
355 {
356 value= &local_value;
357 }
358
359 return memcached_decrement_with_initial_by_key(ptr, key, key_length,
360 key, key_length,
361 offset, initial, expiration, value);
362 }
363
364 memcached_return_t memcached_decrement_with_initial_by_key(memcached_st *ptr,
365 const char *group_key,
366 size_t group_key_length,
367 const char *key,
368 size_t key_length,
369 uint64_t offset,
370 uint64_t initial,
371 time_t expiration,
372 uint64_t *value)
373 {
374 uint64_t local_value;
375 if (value == NULL)
376 {
377 value= &local_value;
378 }
379
380 memcached_return_t rc;
381 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
382 {
383 return rc;
384 }
385
386 if (memcached_failed(rc= initialize_query(ptr)))
387 {
388 return rc;
389 }
390
391
392 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
393 if (ptr->flags.binary_protocol)
394 {
395 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
396 group_key, group_key_length, key, key_length,
397 offset, initial, (uint32_t)expiration,
398 value);
399 }
400 else
401 {
402 rc= MEMCACHED_PROTOCOL_ERROR;
403 }
404
405 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
406
407 return rc;
408 }
409