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