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