Lossen up restrictions on the recv().
[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 (not error)
48 error= &unused;
49
50
51 unlikely (ptr->flags.use_udp)
52 {
53 *error= MEMCACHED_NOT_SUPPORTED;
54 return NULL;
55 }
56
57 result_buffer= memcached_fetch_result(ptr, result_buffer, error);
58
59 if (result_buffer == NULL or *error != MEMCACHED_SUCCESS)
60 {
61 WATCHPOINT_ASSERT(result_buffer == NULL);
62 *value_length= 0;
63 return NULL;
64 }
65
66 *value_length= memcached_string_length(&result_buffer->value);
67
68 if (key)
69 {
70 if (result_buffer->key_length > MEMCACHED_MAX_KEY)
71 {
72 *error= MEMCACHED_KEY_TOO_BIG;
73 *value_length= 0;
74
75 return NULL;
76 }
77 strncpy(key, result_buffer->item_key, result_buffer->key_length); // For the binary protocol we will cut off the key :(
78 *key_length= result_buffer->key_length;
79 }
80
81 if (flags)
82 *flags= result_buffer->item_flags;
83
84 return memcached_string_c_copy(&result_buffer->value);
85 }
86
87 memcached_result_st *memcached_fetch_result(memcached_st *ptr,
88 memcached_result_st *result,
89 memcached_return_t *error)
90 {
91 memcached_server_st *server;
92
93 memcached_return_t unused;
94 if (not error)
95 error= &unused;
96
97 if (ptr->flags.use_udp)
98 {
99 *error= MEMCACHED_NOT_SUPPORTED;
100 return NULL;
101 }
102
103 if (not result)
104 {
105 if (not (result= memcached_result_create(ptr, NULL)))
106 {
107 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
108 return NULL;
109 }
110 }
111
112 while ((server= memcached_io_get_readable_server(ptr)))
113 {
114 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
115 *error= memcached_response(server, buffer, sizeof(buffer), result);
116
117 if (*error == MEMCACHED_SUCCESS)
118 {
119 return result;
120 }
121 else if (*error == MEMCACHED_END)
122 {
123 memcached_server_response_reset(server);
124 }
125 else if (*error != MEMCACHED_NOTFOUND)
126 {
127 break;
128 }
129 }
130
131 /* We have completed reading data */
132 if (memcached_is_allocated(result))
133 {
134 memcached_result_free(result);
135 }
136 else
137 {
138 memcached_string_reset(&result->value);
139 }
140
141 if (*error == MEMCACHED_NOTFOUND)
142 *error= MEMCACHED_END;
143
144 if (*error == MEMCACHED_SUCCESS)
145 *error= MEMCACHED_END;
146
147 return NULL;
148 }
149
150 memcached_return_t memcached_fetch_execute(memcached_st *ptr,
151 memcached_execute_fn *callback,
152 void *context,
153 uint32_t number_of_callbacks)
154 {
155 memcached_result_st *result= &ptr->result;
156 memcached_return_t rc= MEMCACHED_FAILURE;
157
158 while ((result= memcached_fetch_result(ptr, result, &rc)))
159 {
160 if (memcached_success(rc))
161 {
162 for (uint32_t x= 0; x < number_of_callbacks; x++)
163 {
164 rc= (*callback[x])(ptr, result, context);
165 if (memcached_failed(rc))
166 break;
167 }
168 }
169 }
170 return rc;
171 }