From e233475c354a12f9f07adbd73b13fbf719b21367 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Fri, 2 Apr 2010 12:55:21 -0700 Subject: [PATCH] Make use of new vdo(). --- libmemcached/auto.c | 4 ++-- libmemcached/common.h | 2 +- libmemcached/delete.c | 19 +++++++-------- libmemcached/do.c | 50 ++++++++++++++++++++++++++++++++++++++++ libmemcached/do.h | 5 ++++ libmemcached/flush.c | 8 +++---- libmemcached/quit.c | 2 +- libmemcached/stats.c | 14 +++++++---- libmemcached/storage.c | 33 ++++++++++++-------------- libmemcached/verbosity.c | 3 +-- libmemcached/version.c | 5 ++-- 11 files changed, 99 insertions(+), 46 deletions(-) diff --git a/libmemcached/auto.c b/libmemcached/auto.c index 16e65b86..b73425f0 100644 --- a/libmemcached/auto.c +++ b/libmemcached/auto.c @@ -118,13 +118,13 @@ static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd, struct __write_vector_st vector[]= { + { .length= sizeof(request.bytes), .buffer= request.bytes }, { .length= ptr->prefix_key_length, .buffer= ptr->prefix_key }, { .length= key_length, .buffer= key } }; memcached_return_t rc; - if (((rc= memcached_do(instance, request.bytes, sizeof(request.bytes), false)) != MEMCACHED_SUCCESS) || - (memcached_io_writev(instance, vector, 2, true) == -1)) + if ((rc= memcached_vdo(instance, vector, 3, true)) != MEMCACHED_SUCCESS) { memcached_io_reset(instance); return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc; diff --git a/libmemcached/common.h b/libmemcached/common.h index a03cabbf..f8f236ed 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -60,8 +60,8 @@ LIBMEMCACHED_LOCAL memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key); /* These are private not to be installed headers */ -#include "libmemcached/do.h" #include "libmemcached/io.h" +#include "libmemcached/do.h" #include "libmemcached/internal.h" #include "libmemcached/libmemcached_probes.h" #include "libmemcached/memcached/protocol_binary.h" diff --git a/libmemcached/delete.c b/libmemcached/delete.c index d2109ecc..2af6e159 100644 --- a/libmemcached/delete.c +++ b/libmemcached/delete.c @@ -99,11 +99,13 @@ memcached_return_t memcached_delete_by_key(memcached_st *ptr, } } else - send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, - "delete %.*s%.*s%s\r\n", - (int)ptr->prefix_key_length, - ptr->prefix_key, - (int)key_length, key, no_reply ? " noreply" :""); + { + send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, + "delete %.*s%.*s%s\r\n", + (int)ptr->prefix_key_length, + ptr->prefix_key, + (int)key_length, key, no_reply ? " noreply" :""); + } if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) { @@ -175,14 +177,14 @@ static inline memcached_return_t binary_delete(memcached_st *ptr, struct __write_vector_st vector[]= { + { .length= sizeof(request.bytes), .buffer= request.bytes}, { .length= ptr->prefix_key_length, .buffer= ptr->prefix_key }, { .length= key_length, .buffer= key }, }; memcached_return_t rc= MEMCACHED_SUCCESS; - if (((rc= memcached_do(instance, request.bytes, sizeof(request.bytes), false)) != MEMCACHED_SUCCESS) || - (memcached_io_writev(instance, vector, 2, flush) == -1)) + if ((rc= memcached_vdo(instance, vector, 3, flush)) != MEMCACHED_SUCCESS) { memcached_io_reset(instance); rc= (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc; @@ -202,8 +204,7 @@ static inline memcached_return_t binary_delete(memcached_st *ptr, replica= memcached_server_instance_fetch(ptr, server_key); - if ((memcached_do(replica, (const char*)request.bytes, sizeof(request.bytes), false) != MEMCACHED_SUCCESS) || - (memcached_io_write(replica, key, key_length, flush) == -1)) + if (memcached_vdo(replica, vector, 3, flush) != MEMCACHED_SUCCESS) { memcached_io_reset(replica); } diff --git a/libmemcached/do.c b/libmemcached/do.c index 34c2c953..dcc09afb 100644 --- a/libmemcached/do.c +++ b/libmemcached/do.c @@ -39,9 +39,59 @@ memcached_return_t memcached_do(memcached_server_write_instance_st ptr, const vo sent_length= memcached_io_write(ptr, command, command_length, with_flush); if (sent_length == -1 || (size_t)sent_length != command_length) + { rc= MEMCACHED_WRITE_FAILURE; + } else if ((ptr->root->flags.no_reply) == 0) + { memcached_server_response_increment(ptr); + } + + return rc; +} + +memcached_return_t memcached_vdo(memcached_server_write_instance_st ptr, + const struct __write_vector_st *vector, size_t count, + bool with_flush) +{ + memcached_return_t rc; + ssize_t sent_length; + + WATCHPOINT_ASSERT(command_length); + WATCHPOINT_ASSERT(command); + + if ((rc= memcached_connect(ptr)) != MEMCACHED_SUCCESS) + { + WATCHPOINT_ERROR(rc); + return rc; + } + + /* + ** Since non buffering ops in UDP mode dont check to make sure they will fit + ** before they start writing, if there is any data in buffer, clear it out, + ** otherwise we might get a partial write. + **/ + if (ptr->type == MEMCACHED_CONNECTION_UDP && with_flush && ptr->write_buffer_offset > UDP_DATAGRAM_HEADER_LENGTH) + { + memcached_io_write(ptr, NULL, 0, true); + } + + sent_length= memcached_io_writev(ptr, vector, count, with_flush); + + size_t command_length= 0; + for (uint32_t x= 0; x < count; ++x, vector++) + { + command_length+= vector->length; + } + + if (sent_length == -1 || (size_t)sent_length != command_length) + { + rc= MEMCACHED_WRITE_FAILURE; + } + else if ((ptr->root->flags.no_reply) == 0) + { + memcached_server_response_increment(ptr); + } return rc; } diff --git a/libmemcached/do.h b/libmemcached/do.h index 8f95c8b5..d6d018d9 100644 --- a/libmemcached/do.h +++ b/libmemcached/do.h @@ -22,6 +22,11 @@ memcached_return_t memcached_do(memcached_server_write_instance_st ptr, size_t command_length, bool with_flush); +LIBMEMCACHED_LOCAL +memcached_return_t memcached_vdo(memcached_server_write_instance_st ptr, + const struct __write_vector_st *vector, size_t count, + bool with_flush); + #ifdef __cplusplus } #endif diff --git a/libmemcached/flush.c b/libmemcached/flush.c index c8428f70..425d4576 100644 --- a/libmemcached/flush.c +++ b/libmemcached/flush.c @@ -55,7 +55,6 @@ static memcached_return_t memcached_flush_textual(memcached_st *ptr, static memcached_return_t memcached_flush_binary(memcached_st *ptr, time_t expiration) { - uint32_t x; protocol_binary_request_flush request= {.bytes= {0}}; unlikely (memcached_server_count(ptr) == 0) @@ -68,7 +67,7 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr, request.message.header.request.bodylen= htonl(request.message.header.request.extlen); request.message.body.expiration= htonl((uint32_t) expiration); - for (x= 0; x < memcached_server_count(ptr); x++) + for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); @@ -82,15 +81,14 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr, request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH; } - if (memcached_do(instance, request.bytes, - sizeof(request.bytes), true) != MEMCACHED_SUCCESS) + if (memcached_do(instance, request.bytes, sizeof(request.bytes), true) != MEMCACHED_SUCCESS) { memcached_io_reset(instance); return MEMCACHED_WRITE_FAILURE; } } - for (x= 0; x < memcached_server_count(ptr); x++) + for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); diff --git a/libmemcached/quit.c b/libmemcached/quit.c index 35d434bc..592373f6 100644 --- a/libmemcached/quit.c +++ b/libmemcached/quit.c @@ -30,7 +30,7 @@ void memcached_quit_server(memcached_server_st *ptr, bool io_death) } else { - rc= memcached_do(ptr, "quit\r\n", sizeof("quit\r\n"), true); + rc= memcached_do(ptr, "quit\r\n", strlen("quit\r\n"), true); } WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_FETCH_NOTFINISHED); diff --git a/libmemcached/stats.c b/libmemcached/stats.c index fcafe215..ff5416d4 100644 --- a/libmemcached/stats.c +++ b/libmemcached/stats.c @@ -250,9 +250,13 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, request.message.header.request.keylen= htons((uint16_t)len); request.message.header.request.bodylen= htonl((uint32_t) len); - if ((memcached_do(instance, request.bytes, - sizeof(request.bytes), false) != MEMCACHED_SUCCESS) || - (memcached_io_write(instance, args, len, true) == -1)) + struct __write_vector_st vector[]= + { + { .length= sizeof(request.bytes), .buffer= request.bytes }, + { .length= len, .buffer= args } + }; + + if (memcached_vdo(instance, vector, 2, true) != MEMCACHED_SUCCESS) { memcached_io_reset(instance); return MEMCACHED_WRITE_FAILURE; @@ -271,8 +275,8 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, memcached_server_response_decrement(instance); do { - rc= memcached_response(instance, buffer, - sizeof(buffer), NULL); + rc= memcached_response(instance, buffer, sizeof(buffer), NULL); + if (rc == MEMCACHED_END) break; diff --git a/libmemcached/storage.c b/libmemcached/storage.c index f152f832..2b232fe7 100644 --- a/libmemcached/storage.c +++ b/libmemcached/storage.c @@ -148,10 +148,13 @@ static inline memcached_return_t memcached_send(memcached_st *ptr, goto error; } - /* Send command header */ - rc= memcached_do(instance, buffer, write_length, false); - if (rc != MEMCACHED_SUCCESS) - goto error; + + struct __write_vector_st vector[]= + { + { .length= write_length, .buffer= buffer }, + { .length= value_length, .buffer= value }, + { .length= 2, .buffer= "\r\n" } + }; if (ptr->flags.buffer_requests && verb == SET_OP) { @@ -162,17 +165,10 @@ static inline memcached_return_t memcached_send(memcached_st *ptr, to_write= true; } - struct __write_vector_st vector[]= - { - { .length= value_length, .buffer= value }, - { .length= 2, .buffer= "\r\n" } - }; - - if (memcached_io_writev(instance, vector, 2, to_write) == -1) + /* Send command header */ + rc= memcached_vdo(instance, vector, 3, to_write); + if (rc == MEMCACHED_SUCCESS) { - rc= MEMCACHED_WRITE_FAILURE; - goto error; - } if (ptr->flags.no_reply) return (to_write == false) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS; @@ -186,8 +182,10 @@ static inline memcached_return_t memcached_send(memcached_st *ptr, return MEMCACHED_SUCCESS; else return rc; + } error: + memcached_io_reset(instance); return rc; @@ -488,6 +486,7 @@ static memcached_return_t memcached_send_binary(memcached_st *ptr, struct __write_vector_st vector[]= { + { .length= send_length, .buffer= request.bytes }, { .length= ptr->prefix_key_length, .buffer= ptr->prefix_key }, { .length= key_length, .buffer= key }, { .length= value_length, .buffer= value } @@ -495,8 +494,7 @@ static memcached_return_t memcached_send_binary(memcached_st *ptr, /* write the header */ memcached_return_t rc; - if (((rc= memcached_do(server, (const char*)request.bytes, send_length, false)) != MEMCACHED_SUCCESS) || - (memcached_io_writev(server, vector, 3, flush) == -1)) + if ((rc= memcached_vdo(server, vector, 4, flush)) != MEMCACHED_SUCCESS) { memcached_io_reset(server); return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc; @@ -516,8 +514,7 @@ static memcached_return_t memcached_send_binary(memcached_st *ptr, instance= memcached_server_instance_fetch(ptr, server_key); - if ((memcached_do(instance, (const char*)request.bytes, send_length, false) != MEMCACHED_SUCCESS) || - (memcached_io_writev(instance, vector, 3, flush) == -1)) + if (memcached_vdo(instance, vector, 4, false) != MEMCACHED_SUCCESS) { memcached_io_reset(instance); } diff --git a/libmemcached/verbosity.c b/libmemcached/verbosity.c index 00c89fbf..24db8d78 100644 --- a/libmemcached/verbosity.c +++ b/libmemcached/verbosity.c @@ -2,7 +2,6 @@ memcached_return_t memcached_verbosity(memcached_st *ptr, uint32_t verbosity) { - uint32_t x; size_t send_length; memcached_return_t rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; @@ -13,7 +12,7 @@ memcached_return_t memcached_verbosity(memcached_st *ptr, uint32_t verbosity) return MEMCACHED_WRITE_FAILURE; rc= MEMCACHED_SUCCESS; - for (x= 0; x < memcached_server_count(ptr); x++) + for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { memcached_return_t rrc; memcached_server_write_instance_st instance= diff --git a/libmemcached/version.c b/libmemcached/version.c index 7ab48880..ba0b243e 100644 --- a/libmemcached/version.c +++ b/libmemcached/version.c @@ -69,14 +69,13 @@ static inline memcached_return_t memcached_version_textual(memcached_st *ptr) static inline memcached_return_t memcached_version_binary(memcached_st *ptr) { memcached_return_t rc; - unsigned int x; protocol_binary_request_version request= { .bytes= {0}}; request.message.header.request.magic= PROTOCOL_BINARY_REQ; request.message.header.request.opcode= PROTOCOL_BINARY_CMD_VERSION; request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; rc= MEMCACHED_SUCCESS; - for (x= 0; x < memcached_server_count(ptr); x++) + for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { memcached_return_t rrc; @@ -92,7 +91,7 @@ static inline memcached_return_t memcached_version_binary(memcached_st *ptr) } } - for (x= 0; x < memcached_server_count(ptr); x++) + for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); -- 2.30.2