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