Started to abstract out the stats calls.
[m6w6/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 char *buffer_ptr;
121 unsigned int x;
122 memcached_return rc;
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 memcpy(buffer, "get", strlen("get"));
133 buffer_ptr= buffer;
134 buffer_ptr+=strlen("get");
135
136 for (x= 0; x < number_of_keys; x++)
137 {
138 *buffer_ptr= ' ';
139 buffer_ptr++;
140 memcpy(buffer_ptr, keys[x], key_length[x]);
141 buffer_ptr+= key_length[x];
142 }
143
144 memcpy(buffer_ptr, "\r\n", 2);
145 buffer_ptr+=2;
146
147 /*
148 This must be fixed. Right now we hit every server, and send keys
149 to all servers. We should fix this quickly.
150 */
151 for (x= 0; x < ptr->number_of_hosts; x++)
152 {
153 if ((write(ptr->hosts[x].fd, buffer, (size_t)(buffer_ptr - buffer)) == -1))
154 {
155 memcached_quit(ptr);
156 rc= MEMCACHED_SOME_ERRORS;
157 }
158 }
159
160 return rc;
161 }
162
163 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
164 size_t *value_length,
165 uint16_t *flags,
166 memcached_return *error)
167 {
168 char *value_check;
169
170 while (ptr->cursor_server < ptr->number_of_hosts)
171 {
172 value_check= memcached_value_fetch(ptr, key, key_length, value_length, flags,
173 error, 1, ptr->cursor_server);
174
175 if (*error == MEMCACHED_NOTFOUND)
176 ptr->cursor_server++;
177 else
178 return value_check;
179 }
180
181 return NULL;
182 }