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