3 static char *memcached_value_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
6 memcached_return
*error
,
8 unsigned int server_key
)
10 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
18 end_ptr
= buffer
+ MEMCACHED_DEFAULT_COMMAND_SIZE
;
22 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
23 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
25 if (*error
== MEMCACHED_SUCCESS
)
30 string_ptr
+= 6; /* "VALUE " */
35 memset(key
, 0, MEMCACHED_MAX_KEY
);
36 for (; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++)
42 else /* Skip characters */
43 for (; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++);
45 if (end_ptr
== string_ptr
)
48 /* Flags fetch move past space */
50 if (end_ptr
== string_ptr
)
52 for (next_ptr
= string_ptr
; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++);
53 *flags
= (uint16_t)strtol(next_ptr
, &string_ptr
, 10);
55 if (end_ptr
== string_ptr
)
58 /* Length fetch move past space*/
60 if (end_ptr
== string_ptr
)
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);
66 if (end_ptr
== string_ptr
)
69 /* Skip past the \r\n */
72 if (end_ptr
< string_ptr
)
78 size_t partial_length
;
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));
90 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
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)
100 value_ptr
+= partial_length
;
101 read_length
+= partial_length
;
102 to_read
-= partial_length
;
103 if (read_length
== (size_t)(*value_length
+ 2))
107 if (read_length
!= (size_t)(*value_length
+ 2))
119 *error
= MEMCACHED_PARTIAL_READ
;
123 char *memcached_get(memcached_st
*ptr
, char *key
, size_t key_length
,
124 size_t *value_length
,
126 memcached_return
*error
)
129 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
130 unsigned int server_key
;
133 LIBMEMCACHED_MEMCACHED_GET_START();
136 *error
= memcached_connect(ptr
);
138 if (*error
!= MEMCACHED_SUCCESS
)
141 server_key
= memcached_generate_hash(key
, key_length
) % ptr
->number_of_hosts
;
143 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, "get %.*s\r\n",
144 (int)key_length
, key
);
145 if (*error
!= MEMCACHED_SUCCESS
)
148 if ((send(ptr
->hosts
[server_key
].fd
, buffer
, send_length
, 0) == -1))
150 *error
= MEMCACHED_WRITE_FAILURE
;
154 value
= memcached_value_fetch(ptr
, key
, &key_length
, value_length
, flags
,
155 error
, 0, server_key
);
156 /* We need to read END */
157 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
158 if (rc
!= MEMCACHED_NOTFOUND
)
162 *error
= MEMCACHED_PROTOCOL_ERROR
;
164 LIBMEMCACHED_MEMCACHED_GET_END();
169 memcached_return
memcached_mget(memcached_st
*ptr
,
170 char **keys
, size_t *key_length
,
171 unsigned int number_of_keys
)
173 char buffer
[HUGE_STRING_LEN
];
176 memcached_string_st
**cursor_key_exec
;
177 LIBMEMCACHED_MEMCACHED_MGET_START();
179 ptr
->cursor_server
= 0;
180 memset(buffer
, 0, HUGE_STRING_LEN
);
182 rc
= memcached_connect(ptr
);
184 if (rc
!= MEMCACHED_SUCCESS
)
187 cursor_key_exec
= (memcached_string_st
**)malloc(sizeof(memcached_string_st
*) * ptr
->number_of_hosts
);
188 memset(cursor_key_exec
, 0, sizeof(memcached_string_st
*) * ptr
->number_of_hosts
);
191 for (x
= 0; x
< number_of_keys
; x
++)
193 unsigned int server_key
;
195 server_key
= memcached_generate_hash(keys
[x
], key_length
[x
]) % ptr
->number_of_hosts
;
197 if (cursor_key_exec
[server_key
])
199 memcached_string_st
*string
= cursor_key_exec
[server_key
];
201 memcached_string_append_character(ptr
, string
, ' ');
202 memcached_string_append(ptr
, string
, keys
[x
], key_length
[x
]);
206 memcached_string_st
*string
= memcached_string_init(ptr
, SMALL_STRING_LEN
);
208 /* We need to figure out the correct way to error in case of this failure */
212 memcached_string_append(ptr
, string
, "get ", 4);
213 memcached_string_append(ptr
, string
, keys
[x
], key_length
[x
]);
215 cursor_key_exec
[server_key
]= string
;
221 Should we muddle on if some servers are dead?
223 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
225 if (cursor_key_exec
[x
])
227 memcached_string_st
*string
= cursor_key_exec
[x
];
228 memcached_string_append(ptr
, string
, "\r\n", 2);
230 if ((send(ptr
->hosts
[x
].fd
, string
->string
,
231 memcached_string_length(ptr
, string
), 0) == -1))
234 rc
= MEMCACHED_SOME_ERRORS
;
236 memcached_string_free(ptr
, string
);
237 cursor_key_exec
[x
]= NULL
; /* Remove warning */
241 free(cursor_key_exec
);
243 LIBMEMCACHED_MEMCACHED_MGET_END();
247 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
248 size_t *value_length
,
250 memcached_return
*error
)
254 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
256 value_check
= memcached_value_fetch(ptr
, key
, key_length
, value_length
, flags
,
257 error
, 1, ptr
->cursor_server
);
259 if (*error
== MEMCACHED_NOTFOUND
)
260 ptr
->cursor_server
++;
261 else if (*error
!= MEMCACHED_SUCCESS
)