Fix it so that we use the result object for storing the numeric result that we eventu...
[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 void auto_response(memcached_server_write_instance_st instance, const bool reply, memcached_return_t& rc, uint64_t* value)
41 {
42 // If the message was successfully sent, then get the response, otherwise
43 // fail.
44 if (memcached_success(rc))
45 {
46 if (reply == false)
47 {
48 *value= UINT64_MAX;
49 return;
50 }
51
52 rc= memcached_response(instance, &instance->root->result);
53 }
54
55 if (memcached_success(rc))
56 {
57 *value= instance->root->result.numeric_value;
58 }
59 else
60 {
61 *value= UINT64_MAX;
62 }
63 }
64
65 static memcached_return_t text_incr_decr(memcached_server_write_instance_st instance,
66 const bool is_incr,
67 const char *key, size_t key_length,
68 const uint64_t offset,
69 const bool reply)
70 {
71 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
72
73 int send_length= snprintf(buffer, sizeof(buffer), " %" PRIu64, offset);
74 if (size_t(send_length) >= sizeof(buffer) or send_length < 0)
75 {
76 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
77 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
78 }
79
80 libmemcached_io_vector_st vector[]=
81 {
82 { NULL, 0 },
83 { memcached_literal_param("incr ") },
84 { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) },
85 { key, key_length },
86 { buffer, send_length },
87 { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") },
88 { memcached_literal_param("\r\n") }
89 };
90
91 if (is_incr == false)
92 {
93 vector[1].buffer= "decr ";
94 }
95
96 return memcached_vdo(instance, vector, 7, true);
97 }
98
99 static memcached_return_t binary_incr_decr(memcached_server_write_instance_st instance,
100 protocol_binary_command cmd,
101 const char *key, const size_t key_length,
102 const uint64_t offset,
103 const uint64_t initial,
104 const uint32_t expiration,
105 const bool reply)
106 {
107 if (reply == false)
108 {
109 if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
110 {
111 cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
112 }
113
114 if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
115 {
116 cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
117 }
118 }
119 protocol_binary_request_incr request= {}; // = {.bytes= {0}};
120
121 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
122 request.message.header.request.opcode= cmd;
123 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(instance->root->_namespace)));
124 request.message.header.request.extlen= 20;
125 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
126 request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(instance->root->_namespace) +request.message.header.request.extlen));
127 request.message.body.delta= memcached_htonll(offset);
128 request.message.body.initial= memcached_htonll(initial);
129 request.message.body.expiration= htonl((uint32_t) expiration);
130
131 libmemcached_io_vector_st vector[]=
132 {
133 { NULL, 0 },
134 { request.bytes, sizeof(request.bytes) },
135 { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) },
136 { key, key_length }
137 };
138
139 return memcached_vdo(instance, vector, 4, true);
140 }
141
142 memcached_return_t memcached_increment(memcached_st *ptr,
143 const char *key, size_t key_length,
144 uint32_t offset,
145 uint64_t *value)
146 {
147 return memcached_increment_by_key(ptr, key, key_length, key, key_length, offset, value);
148 }
149
150 memcached_return_t memcached_decrement(memcached_st *ptr,
151 const char *key, size_t key_length,
152 uint32_t offset,
153 uint64_t *value)
154 {
155 return memcached_decrement_by_key(ptr, key, key_length, key, key_length, offset, value);
156 }
157
158 memcached_return_t memcached_increment_by_key(memcached_st *ptr,
159 const char *group_key, size_t group_key_length,
160 const char *key, size_t key_length,
161 uint64_t offset,
162 uint64_t *value)
163 {
164 memcached_return_t rc;
165 uint64_t local_value;
166 if (value == NULL)
167 {
168 value= &local_value;
169 }
170
171 if (memcached_failed(rc= initialize_query(ptr, true)))
172 {
173 return rc;
174 }
175
176 if (memcached_failed(rc= memcached_key_test(*ptr, (const char **)&key, &key_length, 1)))
177 {
178 return rc;
179 }
180
181 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
182 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
183
184 bool reply= memcached_is_replying(instance->root);
185
186 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
187 if (memcached_is_binary(ptr))
188 {
189 rc= binary_incr_decr(instance, PROTOCOL_BINARY_CMD_INCREMENT,
190 key, key_length,
191 uint64_t(offset), 0, MEMCACHED_EXPIRATION_NOT_ADD,
192 reply);
193 }
194 else
195 {
196 rc= text_incr_decr(instance, true, key, key_length, offset, reply);
197 }
198
199 auto_response(instance, reply, rc, value);
200
201 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
202
203 return rc;
204 }
205
206 memcached_return_t memcached_decrement_by_key(memcached_st *ptr,
207 const char *group_key, size_t group_key_length,
208 const char *key, size_t key_length,
209 uint64_t offset,
210 uint64_t *value)
211 {
212 uint64_t local_value;
213 if (value == NULL)
214 {
215 value= &local_value;
216 }
217
218 memcached_return_t rc;
219 if (memcached_failed(rc= initialize_query(ptr, true)))
220 {
221 return rc;
222 }
223
224 if (memcached_failed(rc= memcached_key_test(*ptr, (const char **)&key, &key_length, 1)))
225 {
226 return rc;
227 }
228
229
230 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
231 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
232
233 bool reply= memcached_is_replying(instance->root);
234
235 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
236 if (memcached_is_binary(ptr))
237 {
238 rc= binary_incr_decr(instance, PROTOCOL_BINARY_CMD_DECREMENT,
239 key, key_length,
240 offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
241 reply);
242 }
243 else
244 {
245 rc= text_incr_decr(instance, false, key, key_length, offset, reply);
246 }
247
248 auto_response(instance, reply, rc, value);
249
250 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
251
252 return rc;
253 }
254
255 memcached_return_t memcached_increment_with_initial(memcached_st *ptr,
256 const char *key,
257 size_t key_length,
258 uint64_t offset,
259 uint64_t initial,
260 time_t expiration,
261 uint64_t *value)
262 {
263 return memcached_increment_with_initial_by_key(ptr, key, key_length,
264 key, key_length,
265 offset, initial, expiration, value);
266 }
267
268 memcached_return_t memcached_increment_with_initial_by_key(memcached_st *ptr,
269 const char *group_key,
270 size_t group_key_length,
271 const char *key,
272 size_t key_length,
273 uint64_t offset,
274 uint64_t initial,
275 time_t expiration,
276 uint64_t *value)
277 {
278 uint64_t local_value;
279 if (value == NULL)
280 {
281 value= &local_value;
282 }
283
284 memcached_return_t rc;
285 if (memcached_failed(rc= initialize_query(ptr, true)))
286 {
287 return rc;
288 }
289
290 if (memcached_failed(rc= memcached_key_test(*ptr, (const char **)&key, &key_length, 1)))
291 {
292 return rc;
293 }
294
295 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
296 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
297
298 bool reply= memcached_is_replying(instance->root);
299
300 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
301 if (memcached_is_binary(ptr))
302 {
303 rc= binary_incr_decr(instance, PROTOCOL_BINARY_CMD_INCREMENT,
304 key, key_length,
305 offset, initial, uint32_t(expiration),
306 reply);
307
308 }
309 else
310 {
311 rc= memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
312 memcached_literal_param("memcached_increment_with_initial_by_key() is not supported via the ASCII protocol"));
313 }
314
315 auto_response(instance, reply, rc, value);
316
317 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
318
319 return rc;
320 }
321
322 memcached_return_t memcached_decrement_with_initial(memcached_st *ptr,
323 const char *key,
324 size_t key_length,
325 uint64_t offset,
326 uint64_t initial,
327 time_t expiration,
328 uint64_t *value)
329 {
330 return memcached_decrement_with_initial_by_key(ptr, key, key_length,
331 key, key_length,
332 offset, initial, expiration, value);
333 }
334
335 memcached_return_t memcached_decrement_with_initial_by_key(memcached_st *ptr,
336 const char *group_key,
337 size_t group_key_length,
338 const char *key,
339 size_t key_length,
340 uint64_t offset,
341 uint64_t initial,
342 time_t expiration,
343 uint64_t *value)
344 {
345 uint64_t local_value;
346 if (value == NULL)
347 {
348 value= &local_value;
349 }
350
351 memcached_return_t rc;
352 if (memcached_failed(rc= initialize_query(ptr, true)))
353 {
354 return rc;
355 }
356
357 if (memcached_failed(rc= memcached_key_test(*ptr, (const char **)&key, &key_length, 1)))
358 {
359 return rc;
360 }
361
362 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
363 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
364
365 bool reply= memcached_is_replying(instance->root);
366
367
368 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
369 if (memcached_is_binary(ptr))
370 {
371 rc= binary_incr_decr(instance, PROTOCOL_BINARY_CMD_DECREMENT,
372 key, key_length,
373 offset, initial, uint32_t(expiration),
374 reply);
375
376 }
377 else
378 {
379 rc= memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
380 memcached_literal_param("memcached_decrement_with_initial_by_key() is not supported via the ASCII protocol"));
381 }
382
383 auto_response(instance, reply, rc, value);
384
385 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
386
387 return rc;
388 }