Ignore *.exe
[awesomized/libmemcached] / libmemcached / auto.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: Methods for adding or decrementing values from an object in memcached
9 *
10 */
11
12 #include "common.h"
13
14 static memcached_return_t text_incr_decr(memcached_st *ptr,
15 const char *verb,
16 const char *master_key, size_t master_key_length,
17 const char *key, size_t key_length,
18 uint64_t offset,
19 uint64_t *value)
20 {
21 size_t send_length;
22 memcached_return_t rc;
23 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
24 uint32_t server_key;
25 memcached_server_write_instance_st instance;
26 bool no_reply= ptr->flags.no_reply;
27
28 unlikely (memcached_server_count(ptr) == 0)
29 return MEMCACHED_NO_SERVERS;
30
31 if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
32 return MEMCACHED_BAD_KEY_PROVIDED;
33
34 server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
35 instance= memcached_server_instance_fetch(ptr, server_key);
36
37 send_length= (size_t)snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
38 "%s %.*s%.*s %" PRIu64 "%s\r\n", verb,
39 (int)ptr->prefix_key_length,
40 ptr->prefix_key,
41 (int)key_length, key,
42 offset, no_reply ? " noreply" : "");
43 unlikely (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
44 return MEMCACHED_WRITE_FAILURE;
45
46 rc= memcached_do(instance, buffer, send_length, true);
47 if (no_reply || rc != MEMCACHED_SUCCESS)
48 return rc;
49
50 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
51
52 /*
53 So why recheck responce? Because the protocol is brain dead :)
54 The number returned might end up equaling one of the string
55 values. Less chance of a mistake with strncmp() so we will
56 use it. We still called memcached_response() though since it
57 worked its magic for non-blocking IO.
58 */
59 if (! strncmp(buffer, "ERROR\r\n", 7))
60 {
61 *value= 0;
62 rc= MEMCACHED_PROTOCOL_ERROR;
63 }
64 else if (! strncmp(buffer, "CLIENT_ERROR\r\n", 14))
65 {
66 *value= 0;
67 rc= MEMCACHED_PROTOCOL_ERROR;
68 }
69 else if (!strncmp(buffer, "NOT_FOUND\r\n", 11))
70 {
71 *value= 0;
72 rc= MEMCACHED_NOTFOUND;
73 }
74 else
75 {
76 *value= strtoull(buffer, (char **)NULL, 10);
77 rc= MEMCACHED_SUCCESS;
78 }
79
80 return rc;
81 }
82
83 static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd,
84 const char *master_key, size_t master_key_length,
85 const char *key, size_t key_length,
86 uint64_t offset, uint64_t initial,
87 uint32_t expiration,
88 uint64_t *value)
89 {
90 uint32_t server_key;
91 memcached_server_write_instance_st instance;
92 bool no_reply= ptr->flags.no_reply;
93
94 unlikely (memcached_server_count(ptr) == 0)
95 return MEMCACHED_NO_SERVERS;
96
97 server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
98 instance= memcached_server_instance_fetch(ptr, server_key);
99
100 if (no_reply)
101 {
102 if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
103 cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
104 if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
105 cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
106 }
107 protocol_binary_request_incr request= {.bytes= {0}};
108
109 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
110 request.message.header.request.opcode= cmd;
111 request.message.header.request.keylen= htons((uint16_t)(key_length + ptr->prefix_key_length));
112 request.message.header.request.extlen= 20;
113 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
114 request.message.header.request.bodylen= htonl((uint32_t)(key_length + ptr->prefix_key_length + request.message.header.request.extlen));
115 request.message.body.delta= htonll(offset);
116 request.message.body.initial= htonll(initial);
117 request.message.body.expiration= htonl((uint32_t) expiration);
118
119 struct __write_vector_st vector[]=
120 {
121 { .length= sizeof(request.bytes), .buffer= request.bytes },
122 { .length= ptr->prefix_key_length, .buffer= ptr->prefix_key },
123 { .length= key_length, .buffer= key }
124 };
125
126 memcached_return_t rc;
127 if ((rc= memcached_vdo(instance, vector, 3, true)) != MEMCACHED_SUCCESS)
128 {
129 memcached_io_reset(instance);
130 return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
131 }
132
133 if (no_reply)
134 return MEMCACHED_SUCCESS;
135 return memcached_response(instance, (char*)value, sizeof(*value), NULL);
136 }
137
138 memcached_return_t memcached_increment(memcached_st *ptr,
139 const char *key, size_t key_length,
140 uint32_t offset,
141 uint64_t *value)
142 {
143 return memcached_increment_by_key(ptr, key, key_length, key, key_length, offset, value);
144 }
145
146 memcached_return_t memcached_decrement(memcached_st *ptr,
147 const char *key, size_t key_length,
148 uint32_t offset,
149 uint64_t *value)
150 {
151 return memcached_decrement_by_key(ptr, key, key_length, key, key_length, offset, value);
152 }
153
154 memcached_return_t memcached_increment_by_key(memcached_st *ptr,
155 const char *master_key, size_t master_key_length,
156 const char *key, size_t key_length,
157 uint64_t offset,
158 uint64_t *value)
159 {
160 memcached_return_t rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
161 unlikely (rc != MEMCACHED_SUCCESS)
162 return rc;
163
164 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
165 if (ptr->flags.binary_protocol)
166 {
167 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
168 master_key, master_key_length, key, key_length,
169 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
170 value);
171 }
172 else
173 {
174 rc= text_incr_decr(ptr, "incr", master_key, master_key_length, key, key_length, offset, value);
175 }
176
177 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
178
179 return rc;
180 }
181
182 memcached_return_t memcached_decrement_by_key(memcached_st *ptr,
183 const char *master_key, size_t master_key_length,
184 const char *key, size_t key_length,
185 uint64_t offset,
186 uint64_t *value)
187 {
188 memcached_return_t rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
189 unlikely (rc != MEMCACHED_SUCCESS)
190 return rc;
191
192 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
193 if (ptr->flags.binary_protocol)
194 {
195 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
196 master_key, master_key_length, key, key_length,
197 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
198 value);
199 }
200 else
201 {
202 rc= text_incr_decr(ptr, "decr", master_key, master_key_length, key, key_length, offset, value);
203 }
204
205 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
206
207 return rc;
208 }
209
210 memcached_return_t memcached_increment_with_initial(memcached_st *ptr,
211 const char *key,
212 size_t key_length,
213 uint64_t offset,
214 uint64_t initial,
215 time_t expiration,
216 uint64_t *value)
217 {
218 return memcached_increment_with_initial_by_key(ptr, key, key_length,
219 key, key_length,
220 offset, initial, expiration, value);
221 }
222
223 memcached_return_t memcached_increment_with_initial_by_key(memcached_st *ptr,
224 const char *master_key,
225 size_t master_key_length,
226 const char *key,
227 size_t key_length,
228 uint64_t offset,
229 uint64_t initial,
230 time_t expiration,
231 uint64_t *value)
232 {
233 memcached_return_t rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
234 unlikely (rc != MEMCACHED_SUCCESS)
235 return rc;
236
237 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
238 if (ptr->flags.binary_protocol)
239 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
240 master_key, master_key_length, key, key_length,
241 offset, initial, (uint32_t)expiration,
242 value);
243 else
244 rc= MEMCACHED_PROTOCOL_ERROR;
245
246 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
247
248 return rc;
249 }
250
251 memcached_return_t memcached_decrement_with_initial(memcached_st *ptr,
252 const char *key,
253 size_t key_length,
254 uint64_t offset,
255 uint64_t initial,
256 time_t expiration,
257 uint64_t *value)
258 {
259 return memcached_decrement_with_initial_by_key(ptr, key, key_length,
260 key, key_length,
261 offset, initial, expiration, value);
262 }
263
264 memcached_return_t memcached_decrement_with_initial_by_key(memcached_st *ptr,
265 const char *master_key,
266 size_t master_key_length,
267 const char *key,
268 size_t key_length,
269 uint64_t offset,
270 uint64_t initial,
271 time_t expiration,
272 uint64_t *value)
273 {
274 memcached_return_t rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
275 unlikely (rc != MEMCACHED_SUCCESS)
276 return rc;
277
278 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
279 if (ptr->flags.binary_protocol)
280 {
281 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
282 master_key, master_key_length, key, key_length,
283 offset, initial, (uint32_t)expiration,
284 value);
285 }
286 else
287 {
288 rc= MEMCACHED_PROTOCOL_ERROR;
289 }
290
291 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
292
293 return rc;
294 }
295