Merge up trunk
[awesomized/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 (not error)
48 error= &unused;
49
50
51 unlikely (ptr->flags.use_udp)
52 {
53 if (value_length)
54 *value_length= 0;
55
56 if (key_length)
57 *key_length= 0;
58
59 if (flags)
60 *flags= 0;
61
62 if (key)
63 *key= 0;
64
65 *error= MEMCACHED_NOT_SUPPORTED;
66 return NULL;
67 }
68
69 result_buffer= memcached_fetch_result(ptr, result_buffer, error);
70 if (result_buffer == NULL or memcached_failed(*error))
71 {
72 WATCHPOINT_ASSERT(result_buffer == NULL);
73 if (value_length)
74 *value_length= 0;
75
76 if (key_length)
77 *key_length= 0;
78
79 if (flags)
80 *flags= 0;
81
82 if (key)
83 *key= 0;
84
85 return NULL;
86 }
87
88 if (value_length)
89 *value_length= memcached_string_length(&result_buffer->value);
90
91 if (key)
92 {
93 if (result_buffer->key_length > MEMCACHED_MAX_KEY)
94 {
95 *error= MEMCACHED_KEY_TOO_BIG;
96 if (value_length)
97 *value_length= 0;
98
99 if (key_length)
100 *key_length= 0;
101
102 if (flags)
103 *flags= 0;
104
105 if (key)
106 *key= 0;
107
108 return NULL;
109 }
110 strncpy(key, result_buffer->item_key, result_buffer->key_length); // For the binary protocol we will cut off the key :(
111 if (key_length)
112 *key_length= result_buffer->key_length;
113 }
114
115 if (flags)
116 *flags= result_buffer->item_flags;
117
118 return memcached_string_take_value(&result_buffer->value);
119 }
120
121 memcached_result_st *memcached_fetch_result(memcached_st *ptr,
122 memcached_result_st *result,
123 memcached_return_t *error)
124 {
125 memcached_return_t unused;
126 if (not error)
127 error= &unused;
128
129 if (not ptr)
130 {
131 *error= MEMCACHED_INVALID_ARGUMENTS;
132 return NULL;
133 }
134
135 if (ptr->flags.use_udp)
136 {
137 *error= MEMCACHED_NOT_SUPPORTED;
138 return NULL;
139 }
140
141 if (not result)
142 {
143 // If we have already initialized (ie it is in use) our internal, we
144 // create one.
145 if (memcached_is_initialized(&ptr->result))
146 {
147 if (not (result= memcached_result_create(ptr, NULL)))
148 {
149 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
150 return NULL;
151 }
152 }
153 else
154 {
155 result= memcached_result_create(ptr, &ptr->result);
156 }
157 }
158
159 *error= MEMCACHED_MAXIMUM_RETURN; // We use this to see if we ever go into the loop
160 memcached_server_st *server;
161 while ((server= memcached_io_get_readable_server(ptr)))
162 {
163 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
164 *error= memcached_response(server, buffer, sizeof(buffer), result);
165
166 if (*error == MEMCACHED_IN_PROGRESS)
167 {
168 continue;
169 }
170 else if (*error == MEMCACHED_SUCCESS)
171 {
172 result->count++;
173 return result;
174 }
175 else if (*error == MEMCACHED_END)
176 {
177 memcached_server_response_reset(server);
178 }
179 else if (*error != MEMCACHED_NOTFOUND)
180 {
181 break;
182 }
183 }
184
185 if (*error == MEMCACHED_NOTFOUND and result->count)
186 {
187 *error= MEMCACHED_END;
188 }
189 else if (*error == MEMCACHED_MAXIMUM_RETURN and result->count)
190 {
191 *error= MEMCACHED_END;
192 }
193 else if (*error == MEMCACHED_MAXIMUM_RETURN) // while() loop was never entered
194 {
195 *error= MEMCACHED_NOTFOUND;
196 }
197 else if (*error == MEMCACHED_SUCCESS)
198 {
199 *error= MEMCACHED_END;
200 }
201 else if (result->count == 0)
202 {
203 *error= MEMCACHED_NOTFOUND;
204 }
205
206 /* We have completed reading data */
207 if (memcached_is_allocated(result))
208 {
209 memcached_result_free(result);
210 }
211 else
212 {
213 result->count= 0;
214 memcached_string_reset(&result->value);
215 }
216
217 return NULL;
218 }
219
220 memcached_return_t memcached_fetch_execute(memcached_st *ptr,
221 memcached_execute_fn *callback,
222 void *context,
223 uint32_t number_of_callbacks)
224 {
225 memcached_result_st *result= &ptr->result;
226 memcached_return_t rc;
227 bool some_errors= false;
228
229 while ((result= memcached_fetch_result(ptr, result, &rc)))
230 {
231 if (memcached_failed(rc) and rc == MEMCACHED_NOTFOUND)
232 {
233 continue;
234 }
235 else if (memcached_failed(rc))
236 {
237 memcached_set_error(*ptr, rc, MEMCACHED_AT);
238 some_errors= true;
239 continue;
240 }
241
242 for (uint32_t x= 0; x < number_of_callbacks; x++)
243 {
244 memcached_return_t ret= (*callback[x])(ptr, result, context);
245 if (memcached_failed(ret))
246 {
247 some_errors= true;
248 memcached_set_error(*ptr, ret, MEMCACHED_AT);
249 break;
250 }
251 }
252 }
253
254 if (some_errors)
255 {
256 return MEMCACHED_SOME_ERRORS;
257 }
258
259 // If we were able to run all keys without issue we return
260 // MEMCACHED_SUCCESS
261 if (memcached_success(rc))
262 {
263 return MEMCACHED_SUCCESS;
264 }
265
266 return rc;
267 }