4d2b10d2edad397ae2efd18bba7a25d1a81b9bed
[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 if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
141 {
142 *error= MEMCACHED_WRITE_FAILURE;
143 goto error;
144 }
145
146 value= memcached_value_fetch(ptr, key, &key_length, value_length, flags,
147 error, 0, server_key);
148 if (*error == MEMCACHED_END && *value_length == 0)
149 {
150 *error= MEMCACHED_NOTFOUND;
151 goto error;
152 }
153 else if (*error == MEMCACHED_SUCCESS)
154 {
155 memcached_return rc;
156 /* We need to read END */
157 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
158
159 if (rc != MEMCACHED_END)
160 {
161 *error= MEMCACHED_PROTOCOL_ERROR;
162 goto error;
163 }
164 }
165 else
166 goto error;
167
168 LIBMEMCACHED_MEMCACHED_GET_END();
169
170 return value;
171
172 error:
173 free(value);
174 *value_length= 0;
175
176 LIBMEMCACHED_MEMCACHED_GET_END();
177
178 return NULL;
179 }
180
181 memcached_return memcached_mget(memcached_st *ptr,
182 char **keys, size_t *key_length,
183 unsigned int number_of_keys)
184 {
185 char buffer[HUGE_STRING_LEN];
186 unsigned int x;
187 memcached_return rc;
188 memcached_string_st **cursor_key_exec;
189 LIBMEMCACHED_MEMCACHED_MGET_START();
190
191 ptr->cursor_server= 0;
192 memset(buffer, 0, HUGE_STRING_LEN);
193
194 rc= memcached_connect(ptr);
195
196 if (rc != MEMCACHED_SUCCESS)
197 return rc;
198
199 cursor_key_exec= (memcached_string_st **)malloc(sizeof(memcached_string_st *) * ptr->number_of_hosts);
200 memset(cursor_key_exec, 0, sizeof(memcached_string_st *) * ptr->number_of_hosts);
201
202
203 for (x= 0; x < number_of_keys; x++)
204 {
205 unsigned int server_key;
206
207 server_key= memcached_generate_hash(keys[x], key_length[x]) % ptr->number_of_hosts;
208
209 if (cursor_key_exec[server_key])
210 {
211 memcached_string_st *string= cursor_key_exec[server_key];
212
213 memcached_string_append_character(ptr, string, ' ');
214 memcached_string_append(ptr, string, keys[x], key_length[x]);
215 }
216 else
217 {
218 memcached_string_st *string= memcached_string_create(ptr, SMALL_STRING_LEN);
219
220 /* We need to figure out the correct way to error in case of this failure */
221 if (!string)
222 assert(0);
223
224 memcached_string_append(ptr, string, "get ", 4);
225 memcached_string_append(ptr, string, keys[x], key_length[x]);
226
227 cursor_key_exec[server_key]= string;
228 }
229 }
230
231
232 /*
233 Should we muddle on if some servers are dead?
234 */
235 for (x= 0; x < ptr->number_of_hosts; x++)
236 {
237 if (cursor_key_exec[x])
238 {
239 memcached_string_st *string= cursor_key_exec[x];
240 memcached_string_append(ptr, string, "\r\n", 2);
241
242 if ((send(ptr->hosts[x].fd, string->string,
243 memcached_string_length(ptr, string), 0) == -1))
244 {
245 memcached_quit(ptr);
246 rc= MEMCACHED_SOME_ERRORS;
247 }
248 memcached_string_free(ptr, string);
249 cursor_key_exec[x]= NULL; /* Remove warning */
250 }
251 }
252
253 free(cursor_key_exec);
254
255 LIBMEMCACHED_MEMCACHED_MGET_END();
256 return rc;
257 }
258
259 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
260 size_t *value_length,
261 uint16_t *flags,
262 memcached_return *error)
263 {
264 char *value_check;
265
266 while (ptr->cursor_server < ptr->number_of_hosts)
267 {
268 value_check= memcached_value_fetch(ptr, key, key_length, value_length, flags,
269 error, 1, ptr->cursor_server);
270
271 if (*error == MEMCACHED_NOTFOUND)
272 ptr->cursor_server++;
273 else if (*error != MEMCACHED_SUCCESS)
274 return NULL;
275 else
276 return value_check;
277 }
278
279 return NULL;
280 }