libmemcached: sasl (fix #14 and #17)
[awesomized/libmemcached] / libmemcached / fetch.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011-2012 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 char *memcached_fetch(memcached_st *shell, char *key, size_t *key_length,
41 size_t *value_length,
42 uint32_t *flags,
43 memcached_return_t *error)
44 {
45 Memcached* ptr= memcached2Memcached(shell);
46 memcached_return_t unused;
47 if (error == NULL)
48 {
49 error= &unused;
50 }
51
52 if (memcached_is_udp(ptr))
53 {
54 if (value_length)
55 {
56 *value_length= 0;
57 }
58
59 if (key_length)
60 {
61 *key_length= 0;
62 }
63
64 if (flags)
65 {
66 *flags= 0;
67 }
68
69 if (key)
70 {
71 *key= 0;
72 }
73
74 *error= MEMCACHED_NOT_SUPPORTED;
75 return NULL;
76 }
77
78 memcached_result_st *result_buffer= &ptr->result;
79 result_buffer= memcached_fetch_result(ptr, result_buffer, error);
80 if (result_buffer == NULL or memcached_failed(*error))
81 {
82 WATCHPOINT_ASSERT(result_buffer == NULL);
83 if (value_length)
84 {
85 *value_length= 0;
86 }
87
88 if (key_length)
89 {
90 *key_length= 0;
91 }
92
93 if (flags)
94 {
95 *flags= 0;
96 }
97
98 if (key)
99 {
100 *key= 0;
101 }
102
103 return NULL;
104 }
105
106 if (value_length)
107 {
108 *value_length= memcached_string_length(&result_buffer->value);
109 }
110
111 if (key)
112 {
113 if (result_buffer->key_length > MEMCACHED_MAX_KEY)
114 {
115 *error= MEMCACHED_KEY_TOO_BIG;
116 if (value_length)
117 {
118 *value_length= 0;
119 }
120
121 if (key_length)
122 {
123 *key_length= 0;
124 }
125
126 if (flags)
127 {
128 *flags= 0;
129 }
130
131 if (key)
132 {
133 *key= 0;
134 }
135
136 return NULL;
137 }
138
139 strncpy(key, result_buffer->item_key, result_buffer->key_length); // For the binary protocol we will cut off the key :(
140 if (key_length)
141 {
142 *key_length= result_buffer->key_length;
143 }
144 }
145
146 if (flags)
147 {
148 *flags= result_buffer->item_flags;
149 }
150
151 return memcached_string_take_value(&result_buffer->value);
152 }
153
154 memcached_result_st *memcached_fetch_result(memcached_st *ptr,
155 memcached_result_st *result,
156 memcached_return_t *error)
157 {
158 memcached_return_t unused;
159 if (error == NULL)
160 {
161 error= &unused;
162 }
163
164 if (ptr == NULL)
165 {
166 *error= MEMCACHED_INVALID_ARGUMENTS;
167 return NULL;
168 }
169
170 if (memcached_is_udp(ptr))
171 {
172 *error= MEMCACHED_NOT_SUPPORTED;
173 return NULL;
174 }
175
176 if (result == NULL)
177 {
178 // If we have already initialized (ie it is in use) our internal, we
179 // create one.
180 if (memcached_is_initialized(&ptr->result))
181 {
182 if ((result= memcached_result_create(ptr, NULL)) == NULL)
183 {
184 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
185 return NULL;
186 }
187 }
188 else
189 {
190 result= memcached_result_create(ptr, &ptr->result);
191 }
192 }
193
194 *error= MEMCACHED_MAXIMUM_RETURN; // We use this to see if we ever go into the loop
195 memcached_instance_st *server;
196 memcached_return_t read_ret= MEMCACHED_SUCCESS;
197 bool connection_failures= false;
198 while ((server= memcached_io_get_readable_server(ptr, read_ret)))
199 {
200 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
201 *error= memcached_response(server, buffer, sizeof(buffer), result);
202
203 if (*error == MEMCACHED_IN_PROGRESS)
204 {
205 continue;
206 }
207 else if (*error == MEMCACHED_CONNECTION_FAILURE)
208 {
209 connection_failures= true;
210 continue;
211 }
212 else if (*error == MEMCACHED_SUCCESS)
213 {
214 result->count++;
215 return result;
216 }
217 else if (*error == MEMCACHED_END)
218 {
219 memcached_server_response_reset(server);
220 }
221 else if (*error != MEMCACHED_NOTFOUND)
222 {
223 break;
224 }
225 }
226
227 if (*error == MEMCACHED_NOTFOUND and result->count)
228 {
229 *error= MEMCACHED_END;
230 }
231 else if (*error == MEMCACHED_MAXIMUM_RETURN and result->count)
232 {
233 *error= MEMCACHED_END;
234 }
235 else if (*error == MEMCACHED_MAXIMUM_RETURN) // while() loop was never entered
236 {
237 *error= MEMCACHED_NOTFOUND;
238 }
239 else if (connection_failures)
240 {
241 /*
242 If we have a connection failure to some servers, the caller may
243 wish to treat that differently to getting a definitive NOT_FOUND
244 from all servers, so return MEMCACHED_CONNECTION_FAILURE to allow
245 that.
246 */
247 *error= MEMCACHED_CONNECTION_FAILURE;
248 }
249 else if (*error == MEMCACHED_SUCCESS)
250 {
251 *error= MEMCACHED_END;
252 }
253 else if (result->count == 0)
254 {
255 *error= MEMCACHED_NOTFOUND;
256 }
257
258 /* We have completed reading data */
259 if (memcached_is_allocated(result))
260 {
261 memcached_result_free(result);
262 }
263 else
264 {
265 result->count= 0;
266 memcached_string_reset(&result->value);
267 }
268
269 return NULL;
270 }
271
272 memcached_return_t memcached_fetch_execute(memcached_st *shell,
273 memcached_execute_fn *callback,
274 void *context,
275 uint32_t number_of_callbacks)
276 {
277 Memcached* ptr= memcached2Memcached(shell);
278 memcached_result_st *result= &ptr->result;
279 memcached_return_t rc;
280 bool some_errors= false;
281
282 while ((result= memcached_fetch_result(ptr, result, &rc)))
283 {
284 if (memcached_failed(rc) and rc == MEMCACHED_NOTFOUND)
285 {
286 continue;
287 }
288 else if (memcached_failed(rc))
289 {
290 memcached_set_error(*ptr, rc, MEMCACHED_AT);
291 some_errors= true;
292 continue;
293 }
294
295 for (uint32_t x= 0; x < number_of_callbacks; x++)
296 {
297 memcached_return_t ret= (*callback[x])(ptr, result, context);
298 if (memcached_failed(ret))
299 {
300 some_errors= true;
301 memcached_set_error(*ptr, ret, MEMCACHED_AT);
302 break;
303 }
304 }
305 }
306
307 if (some_errors)
308 {
309 return MEMCACHED_SOME_ERRORS;
310 }
311
312 // If we were able to run all keys without issue we return
313 // MEMCACHED_SUCCESS
314 if (memcached_success(rc))
315 {
316 return MEMCACHED_SUCCESS;
317 }
318
319 return rc;
320 }