1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 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
*ptr
, char *key
, size_t *key_length
,
43 memcached_return_t
*error
)
45 memcached_result_st
*result_buffer
= &ptr
->result
;
46 memcached_return_t unused
;
51 unlikely (ptr
->flags
.use_udp
)
65 *error
= MEMCACHED_NOT_SUPPORTED
;
69 result_buffer
= memcached_fetch_result(ptr
, result_buffer
, error
);
70 if (result_buffer
== NULL
or memcached_failed(*error
))
72 WATCHPOINT_ASSERT(result_buffer
== NULL
);
90 *value_length
= memcached_string_length(&result_buffer
->value
);
95 if (result_buffer
->key_length
> MEMCACHED_MAX_KEY
)
97 *error
= MEMCACHED_KEY_TOO_BIG
;
112 strncpy(key
, result_buffer
->item_key
, result_buffer
->key_length
); // For the binary protocol we will cut off the key :(
114 *key_length
= result_buffer
->key_length
;
118 *flags
= result_buffer
->item_flags
;
120 return memcached_string_take_value(&result_buffer
->value
);
123 memcached_result_st
*memcached_fetch_result(memcached_st
*ptr
,
124 memcached_result_st
*result
,
125 memcached_return_t
*error
)
127 memcached_return_t unused
;
133 *error
= MEMCACHED_INVALID_ARGUMENTS
;
137 if (ptr
->flags
.use_udp
)
139 *error
= MEMCACHED_NOT_SUPPORTED
;
145 // If we have already initialized (ie it is in use) our internal, we
147 if (memcached_is_initialized(&ptr
->result
))
149 if (not (result
= memcached_result_create(ptr
, NULL
)))
151 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
157 result
= memcached_result_create(ptr
, &ptr
->result
);
161 *error
= MEMCACHED_MAXIMUM_RETURN
; // We use this to see if we ever go into the loop
162 memcached_server_st
*server
;
163 while ((server
= memcached_io_get_readable_server(ptr
)))
165 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
166 *error
= memcached_response(server
, buffer
, sizeof(buffer
), result
);
168 if (*error
== MEMCACHED_IN_PROGRESS
)
172 else if (*error
== MEMCACHED_SUCCESS
)
177 else if (*error
== MEMCACHED_END
)
179 memcached_server_response_reset(server
);
181 else if (*error
!= MEMCACHED_NOTFOUND
)
187 if (*error
== MEMCACHED_NOTFOUND
and result
->count
)
189 *error
= MEMCACHED_END
;
191 else if (*error
== MEMCACHED_MAXIMUM_RETURN
and result
->count
)
193 *error
= MEMCACHED_END
;
195 else if (*error
== MEMCACHED_MAXIMUM_RETURN
) // while() loop was never entered
197 *error
= MEMCACHED_NOTFOUND
;
199 else if (*error
== MEMCACHED_SUCCESS
)
201 *error
= MEMCACHED_END
;
203 else if (result
->count
== 0)
205 *error
= MEMCACHED_NOTFOUND
;
208 /* We have completed reading data */
209 if (memcached_is_allocated(result
))
211 memcached_result_free(result
);
216 memcached_string_reset(&result
->value
);
222 memcached_return_t
memcached_fetch_execute(memcached_st
*ptr
,
223 memcached_execute_fn
*callback
,
225 uint32_t number_of_callbacks
)
227 memcached_result_st
*result
= &ptr
->result
;
228 memcached_return_t rc
;
229 bool some_errors
= false;
231 while ((result
= memcached_fetch_result(ptr
, result
, &rc
)))
233 if (memcached_failed(rc
) and rc
== MEMCACHED_NOTFOUND
)
237 else if (memcached_failed(rc
))
239 memcached_set_error(*ptr
, rc
, MEMCACHED_AT
);
244 for (uint32_t x
= 0; x
< number_of_callbacks
; x
++)
246 memcached_return_t ret
= (*callback
[x
])(ptr
, result
, context
);
247 if (memcached_failed(ret
))
250 memcached_set_error(*ptr
, ret
, MEMCACHED_AT
);
258 return MEMCACHED_SOME_ERRORS
;
261 // If we were able to run all keys without issue we return
263 if (memcached_success(rc
))
265 return MEMCACHED_SUCCESS
;