Merge in some updates to style.
[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 { sizeof(request.bytes), request.bytes },
149 { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
150 { key_length, key }
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 return MEMCACHED_SUCCESS;
162
163 return memcached_response(instance, (char*)value, sizeof(*value), NULL);
164 }
165
166 memcached_return_t memcached_increment(memcached_st *ptr,
167 const char *key, size_t key_length,
168 uint32_t offset,
169 uint64_t *value)
170 {
171 return memcached_increment_by_key(ptr, key, key_length, key, key_length, offset, value);
172 }
173
174 memcached_return_t memcached_decrement(memcached_st *ptr,
175 const char *key, size_t key_length,
176 uint32_t offset,
177 uint64_t *value)
178 {
179 return memcached_decrement_by_key(ptr, key, key_length, key, key_length, offset, value);
180 }
181
182 memcached_return_t memcached_increment_by_key(memcached_st *ptr,
183 const char *group_key, size_t group_key_length,
184 const char *key, size_t key_length,
185 uint64_t offset,
186 uint64_t *value)
187 {
188 memcached_return_t rc;
189 uint64_t local_value;
190 if (value == NULL)
191 {
192 value= &local_value;
193 }
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 (value == NULL)
231 {
232 value= &local_value;
233 }
234
235 memcached_return_t rc;
236 if (memcached_failed(rc= initialize_query(ptr)))
237 {
238 return rc;
239 }
240
241 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
242 {
243 return rc;
244 }
245
246
247 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
248 if (ptr->flags.binary_protocol)
249 {
250 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
251 group_key, group_key_length, key, key_length,
252 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
253 value);
254 }
255 else
256 {
257 rc= text_incr_decr(ptr, "decr", group_key, group_key_length, key, key_length, offset, value);
258 }
259
260 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
261
262 return rc;
263 }
264
265 memcached_return_t memcached_increment_with_initial(memcached_st *ptr,
266 const char *key,
267 size_t key_length,
268 uint64_t offset,
269 uint64_t initial,
270 time_t expiration,
271 uint64_t *value)
272 {
273 uint64_t local_value;
274 if (value == NULL)
275 {
276 value= &local_value;
277 }
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 (value == NULL)
296 {
297 value= &local_value;
298 }
299
300 memcached_return_t rc;
301 if (memcached_failed(rc= initialize_query(ptr)))
302 {
303 return rc;
304 }
305
306 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
307 {
308 return rc;
309 }
310
311 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
312 if (ptr->flags.binary_protocol)
313 {
314 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
315 group_key, group_key_length, key, key_length,
316 offset, initial, (uint32_t)expiration,
317 value);
318 }
319 else
320 {
321 rc= MEMCACHED_PROTOCOL_ERROR;
322 }
323
324 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
325
326 return rc;
327 }
328
329 memcached_return_t memcached_decrement_with_initial(memcached_st *ptr,
330 const char *key,
331 size_t key_length,
332 uint64_t offset,
333 uint64_t initial,
334 time_t expiration,
335 uint64_t *value)
336 {
337 uint64_t local_value;
338 if (value == NULL)
339 {
340 value= &local_value;
341 }
342
343 return memcached_decrement_with_initial_by_key(ptr, key, key_length,
344 key, key_length,
345 offset, initial, expiration, value);
346 }
347
348 memcached_return_t memcached_decrement_with_initial_by_key(memcached_st *ptr,
349 const char *group_key,
350 size_t group_key_length,
351 const char *key,
352 size_t key_length,
353 uint64_t offset,
354 uint64_t initial,
355 time_t expiration,
356 uint64_t *value)
357 {
358 uint64_t local_value;
359 if (value == NULL)
360 {
361 value= &local_value;
362 }
363
364 memcached_return_t rc;
365 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
366 {
367 return rc;
368 }
369
370 if (memcached_failed(rc= initialize_query(ptr)))
371 {
372 return rc;
373 }
374
375
376 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
377 if (ptr->flags.binary_protocol)
378 {
379 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
380 group_key, group_key_length, key, key_length,
381 offset, initial, (uint32_t)expiration,
382 value);
383 }
384 else
385 {
386 rc= MEMCACHED_PROTOCOL_ERROR;
387 }
388
389 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
390
391 return rc;
392 }
393