1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
40 char *memcached_fetch(memcached_st
*shell
, char *key
, size_t *key_length
,
43 memcached_return_t
*error
)
45 Memcached
* ptr
= memcached2Memcached(shell
);
46 memcached_return_t unused
;
52 if (memcached_is_udp(ptr
))
74 *error
= MEMCACHED_NOT_SUPPORTED
;
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
))
82 WATCHPOINT_ASSERT(result_buffer
== NULL
);
108 *value_length
= memcached_string_length(&result_buffer
->value
);
113 if (result_buffer
->key_length
> MEMCACHED_MAX_KEY
)
115 *error
= MEMCACHED_KEY_TOO_BIG
;
139 strncpy(key
, result_buffer
->item_key
, result_buffer
->key_length
); // For the binary protocol we will cut off the key :(
142 *key_length
= result_buffer
->key_length
;
148 *flags
= result_buffer
->item_flags
;
151 return memcached_string_take_value(&result_buffer
->value
);
154 memcached_result_st
*memcached_fetch_result(memcached_st
*ptr
,
155 memcached_result_st
*result
,
156 memcached_return_t
*error
)
158 memcached_return_t unused
;
166 *error
= MEMCACHED_INVALID_ARGUMENTS
;
170 if (memcached_is_udp(ptr
))
172 *error
= MEMCACHED_NOT_SUPPORTED
;
178 // If we have already initialized (ie it is in use) our internal, we
180 if (memcached_is_initialized(&ptr
->result
))
182 if ((result
= memcached_result_create(ptr
, NULL
)) == NULL
)
184 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
190 result
= memcached_result_create(ptr
, &ptr
->result
);
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
)))
200 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
201 *error
= memcached_response(server
, buffer
, sizeof(buffer
), result
);
203 if (*error
== MEMCACHED_IN_PROGRESS
)
207 else if (*error
== MEMCACHED_CONNECTION_FAILURE
)
209 connection_failures
= true;
212 else if (*error
== MEMCACHED_SUCCESS
)
217 else if (*error
== MEMCACHED_END
)
219 memcached_server_response_reset(server
);
221 else if (*error
!= MEMCACHED_NOTFOUND
)
227 if (*error
== MEMCACHED_NOTFOUND
and result
->count
)
229 *error
= MEMCACHED_END
;
231 else if (*error
== MEMCACHED_MAXIMUM_RETURN
and result
->count
)
233 *error
= MEMCACHED_END
;
235 else if (*error
== MEMCACHED_MAXIMUM_RETURN
) // while() loop was never entered
237 *error
= MEMCACHED_NOTFOUND
;
239 else if (connection_failures
)
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
247 *error
= MEMCACHED_CONNECTION_FAILURE
;
249 else if (*error
== MEMCACHED_SUCCESS
)
251 *error
= MEMCACHED_END
;
253 else if (result
->count
== 0)
255 *error
= MEMCACHED_NOTFOUND
;
258 /* We have completed reading data */
259 if (memcached_is_allocated(result
))
261 memcached_result_free(result
);
266 memcached_string_reset(&result
->value
);
272 memcached_return_t
memcached_fetch_execute(memcached_st
*shell
,
273 memcached_execute_fn
*callback
,
275 uint32_t number_of_callbacks
)
277 Memcached
* ptr
= memcached2Memcached(shell
);
278 memcached_result_st
*result
= &ptr
->result
;
279 memcached_return_t rc
;
280 bool some_errors
= false;
282 while ((result
= memcached_fetch_result(ptr
, result
, &rc
)))
284 if (memcached_failed(rc
) and rc
== MEMCACHED_NOTFOUND
)
288 else if (memcached_failed(rc
))
290 memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
295 for (uint32_t x
= 0; x
< number_of_callbacks
; x
++)
297 memcached_return_t ret
= (*callback
[x
])(ptr
, result
, context
);
298 if (memcached_failed(ret
))
301 memcached_set_error(*ptr
, ret
, MEMCACHED_AT
);
309 return MEMCACHED_SOME_ERRORS
;
312 // If we were able to run all keys without issue we return
314 if (memcached_success(rc
))
316 return MEMCACHED_SUCCESS
;