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