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