From: Date: Wed, 19 Sep 2007 05:22:03 +0000 (+0200) Subject: Increment and decrement now works. X-Git-Tag: 0.2~25 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=3efc2e6fb6a17e8eb52dbe3590a5dcb063b30537;p=awesomized%2Flibmemcached Increment and decrement now works. Multi host support added. New code for stats (and we now fetch stats array) Further library split. --- diff --git a/configure.in b/configure.in index c9605537..dd64f6b2 100644 --- a/configure.in +++ b/configure.in @@ -1,7 +1,7 @@ AC_INIT(src/main.c) AC_CONFIG_AUX_DIR(config) AM_CONFIG_HEADER(config.h) -AM_INIT_AUTOMAKE("?", 0.1 ) +AM_INIT_AUTOMAKE("?", 0.2 ) AC_PROG_CC AC_PROG_LIBTOOL diff --git a/include/memcached.h b/include/memcached.h index b57b3941..461e7c96 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -56,6 +56,7 @@ typedef enum { MEMCACHED_NOTFOUND, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_PARTIAL_READ, + MEMCACHED_SOME_ERRORS, } memcached_return; typedef enum { @@ -123,11 +124,15 @@ memcached_return memcached_replace(memcached_st *ptr, char *key, size_t key_leng uint16_t flags); memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_length, time_t expiration); -memcached_return memcached_increment(memcached_st *ptr, char *key, size_t key_length, - unsigned int count); -memcached_return memcached_decrement(memcached_st *ptr, char *key, size_t key_length, - unsigned int count); -memcached_stat_st **memcached_stat(memcached_st *ptr, memcached_return *error); +memcached_return memcached_increment(memcached_st *ptr, + char *key, size_t key_length, + unsigned int offset, + unsigned int *value); +memcached_return memcached_decrement(memcached_st *ptr, + char *key, size_t key_length, + unsigned int offset, + unsigned int *value); +memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error); memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, char *hostname, unsigned int port); memcached_return memcached_flush(memcached_st *ptr, time_t expiration); @@ -143,8 +148,9 @@ char *memcached_strerror(memcached_st *ptr, memcached_return rc); /* These are all private, do not use. */ memcached_return memcached_connect(memcached_st *ptr); memcached_return memcached_response(memcached_st *ptr, - char *buffer, - size_t buffer_length); + char *buffer, size_t buffer_length, + unsigned int server_key); +unsigned int memcached_generate_hash(char *key, size_t key_length); #ifdef __cplusplus } diff --git a/lib/Makefile.am b/lib/Makefile.am index 9cd6688f..43e4a27f 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -8,4 +8,9 @@ libmemcached_la_SOURCES = memcached.c \ memcached_get.c \ memcached_storage.c \ memcached_delete.c \ + memcached_hash.c \ + memcached_auto.c \ + memcached_verbosity.c \ + memcached_quit.c \ + memcached_flush.c \ memcached_stats.c diff --git a/lib/memcached.c b/lib/memcached.c index 66a96f04..3cbac0b2 100644 --- a/lib/memcached.c +++ b/lib/memcached.c @@ -23,72 +23,6 @@ memcached_st *memcached_init(memcached_st *ptr) return ptr; } -memcached_return memcached_increment(memcached_st *ptr, char *key, size_t key_length, - unsigned int count) -{ - return MEMCACHED_SUCCESS; -} - -memcached_return memcached_decrement(memcached_st *ptr, char *key, size_t key_length, - unsigned int count) -{ - return MEMCACHED_SUCCESS; -} - -memcached_return memcached_flush(memcached_st *ptr, time_t expiration) -{ - size_t send_length; - memcached_return rc; - char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - - rc= memcached_connect(ptr); - - if (expiration) - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, - "flush_all %u\r\n", expiration); - else - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, - "flush_all\r\n"); - if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1)) - { - fprintf(stderr, "failed flush_all TCP\n"); - - return MEMCACHED_WRITE_FAILURE; - } - - return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); -} - -memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity) -{ - size_t send_length; - memcached_return rc; - char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - - rc= memcached_connect(ptr); - - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, - "verbosity %u\r\n", verbosity); - - if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1)) - { - fprintf(stderr, "failed verbosity\n"); - - return MEMCACHED_WRITE_FAILURE; - } - - return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); -} - -/* - When this is implemented you will be able to remove single hosts - from your current pool of hosts. -*/ -memcached_return memcached_quit(memcached_st *ptr, char *hostname, unsigned port) -{ - return MEMCACHED_SUCCESS; -} - void memcached_deinit(memcached_st *ptr) { unsigned int x; @@ -113,46 +47,3 @@ void memcached_deinit(memcached_st *ptr) else memset(ptr, 0, sizeof(memcached_st)); } - -char *memcached_strerror(memcached_st *ptr, memcached_return rc) -{ - switch (rc) - { - case MEMCACHED_SUCCESS: - return "SUCCESS"; - case MEMCACHED_FAILURE: - return "FAILURE"; - case MEMCACHED_HOST_LOCKUP_FAILURE: - return "HOSTNAME LOOKUP FAILURE"; - case MEMCACHED_CONNECTION_FAILURE: - return "CONNECTION FAILURE"; - case MEMCACHED_CONNECTION_BIND_FAILURE: - return "CONNECTION BIND FAILURE"; - case MEMCACHED_READ_FAILURE: - return "READ FAILURE"; - case MEMCACHED_UNKNOWN_READ_FAILURE: - return "UNKNOWN READ FAILURE"; - case MEMCACHED_PROTOCOL_ERROR: - return "PROTOCOL ERROR"; - case MEMCACHED_CLIENT_ERROR: - return "CLIENT ERROR"; - case MEMCACHED_SERVER_ERROR: - return "SERVER ERROR"; - case MEMCACHED_WRITE_FAILURE: - return "WRITE FAILURE"; - case MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE: - return "CONNECTION SOCKET CREATE FAILURE"; - case MEMCACHED_DATA_EXISTS: - return "CONNECTION DATA EXISTS"; - case MEMCACHED_DATA_DOES_NOT_EXIST: - return "CONNECTION DATA DOES NOT EXIST"; - case MEMCACHED_NOTSTORED: - return "NOT STORED"; - case MEMCACHED_NOTFOUND: - return "NOT FOUND"; - case MEMCACHED_MEMORY_ALLOCATION_FAILURE: - return "MEMORY ALLOCATION FAILURE"; - case MEMCACHED_PARTIAL_READ: - return "PARTIAL READ"; - }; -} diff --git a/lib/memcached_auto.c b/lib/memcached_auto.c new file mode 100644 index 00000000..268f95b8 --- /dev/null +++ b/lib/memcached_auto.c @@ -0,0 +1,69 @@ +#include + +static memcached_return memcached_auto(memcached_st *ptr, + char *verb, + char *key, size_t key_length, + unsigned int offset, + unsigned int *value) +{ + size_t send_length; + memcached_return rc; + char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + unsigned int server_key; + + rc= memcached_connect(ptr); + + if (rc != MEMCACHED_SUCCESS) + return rc; + + server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts; + + send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, + "%s %.*s %u\r\n", verb, + key_length, key, + offset); + + if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) + { + fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key); + + return MEMCACHED_WRITE_FAILURE; + } + + memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE); + send_length= read(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); + + if (!memcmp(buffer, "ERROR\r\n", MEMCACHED_DEFAULT_COMMAND_SIZE)) + { + *value= 0; + rc= MEMCACHED_PROTOCOL_ERROR; + } + else if (!memcmp(buffer, "NOT_FOUND\r\n", MEMCACHED_DEFAULT_COMMAND_SIZE)) + { + *value= 0; + rc= MEMCACHED_NOTFOUND; + } + else + { + *value= strtol(buffer, (char **)NULL, 10); + rc= MEMCACHED_SUCCESS; + } + + return rc; +} + +memcached_return memcached_increment(memcached_st *ptr, + char *key, size_t key_length, + unsigned int offset, + unsigned int *value) +{ + return memcached_auto(ptr, "incr", key, key_length, offset, value); +} + +memcached_return memcached_decrement(memcached_st *ptr, + char *key, size_t key_length, + unsigned int offset, + unsigned int *value) +{ + return memcached_auto(ptr, "decr", key, key_length, offset, value); +} diff --git a/lib/memcached_delete.c b/lib/memcached_delete.c index df16d2f4..e601e6fa 100644 --- a/lib/memcached_delete.c +++ b/lib/memcached_delete.c @@ -6,21 +6,27 @@ memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_lengt size_t send_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + unsigned int server_key; rc= memcached_connect(ptr); + if (rc != MEMCACHED_SUCCESS) + return rc; + + server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts; + if (expiration) send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "delete %.*s %u\r\n", key_length, key, expiration); else send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "delete %.*s\r\n", key_length, key); - if ((write(ptr->hosts[0].fd, buffer, send_length) == -1)) + if ((write(ptr->hosts[server_key].fd, buffer, send_length) == -1)) { fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key); return MEMCACHED_WRITE_FAILURE; } - return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); + return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key); } diff --git a/lib/memcached_flush.c b/lib/memcached_flush.c new file mode 100644 index 00000000..f7527a7c --- /dev/null +++ b/lib/memcached_flush.c @@ -0,0 +1,37 @@ +#include + +memcached_return memcached_flush(memcached_st *ptr, time_t expiration) +{ + unsigned int x; + size_t send_length; + memcached_return rc; + char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + + rc= memcached_connect(ptr); + + if (rc != MEMCACHED_SUCCESS) + rc= MEMCACHED_SOME_ERRORS; + + for (x= 0; x < ptr->number_of_hosts; x++) + { + if (expiration) + send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, + "flush_all %u\r\n", expiration); + else + send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, + "flush_all\r\n"); + if ((send(ptr->hosts[x].fd, buffer, send_length, 0) == -1)) + { + fprintf(stderr, "failed flush_all TCP\n"); + + return MEMCACHED_WRITE_FAILURE; + } + + rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x); + + if (rc != MEMCACHED_SUCCESS) + rc= MEMCACHED_SOME_ERRORS; + } + + return rc; +} diff --git a/lib/memcached_get.c b/lib/memcached_get.c index c535a7ab..2dcdcccd 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -8,18 +8,21 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, size_t send_length; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; char *string_ptr; + unsigned int server_key; *error= memcached_connect(ptr); if (*error != MEMCACHED_SUCCESS) return NULL; + server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts; + send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n", key_length, key); if (*error != MEMCACHED_SUCCESS) return NULL; - if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1)) + if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) { fprintf(stderr, "failed fetch on %.*s TCP\n", key_length+1, key); *error= MEMCACHED_WRITE_FAILURE; @@ -27,7 +30,7 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, } memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE); - *error= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); + *error= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key); if (*error == MEMCACHED_SUCCESS) { @@ -79,7 +82,7 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, need_to_read= *value_length - need_to_copy; - read_length= read(ptr->hosts[0].fd, pos_ptr, need_to_read); + read_length= read(ptr->hosts[server_key].fd, pos_ptr, need_to_read); if (read_length != need_to_read) { free(value); diff --git a/lib/memcached_hash.c b/lib/memcached_hash.c new file mode 100644 index 00000000..74cd6360 --- /dev/null +++ b/lib/memcached_hash.c @@ -0,0 +1,20 @@ +#include + +unsigned int memcached_generate_hash(char *key, size_t key_length) +{ + unsigned int x = key_length; + char *ptr = key; + unsigned int value = 0; + + while (x--) + { + value += *ptr++; + value += (value << 10); + value ^= (value >> 6); + } + value += (value << 3); + value ^= (value >> 11); + value += (value << 15); + + return value == 0 ? 1 : value; +} diff --git a/lib/memcached_quit.c b/lib/memcached_quit.c new file mode 100644 index 00000000..8d0e5551 --- /dev/null +++ b/lib/memcached_quit.c @@ -0,0 +1,10 @@ +#include + +/* + When this is implemented you will be able to remove single hosts + from your current pool of hosts. +*/ +memcached_return memcached_quit(memcached_st *ptr, char *hostname, unsigned port) +{ + return MEMCACHED_SUCCESS; +} diff --git a/lib/memcached_response.c b/lib/memcached_response.c index 99020559..ec29f0aa 100644 --- a/lib/memcached_response.c +++ b/lib/memcached_response.c @@ -8,12 +8,13 @@ #include memcached_return memcached_response(memcached_st *ptr, - char *buffer, size_t buffer_length) + char *buffer, size_t buffer_length, + unsigned int server_key) { size_t send_length; memset(buffer, 0, buffer_length); - send_length= read(ptr->hosts[0].fd, buffer, buffer_length); + send_length= read(ptr->hosts[server_key].fd, buffer, buffer_length); if (send_length) switch(buffer[0]) diff --git a/lib/memcached_stats.c b/lib/memcached_stats.c index c3371d1d..2511532c 100644 --- a/lib/memcached_stats.c +++ b/lib/memcached_stats.c @@ -115,22 +115,16 @@ static void set_data(memcached_stat_st *stat, char *key, char *value) } } -memcached_stat_st **memcached_stat(memcached_st *ptr, memcached_return *error) +static memcached_return memcached_stats_fetch(memcached_st *ptr, + memcached_stat_st *stat, + char *args, + unsigned int server_key) { - return NULL; -} - -memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, - char *hostname, unsigned int port) -{ - size_t send_length; memcached_return rc; char buffer[HUGE_STRING_LEN]; - memcached_st memc; - - memcached_init(&memc); + size_t send_length; - rc= memcached_connect(&memc); + rc= memcached_connect(ptr); if (rc != MEMCACHED_SUCCESS) return rc; @@ -142,15 +136,14 @@ memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, send_length= snprintf(buffer, HUGE_STRING_LEN, "stats\r\n"); - if ((send(memc.hosts[0].fd, buffer, send_length, 0) == -1)) + if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) { fprintf(stderr, "failed on stats\n"); - rc= MEMCACHED_WRITE_FAILURE; - goto error; + return MEMCACHED_WRITE_FAILURE; } - rc= memcached_response(&memc, buffer, HUGE_STRING_LEN); + rc= memcached_response(ptr, buffer, HUGE_STRING_LEN, 0); if (rc == MEMCACHED_SUCCESS) { @@ -176,7 +169,60 @@ memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, } } -error: + return rc; +} + +memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error) +{ + unsigned int x; + memcached_return rc; + memcached_stat_st *stats; + rc= memcached_connect(ptr); + + if (rc != MEMCACHED_SUCCESS) + { + *error= rc; + return NULL; + } + + stats= (memcached_stat_st *)malloc(sizeof(memcached_st)*(ptr->number_of_hosts+1)); + if (stats) + { + *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + return NULL; + } + memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts+1)); + + for (x= 0; x < ptr->number_of_hosts; x++) + { + rc= memcached_stats_fetch(ptr, stats+x, args, x); + if (rc != MEMCACHED_SUCCESS) + rc= MEMCACHED_SOME_ERRORS; + } + + *error= x == 0 ? MEMCACHED_SUCCESS : rc; + return stats; +} + +memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, + char *hostname, unsigned int port) +{ + size_t send_length; + memcached_return rc; + char buffer[HUGE_STRING_LEN]; + memcached_st memc; + + memcached_init(&memc); + + memcached_server_add(&memc, hostname, port); + + rc= memcached_connect(&memc); + + if (rc != MEMCACHED_SUCCESS) + return rc; + + rc= memcached_stats_fetch(&memc, stat, args, 0); + memcached_deinit(&memc); return rc; diff --git a/lib/memcached_storage.c b/lib/memcached_storage.c index 40d36394..3df4913a 100644 --- a/lib/memcached_storage.c +++ b/lib/memcached_storage.c @@ -19,16 +19,19 @@ static memcached_return memcached_send(memcached_st *ptr, size_t send_length; memcached_return rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + unsigned int server_key; rc= memcached_connect(ptr); if (rc != MEMCACHED_SUCCESS) return rc; + server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts; + send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%s %.*s %u %u %u\r\n", verb, key_length, key, flags, expiration, value_length); - if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1)) + if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) { fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key); @@ -38,14 +41,14 @@ static memcached_return memcached_send(memcached_st *ptr, send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "%.*s\r\n", value_length, value); - if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1)) + if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1)) { fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key); return MEMCACHED_WRITE_FAILURE; } - send_length= read(ptr->hosts[0].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); + send_length= read(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE); if (send_length && buffer[0] == 'S') /* STORED */ return MEMCACHED_SUCCESS; diff --git a/lib/memcached_strerror.c b/lib/memcached_strerror.c new file mode 100644 index 00000000..5c7dadfb --- /dev/null +++ b/lib/memcached_strerror.c @@ -0,0 +1,46 @@ +#include + +char *memcached_strerror(memcached_st *ptr, memcached_return rc) +{ + switch (rc) + { + case MEMCACHED_SUCCESS: + return "SUCCESS"; + case MEMCACHED_FAILURE: + return "FAILURE"; + case MEMCACHED_HOST_LOCKUP_FAILURE: + return "HOSTNAME LOOKUP FAILURE"; + case MEMCACHED_CONNECTION_FAILURE: + return "CONNECTION FAILURE"; + case MEMCACHED_CONNECTION_BIND_FAILURE: + return "CONNECTION BIND FAILURE"; + case MEMCACHED_READ_FAILURE: + return "READ FAILURE"; + case MEMCACHED_UNKNOWN_READ_FAILURE: + return "UNKNOWN READ FAILURE"; + case MEMCACHED_PROTOCOL_ERROR: + return "PROTOCOL ERROR"; + case MEMCACHED_CLIENT_ERROR: + return "CLIENT ERROR"; + case MEMCACHED_SERVER_ERROR: + return "SERVER ERROR"; + case MEMCACHED_WRITE_FAILURE: + return "WRITE FAILURE"; + case MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE: + return "CONNECTION SOCKET CREATE FAILURE"; + case MEMCACHED_DATA_EXISTS: + return "CONNECTION DATA EXISTS"; + case MEMCACHED_DATA_DOES_NOT_EXIST: + return "CONNECTION DATA DOES NOT EXIST"; + case MEMCACHED_NOTSTORED: + return "NOT STORED"; + case MEMCACHED_NOTFOUND: + return "NOT FOUND"; + case MEMCACHED_MEMORY_ALLOCATION_FAILURE: + return "MEMORY ALLOCATION FAILURE"; + case MEMCACHED_PARTIAL_READ: + return "PARTIAL READ"; + case MEMCACHED_SOME_ERRORS: + return "SOME ERRORS WERE REPORTED "; + }; +} diff --git a/lib/memcached_verbosity.c b/lib/memcached_verbosity.c new file mode 100644 index 00000000..4e586cfb --- /dev/null +++ b/lib/memcached_verbosity.c @@ -0,0 +1,36 @@ +#include + +memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity) +{ + unsigned int x; + size_t send_length; + memcached_return rc; + char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + + rc= memcached_connect(ptr); + + if (rc != MEMCACHED_SUCCESS) + rc= MEMCACHED_SOME_ERRORS; + + send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, + "verbosity %u\r\n", verbosity); + + for (x= 0; x < ptr->number_of_hosts; x++) + { + memcached_return rc; + + if ((send(ptr->hosts[x].fd, buffer, send_length, 0) == -1)) + { + fprintf(stderr, "failed verbosity\n"); + + return MEMCACHED_WRITE_FAILURE; + } + + rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x); + + if (rc != MEMCACHED_SUCCESS) + rc= MEMCACHED_SOME_ERRORS; + } + + return rc; +} diff --git a/tests/test.c b/tests/test.c index 2edcacd2..9c93a87b 100644 --- a/tests/test.c +++ b/tests/test.c @@ -27,7 +27,6 @@ void allocation_test(void) void set_test(void) { memcached_st *memc; - char *foo; memcached_return rc; char *key= "foo"; char *value= "when we sanitize"; @@ -38,7 +37,6 @@ void set_test(void) value, strlen(value), (time_t)0, (uint16_t)0); assert(rc == MEMCACHED_SUCCESS); - assert(foo); memcached_deinit(memc); } @@ -46,7 +44,6 @@ void set_test(void) void add_test(void) { memcached_st *memc; - char *foo; memcached_return rc; char *key= "foo"; char *value= "when we sanitize"; @@ -57,7 +54,6 @@ void add_test(void) value, strlen(value), (time_t)0, (uint16_t)0); assert(rc == MEMCACHED_NOTSTORED); - assert(foo); memcached_deinit(memc); } @@ -65,7 +61,6 @@ void add_test(void) void replace_test(void) { memcached_st *memc; - char *foo; memcached_return rc; char *key= "foo"; char *value= "when we sanitize"; @@ -76,7 +71,6 @@ void replace_test(void) value, strlen(value), (time_t)0, (uint16_t)0); assert(rc == MEMCACHED_SUCCESS); - assert(foo); memcached_deinit(memc); } @@ -84,7 +78,6 @@ void replace_test(void) void delete_test(void) { memcached_st *memc; - char *foo; memcached_return rc; char *key= "foo"; char *value= "when we sanitize"; @@ -95,11 +88,9 @@ void delete_test(void) value, strlen(value), (time_t)0, (uint16_t)0); assert(rc == MEMCACHED_SUCCESS); - assert(foo); rc= memcached_delete(memc, key, strlen(key), (time_t)0); assert(rc == MEMCACHED_SUCCESS); - assert(foo); memcached_deinit(memc); } @@ -141,7 +132,6 @@ void get_test(void) void get_test2(void) { memcached_st *memc; - char *foo; memcached_return rc; char *key= "foo"; char *value= "when we sanitize"; @@ -176,6 +166,62 @@ void stats_hostname_test(void) MEMCACHED_DEFAULT_PORT); } +void increment_test(void) +{ + memcached_st *memc; + unsigned int new_number; + memcached_return rc; + char *key= "number"; + char *value= "0"; + + memc= memcached_init(NULL); + assert(memc); + rc= memcached_set(memc, key, strlen(key), + value, strlen(value), + (time_t)0, (uint16_t)0); + assert(rc == MEMCACHED_SUCCESS); + + rc= memcached_increment(memc, key, strlen(key), + 1, &new_number); + assert(rc == MEMCACHED_SUCCESS); + assert(new_number == 1); + + rc= memcached_increment(memc, key, strlen(key), + 1, &new_number); + assert(rc == MEMCACHED_SUCCESS); + assert(new_number == 2); + + memcached_deinit(memc); +} + +void decrement_test(void) +{ + memcached_st *memc; + unsigned int new_number; + memcached_return rc; + char *key= "number"; + char *value= "3"; + + memc= memcached_init(NULL); + assert(memc); + rc= memcached_set(memc, key, strlen(key), + value, strlen(value), + (time_t)0, (uint16_t)0); + assert(rc == MEMCACHED_SUCCESS); + + rc= memcached_decrement(memc, key, strlen(key), + 1, &new_number); + assert(rc == MEMCACHED_SUCCESS); + assert(new_number == 2); + + rc= memcached_decrement(memc, key, strlen(key), + 1, &new_number); + assert(rc == MEMCACHED_SUCCESS); + assert(new_number == 1); + + memcached_deinit(memc); +} + int main(void) { /* Clean the server before beginning testing */ @@ -193,6 +239,9 @@ int main(void) get_test2(); stats_hostname_test(); + increment_test(); + decrement_test(); + /* Clean up whatever we might have left */ flush_test(); return 0;