From: Brian Aker Date: Tue, 13 Nov 2007 21:31:32 +0000 (-0800) Subject: Refactor all code to go through memcached_do(). X-Git-Tag: 0.10~15 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=5a6b411afb61c7d14aee3f52037f719c42072160;p=m6w6%2Flibmemcached Refactor all code to go through memcached_do(). AKA this removed a bunch of copy and paste. It will also allow me to make commands "safer" in case of mid-cursor exection. --- diff --git a/lib/Makefile.am b/lib/Makefile.am index a84f43f5..2908fbb4 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -31,6 +31,7 @@ libmemcached_la_SOURCES = crc.c \ memcached_behavior.c \ memcached_connect.c \ memcached_delete.c \ + memcached_do.c \ memcached_flush.c \ memcached_get.c \ memcached_hash.c \ diff --git a/lib/common.h b/lib/common.h index 3a3a9695..ae531e16 100644 --- a/lib/common.h +++ b/lib/common.h @@ -78,6 +78,8 @@ memcached_return memcached_string_append(memcached_string_st *string, size_t memcached_string_backspace(memcached_string_st *string, size_t remove); memcached_return memcached_string_reset(memcached_string_st *string); void memcached_string_free(memcached_string_st *string); +memcached_return memcached_do(memcached_st *ptr, unsigned int server_key, char *commmand, + size_t command_length, char with_flush); #endif /* __COMMON_H__ */ diff --git a/lib/memcached_auto.c b/lib/memcached_auto.c index d7e30e56..61ad9125 100644 --- a/lib/memcached_auto.c +++ b/lib/memcached_auto.c @@ -6,7 +6,7 @@ static memcached_return memcached_auto(memcached_st *ptr, unsigned int offset, uint64_t *value) { - size_t send_length, sent_length; + size_t send_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; unsigned int server_key; @@ -19,19 +19,16 @@ static memcached_return memcached_auto(memcached_st *ptr, server_key= memcached_generate_hash(ptr, key, key_length); - if ((rc= memcached_connect(ptr, server_key)) != MEMCACHED_SUCCESS) - return rc; - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s %.*s %u\r\n", verb, (int)key_length, key, offset); if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; - sent_length= memcached_io_write(ptr, server_key, buffer, send_length, 1); - if (sent_length == -1 || sent_length != send_length) - return MEMCACHED_WRITE_FAILURE; + rc= memcached_do(ptr, server_key, buffer, send_length, 1); + if (rc != MEMCACHED_SUCCESS) + return rc; memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE); diff --git a/lib/memcached_delete.c b/lib/memcached_delete.c index cedfa442..0c36aaeb 100644 --- a/lib/memcached_delete.c +++ b/lib/memcached_delete.c @@ -4,7 +4,7 @@ memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_lengt time_t expiration) { char to_write; - size_t send_length, sent_length; + size_t send_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; unsigned int server_key; @@ -19,10 +19,6 @@ memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_lengt server_key= memcached_generate_hash(ptr, key, key_length); - if ((rc= memcached_connect(ptr, server_key)) != MEMCACHED_SUCCESS) - return rc; - - if (expiration) send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "delete %.*s %llu\r\n", (int)key_length, key, @@ -42,12 +38,9 @@ memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_lengt else to_write= 1; - if ((sent_length= memcached_io_write(ptr, server_key, buffer, send_length, to_write)) == -1) - { - memcached_quit_server(ptr, server_key); - rc= MEMCACHED_WRITE_FAILURE; + rc= memcached_do(ptr, server_key, buffer, send_length, to_write); + if (rc != MEMCACHED_SUCCESS) goto error; - } if ((ptr->flags & MEM_NO_BLOCK)) { @@ -61,8 +54,7 @@ memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_lengt rc= MEMCACHED_SUCCESS; } - LIBMEMCACHED_MEMCACHED_DELETE_END(); - error: + LIBMEMCACHED_MEMCACHED_DELETE_END(); return rc; } diff --git a/lib/memcached_do.c b/lib/memcached_do.c new file mode 100644 index 00000000..5fcfe832 --- /dev/null +++ b/lib/memcached_do.c @@ -0,0 +1,22 @@ +#include "common.h" + +memcached_return memcached_do(memcached_st *ptr, unsigned int server_key, char *command, + size_t command_length, char with_flush) +{ + memcached_return rc; + ssize_t sent_length; + + WATCHPOINT_ASSERT(command); + if ((rc= memcached_connect(ptr, server_key)) != MEMCACHED_SUCCESS) + return rc; + + sent_length= memcached_io_write(ptr, server_key, command, command_length, with_flush); + + if (sent_length == -1 || sent_length != command_length) + { + memcached_quit_server(ptr, server_key); + rc= MEMCACHED_WRITE_FAILURE; + } + + return rc; +} diff --git a/lib/memcached_flush.c b/lib/memcached_flush.c index 58f5bff2..f40c1a6b 100644 --- a/lib/memcached_flush.c +++ b/lib/memcached_flush.c @@ -3,7 +3,7 @@ memcached_return memcached_flush(memcached_st *ptr, time_t expiration) { unsigned int x; - size_t send_length, sent_length; + size_t send_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; LIBMEMCACHED_MEMCACHED_FLUSH_START(); @@ -29,10 +29,9 @@ memcached_return memcached_flush(memcached_st *ptr, time_t expiration) if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; - sent_length= memcached_io_write(ptr, x, buffer, send_length, 1); - - if (sent_length == -1 || sent_length != send_length) - return MEMCACHED_WRITE_FAILURE; + rc= memcached_do(ptr, x, buffer, send_length, 1); + if (rc != MEMCACHED_SUCCESS) + goto error; rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x); @@ -40,6 +39,7 @@ memcached_return memcached_flush(memcached_st *ptr, time_t expiration) rc= MEMCACHED_SOME_ERRORS; } +error: LIBMEMCACHED_MEMCACHED_FLUSH_END(); return rc; } diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 70d1b349..cbe94926 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -144,7 +144,10 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, LIBMEMCACHED_MEMCACHED_GET_START(); if (key_length == 0) - return MEMCACHED_NO_KEY_PROVIDED; + { + *error= MEMCACHED_NO_KEY_PROVIDED; + return NULL; + } if (ptr->hosts == NULL || ptr->number_of_hosts == 0) { @@ -156,11 +159,6 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, result_buffer= &ptr->result_buffer; *value_length= 0; - *error= memcached_connect(ptr, server_key); - - if (*error != MEMCACHED_SUCCESS) - goto error; - memcpy(buf_ptr, "get ", 4); buf_ptr+= 4; memcpy(buf_ptr, key, key_length); @@ -168,11 +166,9 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, memcpy(buf_ptr, "\r\n", 2); buf_ptr+= 2; - if ((memcached_io_write(ptr, server_key, buffer, (size_t)(buf_ptr - buffer), 1)) == -1) - { - *error= MEMCACHED_WRITE_FAILURE; + *error= memcached_do(ptr, server_key, buffer, (size_t)(buf_ptr - buffer), 1); + if (*error != MEMCACHED_SUCCESS) goto error; - } *error= memcached_value_fetch(ptr, key, &key_length, result_buffer, flags, 0, server_key); diff --git a/lib/memcached_stats.c b/lib/memcached_stats.c index e90f1c0f..cbfdef35 100644 --- a/lib/memcached_stats.c +++ b/lib/memcached_stats.c @@ -223,7 +223,7 @@ static memcached_return memcached_stats_fetch(memcached_st *ptr, { memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - size_t send_length, sent_length; + size_t send_length; rc= memcached_connect(ptr, server_key); @@ -240,10 +240,9 @@ static memcached_return memcached_stats_fetch(memcached_st *ptr, if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; - sent_length= memcached_io_write(ptr, server_key, buffer, send_length, 1); - - if (sent_length == -1 || sent_length != send_length) - return MEMCACHED_WRITE_FAILURE; + rc= memcached_do(ptr, server_key, buffer, send_length, 1); + if (rc != MEMCACHED_SUCCESS) + goto error; while (1) { @@ -271,6 +270,7 @@ static memcached_return memcached_stats_fetch(memcached_st *ptr, break; } +error: if (rc == MEMCACHED_END) return MEMCACHED_SUCCESS; else diff --git a/lib/memcached_verbosity.c b/lib/memcached_verbosity.c index 6d356642..1ee9d070 100644 --- a/lib/memcached_verbosity.c +++ b/lib/memcached_verbosity.c @@ -7,29 +7,25 @@ memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity) memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - rc= memcached_connect(ptr, 0); - - if (rc != MEMCACHED_SUCCESS) - rc= MEMCACHED_SOME_ERRORS; - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "verbosity %u\r\n", verbosity); if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; + rc= MEMCACHED_SUCCESS; for (x= 0; x < ptr->number_of_hosts; x++) { - memcached_return rc; + memcached_return rrc; - if ((memcached_io_write(ptr, x, buffer, send_length, 1)) == -1) + rrc= memcached_do(ptr, x, buffer, send_length, 1); + if (rrc != MEMCACHED_SUCCESS) { + rc= MEMCACHED_SOME_ERRORS; continue; - return MEMCACHED_SOME_ERRORS; } - rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x); - - if (rc != MEMCACHED_SUCCESS) + rrc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x); + if (rrc != MEMCACHED_SUCCESS) rc= MEMCACHED_SOME_ERRORS; }