2 #include "memcached_io.h"
4 static char *memcached_value_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
7 memcached_return
*error
,
9 unsigned int server_key
)
11 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
19 memset(buffer
, 0, MEMCACHED_DEFAULT_COMMAND_SIZE
);
20 end_ptr
= buffer
+ MEMCACHED_DEFAULT_COMMAND_SIZE
;
24 *error
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
26 if (*error
== MEMCACHED_SUCCESS
)
31 string_ptr
+= 6; /* "VALUE " */
36 memset(key
, 0, MEMCACHED_MAX_KEY
);
37 for (; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++)
43 else /* Skip characters */
44 for (; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++);
46 if (end_ptr
== string_ptr
)
49 /* Flags fetch move past space */
51 if (end_ptr
== string_ptr
)
53 for (next_ptr
= string_ptr
; end_ptr
== string_ptr
|| *string_ptr
!= ' '; string_ptr
++);
54 *flags
= (uint16_t)strtol(next_ptr
, &string_ptr
, 10);
56 if (end_ptr
== string_ptr
)
59 /* Length fetch move past space*/
61 if (end_ptr
== string_ptr
)
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);
67 if (end_ptr
== string_ptr
)
70 /* Skip past the \r\n */
73 if (end_ptr
< string_ptr
)
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;
98 read_length
= memcached_io_read(ptr
, server_key
,
101 if (read_length
!= (size_t)(*value_length
+ 2))
110 else if (*error
== MEMCACHED_END
)
111 *error
= MEMCACHED_NOTFOUND
;
115 *error
= MEMCACHED_PARTIAL_READ
;
119 char *memcached_get(memcached_st
*ptr
, char *key
, size_t key_length
,
120 size_t *value_length
,
122 memcached_return
*error
)
125 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
126 unsigned int server_key
;
128 LIBMEMCACHED_MEMCACHED_GET_START();
131 *error
= memcached_connect(ptr
);
133 if (*error
!= MEMCACHED_SUCCESS
)
136 server_key
= memcached_generate_hash(key
, key_length
) % ptr
->number_of_hosts
;
138 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, "get %.*s\r\n",
139 (int)key_length
, key
);
141 if ((memcached_io_write(ptr
, server_key
, buffer
, send_length
, 1)) == -1)
143 *error
= MEMCACHED_WRITE_FAILURE
;
147 value
= memcached_value_fetch(ptr
, key
, &key_length
, value_length
, flags
,
148 error
, 0, server_key
);
149 if (*error
== MEMCACHED_END
&& *value_length
== 0)
151 *error
= MEMCACHED_NOTFOUND
;
154 else if (*error
== MEMCACHED_SUCCESS
)
157 /* We need to read END */
158 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
160 if (rc
!= MEMCACHED_END
)
162 *error
= MEMCACHED_PROTOCOL_ERROR
;
169 LIBMEMCACHED_MEMCACHED_GET_END();
177 LIBMEMCACHED_MEMCACHED_GET_END();
182 memcached_return
memcached_mget(memcached_st
*ptr
,
183 char **keys
, size_t *key_length
,
184 unsigned int number_of_keys
)
186 char buffer
[HUGE_STRING_LEN
];
189 memcached_string_st
**cursor_key_exec
;
190 LIBMEMCACHED_MEMCACHED_MGET_START();
192 ptr
->cursor_server
= 0;
193 memset(buffer
, 0, HUGE_STRING_LEN
);
195 rc
= memcached_connect(ptr
);
197 if (rc
!= MEMCACHED_SUCCESS
)
200 cursor_key_exec
= (memcached_string_st
**)malloc(sizeof(memcached_string_st
*) * ptr
->number_of_hosts
);
201 memset(cursor_key_exec
, 0, sizeof(memcached_string_st
*) * ptr
->number_of_hosts
);
204 for (x
= 0; x
< number_of_keys
; x
++)
206 unsigned int server_key
;
208 server_key
= memcached_generate_hash(keys
[x
], key_length
[x
]) % ptr
->number_of_hosts
;
210 if (cursor_key_exec
[server_key
])
212 memcached_string_st
*string
= cursor_key_exec
[server_key
];
214 memcached_string_append_character(ptr
, string
, ' ');
215 memcached_string_append(ptr
, string
, keys
[x
], key_length
[x
]);
219 memcached_string_st
*string
= memcached_string_create(ptr
, SMALL_STRING_LEN
);
221 /* We need to figure out the correct way to error in case of this failure */
225 memcached_string_append(ptr
, string
, "get ", 4);
226 memcached_string_append(ptr
, string
, keys
[x
], key_length
[x
]);
228 cursor_key_exec
[server_key
]= string
;
234 Should we muddle on if some servers are dead?
236 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
238 if (cursor_key_exec
[x
])
240 memcached_string_st
*string
= cursor_key_exec
[x
];
241 memcached_string_append(ptr
, string
, "\r\n", 2);
243 if ((memcached_io_write(ptr
, x
, string
->string
,
244 memcached_string_length(ptr
, string
), 1)) == -1)
247 rc
= MEMCACHED_SOME_ERRORS
;
249 memcached_string_free(ptr
, string
);
250 cursor_key_exec
[x
]= NULL
; /* Remove warning */
254 free(cursor_key_exec
);
256 LIBMEMCACHED_MEMCACHED_MGET_END();
260 char *memcached_fetch(memcached_st
*ptr
, char *key
, size_t *key_length
,
261 size_t *value_length
,
263 memcached_return
*error
)
267 while (ptr
->cursor_server
< ptr
->number_of_hosts
)
269 value_check
= memcached_value_fetch(ptr
, key
, key_length
, value_length
, flags
,
270 error
, 1, ptr
->cursor_server
);
272 if (*error
== MEMCACHED_NOTFOUND
)
273 ptr
->cursor_server
++;
274 else if (*error
!= MEMCACHED_SUCCESS
)