Readjusted a number function names.
[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
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 char *value;
80
81 /* We add two bytes so that we can walk the \r\n */
82 value= (char *)malloc(((*value_length) +2) * sizeof(char));
83
84 if (!value)
85 {
86 *value_length= 0;
87 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
88 return NULL;
89 }
90
91 read_length= read(ptr->hosts[server_key].fd, value, (*value_length)+2);
92
93 if (read_length != (size_t)(*value_length + 2))
94 {
95 free(value);
96 goto read_error;
97 }
98
99 return value;
100 }
101 }
102
103 return NULL;
104 read_error:
105 *error= MEMCACHED_PARTIAL_READ;
106 return NULL;
107 }
108
109 char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
110 size_t *value_length,
111 uint16_t *flags,
112 memcached_return *error)
113 {
114 size_t send_length;
115 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
116 unsigned int server_key;
117
118 *value_length= 0;
119 *error= memcached_connect(ptr);
120
121 if (*error != MEMCACHED_SUCCESS)
122 return NULL;
123
124 server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts;
125
126 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n",
127 (int)key_length, key);
128 if (*error != MEMCACHED_SUCCESS)
129 return NULL;
130
131 if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1))
132 {
133 *error= MEMCACHED_WRITE_FAILURE;
134 return NULL;
135 }
136
137 return memcached_value_fetch(ptr, key, &key_length, value_length, flags,
138 error, 0, server_key);
139 }
140
141 memcached_return memcached_mget(memcached_st *ptr,
142 char **keys, size_t *key_length,
143 unsigned int number_of_keys)
144 {
145 char buffer[HUGE_STRING_LEN];
146 unsigned int x;
147 memcached_return rc;
148 memcached_string_st **cursor_key_exec;
149
150 ptr->cursor_server= 0;
151 memset(buffer, 0, HUGE_STRING_LEN);
152
153 rc= memcached_connect(ptr);
154
155 if (rc != MEMCACHED_SUCCESS)
156 return rc;
157
158 cursor_key_exec= (memcached_string_st **)malloc(sizeof(memcached_string_st *) * ptr->number_of_hosts);
159 memset(cursor_key_exec, 0, sizeof(memcached_string_st *) * ptr->number_of_hosts);
160
161
162 for (x= 0; x < number_of_keys; x++)
163 {
164 unsigned int server_key;
165
166 server_key= memcached_generate_hash(keys[x], key_length[x]) % ptr->number_of_hosts;
167
168 if (cursor_key_exec[server_key])
169 {
170 memcached_string_st *string= cursor_key_exec[server_key];
171
172 memcached_string_append_character(ptr, string, ' ');
173 memcached_string_append(ptr, string, keys[x], key_length[x]);
174 }
175 else
176 {
177 memcached_string_st *string= memcached_string_init(ptr, SMALL_STRING_LEN);
178
179 /* We need to figure out the correct way to error in case of this failure */
180 if (!string)
181 assert(0);
182
183 memcached_string_append(ptr, string, "get ", 4);
184 memcached_string_append(ptr, string, keys[x], key_length[x]);
185
186 cursor_key_exec[server_key]= string;
187 }
188 }
189
190
191 /*
192 Should we muddle on if some servers are dead?
193 */
194 for (x= 0; x < ptr->number_of_hosts; x++)
195 {
196 if (cursor_key_exec[x])
197 {
198 memcached_string_st *string= cursor_key_exec[x];
199 memcached_string_append(ptr, string, "\r\n", 2);
200
201 if ((write(ptr->hosts[x].fd, string->string,
202 memcached_string_length(ptr, string)) == -1))
203 {
204 memcached_quit(ptr);
205 rc= MEMCACHED_SOME_ERRORS;
206 }
207 memcached_string_free(ptr, string);
208 cursor_key_exec[x]= NULL; /* Remove warning */
209 }
210 }
211
212 free(cursor_key_exec);
213
214 return rc;
215 }
216
217 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
218 size_t *value_length,
219 uint16_t *flags,
220 memcached_return *error)
221 {
222 char *value_check;
223
224 while (ptr->cursor_server < ptr->number_of_hosts)
225 {
226 value_check= memcached_value_fetch(ptr, key, key_length, value_length, flags,
227 error, 1, ptr->cursor_server);
228
229 if (*error == MEMCACHED_NOTFOUND)
230 ptr->cursor_server++;
231 else if (*error != MEMCACHED_SUCCESS)
232 return NULL;
233 else
234 return value_check;
235 }
236
237 return NULL;
238 }