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