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