Abstraction (which will save us merge hell with 1.2).
[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 org::libmemcached::Instance *server;
196 memcached_return_t read_ret= MEMCACHED_SUCCESS;
197 while ((server= memcached_io_get_readable_server(ptr, read_ret)))
198 {
199 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
200 *error= memcached_response(server, buffer, sizeof(buffer), result);
201
202 if (*error == MEMCACHED_IN_PROGRESS)
203 {
204 continue;
205 }
206 else if (*error == MEMCACHED_SUCCESS)
207 {
208 result->count++;
209 return result;
210 }
211 else if (*error == MEMCACHED_END)
212 {
213 memcached_server_response_reset(server);
214 }
215 else if (*error != MEMCACHED_NOTFOUND)
216 {
217 break;
218 }
219 }
220
221 if (*error == MEMCACHED_NOTFOUND and result->count)
222 {
223 *error= MEMCACHED_END;
224 }
225 else if (*error == MEMCACHED_MAXIMUM_RETURN and result->count)
226 {
227 *error= MEMCACHED_END;
228 }
229 else if (*error == MEMCACHED_MAXIMUM_RETURN) // while() loop was never entered
230 {
231 *error= MEMCACHED_NOTFOUND;
232 }
233 else if (*error == MEMCACHED_SUCCESS)
234 {
235 *error= MEMCACHED_END;
236 }
237 else if (result->count == 0)
238 {
239 *error= MEMCACHED_NOTFOUND;
240 }
241
242 /* We have completed reading data */
243 if (memcached_is_allocated(result))
244 {
245 memcached_result_free(result);
246 }
247 else
248 {
249 result->count= 0;
250 memcached_string_reset(&result->value);
251 }
252
253 return NULL;
254 }
255
256 memcached_return_t memcached_fetch_execute(memcached_st *shell,
257 memcached_execute_fn *callback,
258 void *context,
259 uint32_t number_of_callbacks)
260 {
261 Memcached* ptr= memcached2Memcached(shell);
262 memcached_result_st *result= &ptr->result;
263 memcached_return_t rc;
264 bool some_errors= false;
265
266 while ((result= memcached_fetch_result(ptr, result, &rc)))
267 {
268 if (memcached_failed(rc) and rc == MEMCACHED_NOTFOUND)
269 {
270 continue;
271 }
272 else if (memcached_failed(rc))
273 {
274 memcached_set_error(*ptr, rc, MEMCACHED_AT);
275 some_errors= true;
276 continue;
277 }
278
279 for (uint32_t x= 0; x < number_of_callbacks; x++)
280 {
281 memcached_return_t ret= (*callback[x])(ptr, result, context);
282 if (memcached_failed(ret))
283 {
284 some_errors= true;
285 memcached_set_error(*ptr, ret, MEMCACHED_AT);
286 break;
287 }
288 }
289 }
290
291 if (some_errors)
292 {
293 return MEMCACHED_SOME_ERRORS;
294 }
295
296 // If we were able to run all keys without issue we return
297 // MEMCACHED_SUCCESS
298 if (memcached_success(rc))
299 {
300 return MEMCACHED_SUCCESS;
301 }
302
303 return rc;
304 }