NEw string type.
[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
13 *value_length= 0;
14
15 memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
16 *error= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
17
18 if (*error == MEMCACHED_SUCCESS)
19 {
20 char *end_ptr;
21
22 string_ptr= buffer;
23 string_ptr+= 6; /* "VALUE " */
24
25 /* We load the key */
26 if (load_key)
27 {
28 memset(key, 0, MEMCACHED_MAX_KEY);
29 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++)
30 {
31 *key= *end_ptr;
32 key++;
33 }
34 }
35 else /* Skip characters */
36 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
37
38 /* Flags fetch */
39 string_ptr= end_ptr + 1;
40 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
41 *flags= (uint16_t)strtol(string_ptr, &end_ptr, 10);
42
43 /* Length fetch */
44 string_ptr= end_ptr + 1;
45 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
46 *value_length= strtoll(string_ptr, &end_ptr, 10);
47
48 /* Skip past the \r\n */
49 string_ptr= end_ptr +2;
50
51 if (*value_length)
52 {
53 size_t read_length;
54 char *value;
55
56 /* We add two bytes so that we can walk the \r\n */
57 value= (char *)malloc(((*value_length) +2) * sizeof(char));
58
59 if (!value)
60 {
61 *value_length= 0;
62 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
63 return NULL;
64 }
65
66 read_length= read(ptr->hosts[server_key].fd, value, (*value_length)+2);
67
68 if ((read_length -2) != *value_length)
69 {
70 free(value);
71 *error= MEMCACHED_PARTIAL_READ;
72
73 return NULL;
74 }
75
76 return value;
77 }
78 }
79
80 return NULL;
81 }
82
83 char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
84 size_t *value_length,
85 uint16_t *flags,
86 memcached_return *error)
87 {
88 size_t send_length;
89 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
90 unsigned int server_key;
91
92 *value_length= 0;
93 *error= memcached_connect(ptr);
94
95 if (*error != MEMCACHED_SUCCESS)
96 return NULL;
97
98 server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts;
99
100 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n",
101 (int)key_length, key);
102 if (*error != MEMCACHED_SUCCESS)
103 return NULL;
104
105 if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1))
106 {
107 *error= MEMCACHED_WRITE_FAILURE;
108 return NULL;
109 }
110
111 return memcached_value_fetch(ptr, key, &key_length, value_length, flags,
112 error, 0, server_key);
113 }
114
115 memcached_return memcached_mget(memcached_st *ptr,
116 char **keys, size_t *key_length,
117 unsigned int number_of_keys)
118 {
119 char buffer[HUGE_STRING_LEN];
120 unsigned int x;
121 memcached_return rc;
122 memcached_string_st **cursor_key_exec;
123
124 ptr->cursor_server= 0;
125 memset(buffer, 0, HUGE_STRING_LEN);
126
127 rc= memcached_connect(ptr);
128
129 if (rc != MEMCACHED_SUCCESS)
130 return rc;
131
132 cursor_key_exec= (memcached_string_st **)malloc(sizeof(memcached_string_st *) * ptr->number_of_hosts);
133 memset(cursor_key_exec, 0, sizeof(memcached_string_st *) * ptr->number_of_hosts);
134
135
136 for (x= 0; x < number_of_keys; x++)
137 {
138 unsigned int server_key;
139
140 server_key= memcached_generate_hash(keys[x], key_length[x]) % ptr->number_of_hosts;
141
142 if (cursor_key_exec[server_key])
143 {
144 memcached_string_st *string= cursor_key_exec[server_key];
145
146 memcached_string_append_character(ptr, string, ' ');
147 memcached_string_append(ptr, string, keys[x], key_length[x]);
148 }
149 else
150 {
151 memcached_string_st *string= memcached_string_init(ptr, SMALL_STRING_LEN);
152
153 if (!string)
154 assert(0);
155
156 memcached_string_append(ptr, string, "get ", 4);
157 memcached_string_append(ptr, string, keys[x], key_length[x]);
158
159 cursor_key_exec[server_key]= string;
160 }
161 }
162
163
164 /*
165 Should we muddle on if some servers are dead?
166 */
167 for (x= 0; x < ptr->number_of_hosts; x++)
168 {
169 if (cursor_key_exec[x])
170 {
171 memcached_string_st *string= cursor_key_exec[x];
172 memcached_string_append(ptr, string, "\r\n", 2);
173
174 if ((write(ptr->hosts[x].fd, string->string,
175 memcached_string_length(ptr, string)) == -1))
176 {
177 memcached_quit(ptr);
178 rc= MEMCACHED_SOME_ERRORS;
179 }
180 memcached_string_free(ptr, string);
181 }
182 }
183
184 free(cursor_key_exec);
185
186 return rc;
187 }
188
189 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
190 size_t *value_length,
191 uint16_t *flags,
192 memcached_return *error)
193 {
194 char *value_check;
195
196 while (ptr->cursor_server < ptr->number_of_hosts)
197 {
198 value_check= memcached_value_fetch(ptr, key, key_length, value_length, flags,
199 error, 1, ptr->cursor_server);
200
201 if (*error == MEMCACHED_NOTFOUND)
202 ptr->cursor_server++;
203 else
204 return value_check;
205 }
206
207 return NULL;
208 }