Updating for 1.0.2 release
[awesomized/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 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
124 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
125
126 if (no_reply)
127 {
128 if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
129 cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
130
131 if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
132 cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
133 }
134 protocol_binary_request_incr request= {}; // = {.bytes= {0}};
135
136 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
137 request.message.header.request.opcode= cmd;
138 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
139 request.message.header.request.extlen= 20;
140 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
141 request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(ptr->_namespace) +request.message.header.request.extlen));
142 request.message.body.delta= memcached_htonll(offset);
143 request.message.body.initial= memcached_htonll(initial);
144 request.message.body.expiration= htonl((uint32_t) expiration);
145
146 struct libmemcached_io_vector_st vector[]=
147 {
148 { request.bytes, sizeof(request.bytes) },
149 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
150 { key, key_length }
151 };
152
153 memcached_return_t rc;
154 if (memcached_failed(rc= memcached_vdo(instance, vector, 3, true)))
155 {
156 memcached_io_reset(instance);
157 return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
158 }
159
160 if (no_reply)
161 {
162 return MEMCACHED_SUCCESS;
163 }
164
165 return memcached_response(instance, (char*)value, sizeof(*value), NULL);
166 }
167
168 memcached_return_t memcached_increment(memcached_st *ptr,
169 const char *key, size_t key_length,
170 uint32_t offset,
171 uint64_t *value)
172 {
173 return memcached_increment_by_key(ptr, key, key_length, key, key_length, offset, value);
174 }
175
176 memcached_return_t memcached_decrement(memcached_st *ptr,
177 const char *key, size_t key_length,
178 uint32_t offset,
179 uint64_t *value)
180 {
181 return memcached_decrement_by_key(ptr, key, key_length, key, key_length, offset, value);
182 }
183
184 memcached_return_t memcached_increment_by_key(memcached_st *ptr,
185 const char *group_key, size_t group_key_length,
186 const char *key, size_t key_length,
187 uint64_t offset,
188 uint64_t *value)
189 {
190 memcached_return_t rc;
191 uint64_t local_value;
192 if (value == NULL)
193 {
194 value= &local_value;
195 }
196
197 if (memcached_failed(rc= initialize_query(ptr)))
198 {
199 return rc;
200 }
201
202 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
203 {
204 return rc;
205 }
206
207 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
208 if (ptr->flags.binary_protocol)
209 {
210 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
211 group_key, group_key_length, key, key_length,
212 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
213 value);
214 }
215 else
216 {
217 rc= text_incr_decr(ptr, "incr", group_key, group_key_length, key, key_length, offset, value);
218 }
219
220 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
221
222 return rc;
223 }
224
225 memcached_return_t memcached_decrement_by_key(memcached_st *ptr,
226 const char *group_key, size_t group_key_length,
227 const char *key, size_t key_length,
228 uint64_t offset,
229 uint64_t *value)
230 {
231 uint64_t local_value;
232 if (value == NULL)
233 {
234 value= &local_value;
235 }
236
237 memcached_return_t rc;
238 if (memcached_failed(rc= initialize_query(ptr)))
239 {
240 return rc;
241 }
242
243 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
244 {
245 return rc;
246 }
247
248
249 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
250 if (ptr->flags.binary_protocol)
251 {
252 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
253 group_key, group_key_length, key, key_length,
254 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
255 value);
256 }
257 else
258 {
259 rc= text_incr_decr(ptr, "decr", group_key, group_key_length, key, key_length, offset, value);
260 }
261
262 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
263
264 return rc;
265 }
266
267 memcached_return_t memcached_increment_with_initial(memcached_st *ptr,
268 const char *key,
269 size_t key_length,
270 uint64_t offset,
271 uint64_t initial,
272 time_t expiration,
273 uint64_t *value)
274 {
275 uint64_t local_value;
276 if (value == NULL)
277 {
278 value= &local_value;
279 }
280
281 return memcached_increment_with_initial_by_key(ptr, key, key_length,
282 key, key_length,
283 offset, initial, expiration, value);
284 }
285
286 memcached_return_t memcached_increment_with_initial_by_key(memcached_st *ptr,
287 const char *group_key,
288 size_t group_key_length,
289 const char *key,
290 size_t key_length,
291 uint64_t offset,
292 uint64_t initial,
293 time_t expiration,
294 uint64_t *value)
295 {
296 uint64_t local_value;
297 if (value == NULL)
298 {
299 value= &local_value;
300 }
301
302 memcached_return_t rc;
303 if (memcached_failed(rc= initialize_query(ptr)))
304 {
305 return rc;
306 }
307
308 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
309 {
310 return rc;
311 }
312
313 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
314 if (ptr->flags.binary_protocol)
315 {
316 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
317 group_key, group_key_length, key, key_length,
318 offset, initial, (uint32_t)expiration,
319 value);
320 }
321 else
322 {
323 rc= MEMCACHED_PROTOCOL_ERROR;
324 }
325
326 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
327
328 return rc;
329 }
330
331 memcached_return_t memcached_decrement_with_initial(memcached_st *ptr,
332 const char *key,
333 size_t key_length,
334 uint64_t offset,
335 uint64_t initial,
336 time_t expiration,
337 uint64_t *value)
338 {
339 uint64_t local_value;
340 if (value == NULL)
341 {
342 value= &local_value;
343 }
344
345 return memcached_decrement_with_initial_by_key(ptr, key, key_length,
346 key, key_length,
347 offset, initial, expiration, value);
348 }
349
350 memcached_return_t memcached_decrement_with_initial_by_key(memcached_st *ptr,
351 const char *group_key,
352 size_t group_key_length,
353 const char *key,
354 size_t key_length,
355 uint64_t offset,
356 uint64_t initial,
357 time_t expiration,
358 uint64_t *value)
359 {
360 uint64_t local_value;
361 if (value == NULL)
362 {
363 value= &local_value;
364 }
365
366 memcached_return_t rc;
367 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
368 {
369 return rc;
370 }
371
372 if (memcached_failed(rc= initialize_query(ptr)))
373 {
374 return rc;
375 }
376
377
378 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
379 if (ptr->flags.binary_protocol)
380 {
381 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
382 group_key, group_key_length, key, key_length,
383 offset, initial, (uint32_t)expiration,
384 value);
385 }
386 else
387 {
388 rc= MEMCACHED_PROTOCOL_ERROR;
389 }
390
391 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
392
393 return rc;
394 }
395