From c2d698dbacbbfa7285d2ae8aab06810b4c0e4047 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Wed, 24 Jul 2013 09:20:43 -0400 Subject: [PATCH] added delete command back in, some minor cleanup --- src/handlers.c | 33 ++++++++++++++++++++++++--------- src/handlers.h | 1 + 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/handlers.c b/src/handlers.c index 6fff695..a62dba9 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -46,6 +46,7 @@ void handle_stats(KCLIST* tokens, FILE* client){ kclistdel(parts); strcat(status_buf, buf); } + kclistdel(stats); sprintf(out, STATS_FORMAT, connections, total, hits, misses, hit_ratio, size, status_buf); fputs(out, client); } @@ -56,12 +57,24 @@ void handle_version(KCLIST* tokens, FILE* client){ fputs(out, client); } -void handle_flush(KCLIST* tokens, FILE* client){ - if(kcdbclear(db)){ - fputs("OK\r\n", client); +void handle_delete(KCLIST* tokens, FILE* client){ + if(kclistcount(tokens)){ + char* key; + list_shift(tokens, &key); + if(key != NULL){ + if(kcdbremove(db, key, strlen(key))){ + fputs("DELETED\r\n", client); + return; + } + } } + fputs("NOT_FOUND\r\n", client); } +void handle_flush(KCLIST* tokens, FILE* client){ + kcdbclear(db); + fputs("OK\r\n", client); +} void handle_get(KCLIST* tokens, FILE* client){ if(kclistcount(tokens)){ @@ -91,12 +104,12 @@ void handle_get(KCLIST* tokens, FILE* client){ kcfree(result_buffer); } - if(strlen(value)){ - ++hits; - sprintf(out, "VALUE %s 0 %d\r\n%s\r\nEND\r\n", key, (int)strlen(value), value); - } else if(strcmp(value, "0") == 0){ + if(strcmp(value, "0") == 0){ ++misses; sprintf(out, "END\r\n"); + } else if(strlen(value)){ + ++hits; + sprintf(out, "VALUE %s 0 %d\r\n%s\r\nEND\r\n", key, (int)strlen(value), value); } else{ ++misses; sprintf(out, "END\r\n"); @@ -108,7 +121,7 @@ void handle_get(KCLIST* tokens, FILE* client){ fputs(out, client); free(key); } else{ - fputs("INVALID GET COMMAND: GET \r\n", client); + fputs("ERROR: GET \r\n", client); } } @@ -127,6 +140,8 @@ int handle_command(char* buffer, FILE* client){ handle_get(tokens, client); } else if(strcmp(command, "stats") == 0){ handle_stats(tokens, client); + } else if(strcmp(command, "delete") == 0){ + handle_delete(tokens, client); } else if(strcmp(command, "flush_all") == 0){ handle_flush(tokens, client); } else if(strcmp(command, "version") == 0){ @@ -137,7 +152,7 @@ int handle_command(char* buffer, FILE* client){ status = -1; } else{ char out[1024]; - sprintf(out, "UNKNOWN COMMAND: %s\r\n", command); + sprintf(out, "ERROR: UNKNOWN COMMAND %s\r\n", command); fputs(out, client); } free(command); diff --git a/src/handlers.h b/src/handlers.h index 2164c51..6f63fff 100644 --- a/src/handlers.h +++ b/src/handlers.h @@ -10,5 +10,6 @@ void handle_version(KCLIST* tokens, FILE* client); void handle_flush(KCLIST* tokens, FILE* client); void handle_delete(KCLIST* tokens, FILE* client); void handle_get(KCLIST* tokens, FILE* client); +void handle_help(KCLIST* tokens, FILE* client); int handle_command(char* buffer, FILE* client); #endif