Browse Source

added delete command back in, some minor cleanup

master
Brett Langdon 13 years ago
parent
commit
c2d698dbac
2 changed files with 25 additions and 9 deletions
  1. +24
    -9
      src/handlers.c
  2. +1
    -0
      src/handlers.h

+ 24
- 9
src/handlers.c View File

@ -46,6 +46,7 @@ void handle_stats(KCLIST* tokens, FILE* client){
kclistdel(parts); kclistdel(parts);
strcat(status_buf, buf); strcat(status_buf, buf);
} }
kclistdel(stats);
sprintf(out, STATS_FORMAT, connections, total, hits, misses, hit_ratio, size, status_buf); sprintf(out, STATS_FORMAT, connections, total, hits, misses, hit_ratio, size, status_buf);
fputs(out, client); fputs(out, client);
} }
@ -56,12 +57,24 @@ void handle_version(KCLIST* tokens, FILE* client){
fputs(out, 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){ void handle_get(KCLIST* tokens, FILE* client){
if(kclistcount(tokens)){ if(kclistcount(tokens)){
@ -91,12 +104,12 @@ void handle_get(KCLIST* tokens, FILE* client){
kcfree(result_buffer); 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; ++misses;
sprintf(out, "END\r\n"); 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{ } else{
++misses; ++misses;
sprintf(out, "END\r\n"); sprintf(out, "END\r\n");
@ -108,7 +121,7 @@ void handle_get(KCLIST* tokens, FILE* client){
fputs(out, client); fputs(out, client);
free(key); free(key);
} else{ } else{
fputs("INVALID GET COMMAND: GET <KEY>\r\n", client);
fputs("ERROR: GET <KEY>\r\n", client);
} }
} }
@ -127,6 +140,8 @@ int handle_command(char* buffer, FILE* client){
handle_get(tokens, client); handle_get(tokens, client);
} else if(strcmp(command, "stats") == 0){ } else if(strcmp(command, "stats") == 0){
handle_stats(tokens, client); handle_stats(tokens, client);
} else if(strcmp(command, "delete") == 0){
handle_delete(tokens, client);
} else if(strcmp(command, "flush_all") == 0){ } else if(strcmp(command, "flush_all") == 0){
handle_flush(tokens, client); handle_flush(tokens, client);
} else if(strcmp(command, "version") == 0){ } else if(strcmp(command, "version") == 0){
@ -137,7 +152,7 @@ int handle_command(char* buffer, FILE* client){
status = -1; status = -1;
} else{ } else{
char out[1024]; char out[1024];
sprintf(out, "UNKNOWN COMMAND: %s\r\n", command);
sprintf(out, "ERROR: UNKNOWN COMMAND %s\r\n", command);
fputs(out, client); fputs(out, client);
} }
free(command); free(command);


+ 1
- 0
src/handlers.h View File

@ -10,5 +10,6 @@ void handle_version(KCLIST* tokens, FILE* client);
void handle_flush(KCLIST* tokens, FILE* client); void handle_flush(KCLIST* tokens, FILE* client);
void handle_delete(KCLIST* tokens, FILE* client); void handle_delete(KCLIST* tokens, FILE* client);
void handle_get(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); int handle_command(char* buffer, FILE* client);
#endif #endif

Loading…
Cancel
Save