Merge in code for C++ compiling of libmemcached.
[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 memcached_return_t rc;
48 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
49 uint32_t server_key;
50 memcached_server_write_instance_st instance;
51 bool no_reply= ptr->flags.no_reply;
52
53 if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
54 return MEMCACHED_BAD_KEY_PROVIDED;
55
56 server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
57 instance= memcached_server_instance_fetch(ptr, server_key);
58
59 int send_length;
60 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
61 "%s %.*s%.*s %" PRIu64 "%s\r\n", verb,
62 memcached_print_array(ptr->prefix_key),
63 (int)key_length, key,
64 offset, no_reply ? " noreply" : "");
65 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
66 return MEMCACHED_WRITE_FAILURE;
67
68 rc= memcached_do(instance, buffer, (size_t)send_length, true);
69 if (no_reply || rc != MEMCACHED_SUCCESS)
70 return rc;
71
72 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
73
74 /*
75 So why recheck responce? Because the protocol is brain dead :)
76 The number returned might end up equaling one of the string
77 values. Less chance of a mistake with strncmp() so we will
78 use it. We still called memcached_response() though since it
79 worked its magic for non-blocking IO.
80 */
81 if (! strncmp(buffer, "ERROR\r\n", 7))
82 {
83 *value= 0;
84 rc= MEMCACHED_PROTOCOL_ERROR;
85 }
86 else if (! strncmp(buffer, "CLIENT_ERROR\r\n", 14))
87 {
88 *value= 0;
89 rc= MEMCACHED_PROTOCOL_ERROR;
90 }
91 else if (!strncmp(buffer, "NOT_FOUND\r\n", 11))
92 {
93 *value= 0;
94 rc= MEMCACHED_NOTFOUND;
95 }
96 else
97 {
98 *value= strtoull(buffer, (char **)NULL, 10);
99 rc= MEMCACHED_SUCCESS;
100 }
101
102 return rc;
103 }
104
105 static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd,
106 const char *group_key, size_t group_key_length,
107 const char *key, size_t key_length,
108 uint64_t offset, uint64_t initial,
109 uint32_t expiration,
110 uint64_t *value)
111 {
112 uint32_t server_key;
113 memcached_server_write_instance_st instance;
114 bool no_reply= ptr->flags.no_reply;
115
116 if (memcached_server_count(ptr) == 0)
117 return memcached_set_error(ptr, MEMCACHED_NO_SERVERS, NULL);
118
119 server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
120 instance= memcached_server_instance_fetch(ptr, server_key);
121
122 if (no_reply)
123 {
124 if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
125 cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
126 if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
127 cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
128 }
129 protocol_binary_request_incr request= {}; // = {.bytes= {0}};
130
131 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
132 request.message.header.request.opcode= cmd;
133 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->prefix_key)));
134 request.message.header.request.extlen= 20;
135 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
136 request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(ptr->prefix_key) +request.message.header.request.extlen));
137 request.message.body.delta= htonll(offset);
138 request.message.body.initial= htonll(initial);
139 request.message.body.expiration= htonl((uint32_t) expiration);
140
141 struct libmemcached_io_vector_st vector[]=
142 {
143 { sizeof(request.bytes), request.bytes },
144 { memcached_array_size(ptr->prefix_key), ptr->prefix_key },
145 { key_length, key }
146 };
147
148 memcached_return_t rc;
149 if ((rc= memcached_vdo(instance, vector, 3, true)) != MEMCACHED_SUCCESS)
150 {
151 memcached_io_reset(instance);
152 return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
153 }
154
155 if (no_reply)
156 return MEMCACHED_SUCCESS;
157
158 return memcached_response(instance, (char*)value, sizeof(*value), NULL);
159 }
160
161 memcached_return_t memcached_increment(memcached_st *ptr,
162 const char *key, size_t key_length,
163 uint32_t offset,
164 uint64_t *value)
165 {
166 uint64_t local_value;
167 if (! value)
168 value= &local_value;
169
170 return memcached_increment_by_key(ptr, key, key_length, key, key_length, offset, value);
171 }
172
173 memcached_return_t memcached_decrement(memcached_st *ptr,
174 const char *key, size_t key_length,
175 uint32_t offset,
176 uint64_t *value)
177 {
178 uint64_t local_value;
179 if (! value)
180 value= &local_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