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