27ea3a8413722f987cbb3eebeee503c9820b0703
[m6w6/libmemcached] / libmemcached / fetch.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 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
41 size_t *value_length,
42 uint32_t *flags,
43 memcached_return_t *error)
44 {
45 memcached_result_st *result_buffer= &ptr->result;
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 result_buffer= memcached_fetch_result(ptr, result_buffer, error);
79 if (result_buffer == NULL or memcached_failed(*error))
80 {
81 WATCHPOINT_ASSERT(result_buffer == NULL);
82 if (value_length)
83 {
84 *value_length= 0;
85 }
86
87 if (key_length)
88 {
89 *key_length= 0;
90 }
91
92 if (flags)
93 {
94 *flags= 0;
95 }
96
97 if (key)
98 {
99 *key= 0;
100 }
101
102 return NULL;
103 }
104
105 if (value_length)
106 {
107 *value_length= memcached_string_length(&result_buffer->value);
108 }
109
110 if (key)
111 {
112 if (result_buffer->key_length > MEMCACHED_MAX_KEY)
113 {
114 *error= MEMCACHED_KEY_TOO_BIG;
115 if (value_length)
116 {
117 *value_length= 0;
118 }
119
120 if (key_length)
121 {
122 *key_length= 0;
123 }
124
125 if (flags)
126 {
127 *flags= 0;
128 }
129
130 if (key)
131 {
132 *key= 0;
133 }
134
135 return NULL;
136 }
137
138 strncpy(key, result_buffer->item_key, result_buffer->key_length); // For the binary protocol we will cut off the key :(
139 if (key_length)
140 {
141 *key_length= result_buffer->key_length;
142 }
143 }
144
145 if (flags)
146 {
147 *flags= result_buffer->item_flags;
148 }
149
150 return memcached_string_take_value(&result_buffer->value);
151 }
152
153 memcached_result_st *memcached_fetch_result(memcached_st *ptr,
154 memcached_result_st *result,
155 memcached_return_t *error)
156 {
157 memcached_return_t unused;
158 if (error == NULL)
159 {
160 error= &unused;
161 }
162
163 if (ptr == NULL)
164 {
165 *error= MEMCACHED_INVALID_ARGUMENTS;
166 return NULL;
167 }
168
169 if (memcached_is_udp(ptr))
170 {
171 *error= MEMCACHED_NOT_SUPPORTED;
172 return NULL;
173 }
174
175 if (result == NULL)
176 {
177 // If we have already initialized (ie it is in use) our internal, we
178 // create one.
179 if (memcached_is_initialized(&ptr->result))
180 {
181 if ((result= memcached_result_create(ptr, NULL)) == NULL)
182 {
183 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
184 return NULL;
185 }
186 }
187 else
188 {
189 result= memcached_result_create(ptr, &ptr->result);
190 }
191 }
192
193 *error= MEMCACHED_MAXIMUM_RETURN; // We use this to see if we ever go into the loop
194 memcached_server_st *server;
195 while ((server= memcached_io_get_readable_server(ptr)))
196 {
197 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
198 *error= memcached_response(server, buffer, sizeof(buffer), result);
199
200 if (*error == MEMCACHED_IN_PROGRESS)
201 {
202 continue;
203 }
204 else if (*error == MEMCACHED_SUCCESS)
205 {
206 result->count++;
207 return result;
208 }
209 else if (*error == MEMCACHED_END)
210 {
211 memcached_server_response_reset(server);
212 }
213 else if (*error != MEMCACHED_NOTFOUND)
214 {
215 break;
216 }
217 }
218
219 if (*error == MEMCACHED_NOTFOUND and result->count)
220 {
221 *error= MEMCACHED_END;
222 }
223 else if (*error == MEMCACHED_MAXIMUM_RETURN and result->count)
224 {
225 *error= MEMCACHED_END;
226 }
227 else if (*error == MEMCACHED_MAXIMUM_RETURN) // while() loop was never entered
228 {
229 *error= MEMCACHED_NOTFOUND;
230 }
231 else if (*error == MEMCACHED_SUCCESS)
232 {
233 *error= MEMCACHED_END;
234 }
235 else if (result->count == 0)
236 {
237 *error= MEMCACHED_NOTFOUND;
238 }
239
240 /* We have completed reading data */
241 if (memcached_is_allocated(result))
242 {
243 memcached_result_free(result);
244 }
245 else
246 {
247 result->count= 0;
248 memcached_string_reset(&result->value);
249 }
250
251 return NULL;
252 }
253
254 memcached_return_t memcached_fetch_execute(memcached_st *ptr,
255 memcached_execute_fn *callback,
256 void *context,
257 uint32_t number_of_callbacks)
258 {
259 memcached_result_st *result= &ptr->result;
260 memcached_return_t rc;
261 bool some_errors= false;
262
263 while ((result= memcached_fetch_result(ptr, result, &rc)))
264 {
265 if (memcached_failed(rc) and rc == MEMCACHED_NOTFOUND)
266 {
267 continue;
268 }
269 else if (memcached_failed(rc))
270 {
271 memcached_set_error(*ptr, rc, MEMCACHED_AT);
272 some_errors= true;
273 continue;
274 }
275
276 for (uint32_t x= 0; x < number_of_callbacks; x++)
277 {
278 memcached_return_t ret= (*callback[x])(ptr, result, context);
279 if (memcached_failed(ret))
280 {
281 some_errors= true;
282 memcached_set_error(*ptr, ret, MEMCACHED_AT);
283 break;
284 }
285 }
286 }
287
288 if (some_errors)
289 {
290 return MEMCACHED_SOME_ERRORS;
291 }
292
293 // If we were able to run all keys without issue we return
294 // MEMCACHED_SUCCESS
295 if (memcached_success(rc))
296 {
297 return MEMCACHED_SUCCESS;
298 }
299
300 return rc;
301 }