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