0.4
* Added buffered IO to write calls for keys
+ * Added buffered IO for reads
* memstat was broken (bad if/else on connect)
0.3 Mon Oct 1 06:37:52 PDT 2007
memcached_server_st *hosts;
unsigned int number_of_hosts;
unsigned int cursor_server;
+ char read_buffer[MEMCACHED_MAX_BUFFER];
+ size_t read_buffer_length;
+ char *read_ptr;
char write_buffer[MEMCACHED_MAX_BUFFER];
size_t write_buffer_offset;
size_t write_between_flush;
#include "common.h"
+#include "memcached_io.h"
static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_length,
size_t *value_length,
if (*value_length)
{
size_t read_length;
- size_t partial_length;
size_t to_read;
char *value;
char *value_ptr;
value_ptr= value;
read_length= 0;
to_read= (*value_length) + 2;
- /* This is overkill */
- while ((partial_length= recv(ptr->hosts[server_key].fd, value_ptr, to_read, 0)) > 0)
- {
- value_ptr+= partial_length;
- read_length+= partial_length;
- to_read-= partial_length;
- if (read_length == (size_t)(*value_length + 2))
- break;
- }
+
+ read_length= memcached_io_read(ptr, server_key,
+ value_ptr, to_read);
if (read_length != (size_t)(*value_length + 2))
{
#include <memcached.h>
-ssize_t memcached_io_read(memcached_st *ptr, char *buf, size_t length)
+ssize_t memcached_io_read(memcached_st *ptr, unsigned int server_key,
+ char *buffer, size_t length)
{
- return -1;
+ size_t x;
+
+ for (x= 0; x < length; x++)
+ {
+ if (!ptr->read_buffer_length)
+ {
+ ptr->read_buffer_length= recv(ptr->hosts[server_key].fd,
+ ptr->read_buffer,
+ MEMCACHED_MAX_BUFFER, 0);
+ ptr->read_ptr= ptr->read_buffer;
+
+ if (ptr->read_buffer_length == -1)
+ return -1;
+ if (ptr->read_buffer_length == 0)
+ return x;
+ }
+ buffer[x]= *ptr->read_ptr;
+ ptr->read_ptr++;
+ ptr->read_buffer_length--;
+ }
+
+ return length;
}
ssize_t memcached_io_write(memcached_st *ptr, unsigned int server_key,
ssize_t memcached_io_write(memcached_st *ptr, unsigned int server_key,
char *buffer, size_t length);
void memcached_io_reset(memcached_st *ptr, unsigned int server_key);
+ssize_t memcached_io_read(memcached_st *ptr, unsigned int server_key,
+ char *buffer, size_t length);
while (1)
{
unsigned int read_length;
- read_length= recv(ptr->hosts[server_key].fd, buffer_ptr, 1, 0);
+
+ read_length= memcached_io_read(ptr, server_key,
+ buffer_ptr, 1);
if (read_length != 1)
return MEMCACHED_UNKNOWN_READ_FAILURE;