Possible fix for solaris compiler.
[m6w6/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_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(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, "NOT_FOUND\r\n", 11))
65 {
66 *value= 0;
67 rc= MEMCACHED_NOTFOUND;
68 }
69 else
70 {
71 *value= strtoull(buffer, (char **)NULL, 10);
72 rc= MEMCACHED_SUCCESS;
73 }
74
75 return rc;
76 }
77
78 static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd,
79 const char *master_key, size_t master_key_length,
80 const char *key, size_t key_length,
81 uint64_t offset, uint64_t initial,
82 uint32_t expiration,
83 uint64_t *value)
84 {
85 uint32_t server_key;
86 memcached_server_instance_st *instance;
87 bool no_reply= ptr->flags.no_reply;
88
89 unlikely (memcached_server_count(ptr) == 0)
90 return MEMCACHED_NO_SERVERS;
91
92 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
93 instance= memcached_server_instance_fetch(ptr, server_key);
94
95 if (no_reply)
96 {
97 if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
98 cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
99 if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
100 cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
101 }
102 protocol_binary_request_incr request= {.bytes= {0}};
103
104 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
105 request.message.header.request.opcode= cmd;
106 request.message.header.request.keylen= htons((uint16_t) key_length);
107 request.message.header.request.extlen= 20;
108 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
109 request.message.header.request.bodylen= htonl((uint32_t) (key_length + request.message.header.request.extlen));
110 request.message.body.delta= htonll(offset);
111 request.message.body.initial= htonll(initial);
112 request.message.body.expiration= htonl((uint32_t) expiration);
113
114 if ((memcached_do(instance, request.bytes,
115 sizeof(request.bytes), false) != MEMCACHED_SUCCESS) ||
116 (memcached_io_write(instance, key, key_length, true) == -1))
117 {
118 memcached_io_reset(instance);
119 return MEMCACHED_WRITE_FAILURE;
120 }
121
122 if (no_reply)
123 return MEMCACHED_SUCCESS;
124 return memcached_response(instance, (char*)value, sizeof(*value), NULL);
125 }
126
127 memcached_return_t memcached_increment(memcached_st *ptr,
128 const char *key, size_t key_length,
129 uint32_t offset,
130 uint64_t *value)
131 {
132 return memcached_increment_by_key(ptr, key, key_length, key, key_length, offset, value);
133 }
134
135 memcached_return_t memcached_decrement(memcached_st *ptr,
136 const char *key, size_t key_length,
137 uint32_t offset,
138 uint64_t *value)
139 {
140 return memcached_decrement_by_key(ptr, key, key_length, key, key_length, offset, value);
141 }
142
143 memcached_return_t memcached_increment_by_key(memcached_st *ptr,
144 const char *master_key, size_t master_key_length,
145 const char *key, size_t key_length,
146 uint64_t offset,
147 uint64_t *value)
148 {
149 memcached_return_t rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
150 unlikely (rc != MEMCACHED_SUCCESS)
151 return rc;
152
153 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
154 if (ptr->flags.binary_protocol)
155 {
156 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
157 master_key, master_key_length, key, key_length,
158 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
159 value);
160 }
161 else
162 {
163 rc= text_incr_decr(ptr, "incr", master_key, master_key_length, key, key_length, offset, value);
164 }
165
166 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
167
168 return rc;
169 }
170
171 memcached_return_t memcached_decrement_by_key(memcached_st *ptr,
172 const char *master_key, size_t master_key_length,
173 const char *key, size_t key_length,
174 uint64_t offset,
175 uint64_t *value)
176 {
177 memcached_return_t rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
178 unlikely (rc != MEMCACHED_SUCCESS)
179 return rc;
180
181 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
182 if (ptr->flags.binary_protocol)
183 {
184 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
185 master_key, master_key_length, key, key_length,
186 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
187 value);
188 }
189 else
190 {
191 rc= text_incr_decr(ptr, "decr", master_key, master_key_length, key, key_length, offset, value);
192 }
193
194 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
195
196 return rc;
197 }
198
199 memcached_return_t memcached_increment_with_initial(memcached_st *ptr,
200 const char *key,
201 size_t key_length,
202 uint64_t offset,
203 uint64_t initial,
204 time_t expiration,
205 uint64_t *value)
206 {
207 return memcached_increment_with_initial_by_key(ptr, key, key_length,
208 key, key_length,
209 offset, initial, expiration, value);
210 }
211
212 memcached_return_t memcached_increment_with_initial_by_key(memcached_st *ptr,
213 const char *master_key,
214 size_t master_key_length,
215 const char *key,
216 size_t key_length,
217 uint64_t offset,
218 uint64_t initial,
219 time_t expiration,
220 uint64_t *value)
221 {
222 memcached_return_t rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
223 unlikely (rc != MEMCACHED_SUCCESS)
224 return rc;
225
226 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
227 if (ptr->flags.binary_protocol)
228 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
229 master_key, master_key_length, key, key_length,
230 offset, initial, (uint32_t)expiration,
231 value);
232 else
233 rc= MEMCACHED_PROTOCOL_ERROR;
234
235 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
236
237 return rc;
238 }
239
240 memcached_return_t memcached_decrement_with_initial(memcached_st *ptr,
241 const char *key,
242 size_t key_length,
243 uint64_t offset,
244 uint64_t initial,
245 time_t expiration,
246 uint64_t *value)
247 {
248 return memcached_decrement_with_initial_by_key(ptr, key, key_length,
249 key, key_length,
250 offset, initial, expiration, value);
251 }
252
253 memcached_return_t memcached_decrement_with_initial_by_key(memcached_st *ptr,
254 const char *master_key,
255 size_t master_key_length,
256 const char *key,
257 size_t key_length,
258 uint64_t offset,
259 uint64_t initial,
260 time_t expiration,
261 uint64_t *value)
262 {
263 memcached_return_t rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
264 unlikely (rc != MEMCACHED_SUCCESS)
265 return rc;
266
267 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
268 if (ptr->flags.binary_protocol)
269 {
270 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
271 master_key, master_key_length, key, key_length,
272 offset, initial, (uint32_t)expiration,
273 value);
274 }
275 else
276 {
277 rc= MEMCACHED_PROTOCOL_ERROR;
278 }
279
280 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
281
282 return rc;
283 }
284