Added/restructured all additional hostname information
[awesomized/libmemcached] / lib / memcached_get.c
1 #include "common.h"
2
3 static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_length,
4 size_t *value_length,
5 uint16_t *flags,
6 memcached_return *error,
7 char load_key,
8 unsigned int server_key)
9 {
10 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
11 char *string_ptr;
12 char *end_ptr;
13
14 assert(value_length);
15 assert(flags);
16 assert(error);
17
18 memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
19 end_ptr= buffer + MEMCACHED_DEFAULT_COMMAND_SIZE;
20
21 *value_length= 0;
22
23 *error= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
24
25 if (*error == MEMCACHED_SUCCESS)
26 {
27 char *next_ptr;
28
29 string_ptr= buffer;
30 string_ptr+= 6; /* "VALUE " */
31
32 /* We load the key */
33 if (load_key)
34 {
35 memset(key, 0, MEMCACHED_MAX_KEY);
36 for (; end_ptr == string_ptr || *string_ptr != ' '; string_ptr++)
37 {
38 *key= *string_ptr;
39 key++;
40 }
41 }
42 else /* Skip characters */
43 for (; end_ptr == string_ptr || *string_ptr != ' '; string_ptr++);
44
45 if (end_ptr == string_ptr)
46 goto read_error;
47
48 /* Flags fetch move past space */
49 string_ptr++;
50 if (end_ptr == string_ptr)
51 goto read_error;
52 for (next_ptr= string_ptr; end_ptr == string_ptr || *string_ptr != ' '; string_ptr++);
53 *flags= (uint16_t)strtol(next_ptr, &string_ptr, 10);
54
55 if (end_ptr == string_ptr)
56 goto read_error;
57
58 /* Length fetch move past space*/
59 string_ptr++;
60 if (end_ptr == string_ptr)
61 goto read_error;
62
63 for (next_ptr= string_ptr; end_ptr == string_ptr || *string_ptr != ' '; string_ptr++);
64 *value_length= (size_t)strtoll(next_ptr, &string_ptr, 10);
65
66 if (end_ptr == string_ptr)
67 goto read_error;
68
69 /* Skip past the \r\n */
70 string_ptr+= 2;
71
72 if (end_ptr < string_ptr)
73 goto read_error;
74
75 if (*value_length)
76 {
77 size_t read_length;
78 size_t partial_length;
79 size_t to_read;
80 char *value;
81 char *value_ptr;
82
83 /* We add two bytes so that we can walk the \r\n */
84 value= (char *)malloc(((*value_length) +2) * sizeof(char));
85 memset(value, 0, ((*value_length) +2) * sizeof(char));
86
87 if (!value)
88 {
89 *value_length= 0;
90 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
91 return NULL;
92 }
93
94 value_ptr= value;
95 read_length= 0;
96 to_read= (*value_length) + 2;
97 /* This is overkill */
98 while ((partial_length= recv(ptr->hosts[server_key].fd, value_ptr, to_read, 0)) > 0)
99 {
100 value_ptr+= partial_length;
101 read_length+= partial_length;
102 to_read-= partial_length;
103 if (read_length == (size_t)(*value_length + 2))
104 break;
105 }
106
107 if (read_length != (size_t)(*value_length + 2))
108 {
109 free(value);
110 goto read_error;
111 }
112
113 return value;
114 }
115 }
116 else if (*error == MEMCACHED_END)
117 *error= MEMCACHED_NOTFOUND;
118
119 return NULL;
120 read_error:
121 *error= MEMCACHED_PARTIAL_READ;
122 return NULL;
123 }
124
125 char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
126 size_t *value_length,
127 uint16_t *flags,
128 memcached_return *error)
129 {
130 size_t send_length;
131 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
132 unsigned int server_key;
133 char *value;
134 LIBMEMCACHED_MEMCACHED_GET_START();
135
136 *value_length= 0;
137 *error= memcached_connect(ptr);
138
139 if (*error != MEMCACHED_SUCCESS)
140 goto error;
141
142 server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts;
143
144 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n",
145 (int)key_length, key);
146 if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
147 {
148 *error= MEMCACHED_WRITE_FAILURE;
149 goto error;
150 }
151
152 value= memcached_value_fetch(ptr, key, &key_length, value_length, flags,
153 error, 0, server_key);
154 if (*error == MEMCACHED_END && *value_length == 0)
155 {
156 *error= MEMCACHED_NOTFOUND;
157 goto error;
158 }
159 else if (*error == MEMCACHED_SUCCESS)
160 {
161 memcached_return rc;
162 /* We need to read END */
163 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
164
165 if (rc != MEMCACHED_END)
166 {
167 *error= MEMCACHED_PROTOCOL_ERROR;
168 goto error;
169 }
170 }
171 else
172 goto error;
173
174 LIBMEMCACHED_MEMCACHED_GET_END();
175
176 return value;
177
178 error:
179 free(value);
180 *value_length= 0;
181
182 LIBMEMCACHED_MEMCACHED_GET_END();
183
184 return NULL;
185 }
186
187 memcached_return memcached_mget(memcached_st *ptr,
188 char **keys, size_t *key_length,
189 unsigned int number_of_keys)
190 {
191 char buffer[HUGE_STRING_LEN];
192 unsigned int x;
193 memcached_return rc;
194 memcached_string_st **cursor_key_exec;
195 LIBMEMCACHED_MEMCACHED_MGET_START();
196
197 ptr->cursor_server= 0;
198 memset(buffer, 0, HUGE_STRING_LEN);
199
200 rc= memcached_connect(ptr);
201
202 if (rc != MEMCACHED_SUCCESS)
203 return rc;
204
205 cursor_key_exec= (memcached_string_st **)malloc(sizeof(memcached_string_st *) * ptr->number_of_hosts);
206 memset(cursor_key_exec, 0, sizeof(memcached_string_st *) * ptr->number_of_hosts);
207
208
209 for (x= 0; x < number_of_keys; x++)
210 {
211 unsigned int server_key;
212
213 server_key= memcached_generate_hash(keys[x], key_length[x]) % ptr->number_of_hosts;
214
215 if (cursor_key_exec[server_key])
216 {
217 memcached_string_st *string= cursor_key_exec[server_key];
218
219 memcached_string_append_character(ptr, string, ' ');
220 memcached_string_append(ptr, string, keys[x], key_length[x]);
221 }
222 else
223 {
224 memcached_string_st *string= memcached_string_init(ptr, SMALL_STRING_LEN);
225
226 /* We need to figure out the correct way to error in case of this failure */
227 if (!string)
228 assert(0);
229
230 memcached_string_append(ptr, string, "get ", 4);
231 memcached_string_append(ptr, string, keys[x], key_length[x]);
232
233 cursor_key_exec[server_key]= string;
234 }
235 }
236
237
238 /*
239 Should we muddle on if some servers are dead?
240 */
241 for (x= 0; x < ptr->number_of_hosts; x++)
242 {
243 if (cursor_key_exec[x])
244 {
245 memcached_string_st *string= cursor_key_exec[x];
246 memcached_string_append(ptr, string, "\r\n", 2);
247
248 if ((send(ptr->hosts[x].fd, string->string,
249 memcached_string_length(ptr, string), 0) == -1))
250 {
251 memcached_quit(ptr);
252 rc= MEMCACHED_SOME_ERRORS;
253 }
254 memcached_string_free(ptr, string);
255 cursor_key_exec[x]= NULL; /* Remove warning */
256 }
257 }
258
259 free(cursor_key_exec);
260
261 LIBMEMCACHED_MEMCACHED_MGET_END();
262 return rc;
263 }
264
265 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
266 size_t *value_length,
267 uint16_t *flags,
268 memcached_return *error)
269 {
270 char *value_check;
271
272 while (ptr->cursor_server < ptr->number_of_hosts)
273 {
274 value_check= memcached_value_fetch(ptr, key, key_length, value_length, flags,
275 error, 1, ptr->cursor_server);
276
277 if (*error == MEMCACHED_NOTFOUND)
278 ptr->cursor_server++;
279 else if (*error != MEMCACHED_SUCCESS)
280 return NULL;
281 else
282 return value_check;
283 }
284
285 return NULL;
286 }