From: Brian Aker Date: Tue, 2 Oct 2007 01:38:36 +0000 (-0700) Subject: Adding buffered IO to reads X-Git-Tag: 0.4~8 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=257f0547823cf6b18473ba8bd2580b38235f6d79;p=awesomized%2Flibmemcached Adding buffered IO to reads --- diff --git a/ChangeLog b/ChangeLog index f295a9dd..27a76640 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 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 diff --git a/include/memcached.h b/include/memcached.h index f4d0b25a..a5cc14ba 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -120,6 +120,9 @@ struct memcached_st { 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; diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 404c332c..4d2b10d2 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -1,4 +1,5 @@ #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, @@ -75,7 +76,6 @@ static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_len if (*value_length) { size_t read_length; - size_t partial_length; size_t to_read; char *value; char *value_ptr; @@ -94,15 +94,9 @@ static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_len 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)) { diff --git a/lib/memcached_io.c b/lib/memcached_io.c index 7067237f..a6e563d3 100644 --- a/lib/memcached_io.c +++ b/lib/memcached_io.c @@ -4,9 +4,31 @@ #include -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, diff --git a/lib/memcached_io.h b/lib/memcached_io.h index d9abd625..0fac6d62 100644 --- a/lib/memcached_io.h +++ b/lib/memcached_io.h @@ -5,3 +5,5 @@ ssize_t memcached_io_flush(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); diff --git a/lib/memcached_response.c b/lib/memcached_response.c index cb74be0a..24ee832f 100644 --- a/lib/memcached_response.c +++ b/lib/memcached_response.c @@ -21,7 +21,9 @@ memcached_return memcached_response(memcached_st *ptr, 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;