Command Interface & Putting It Together
Final piece: a REPL — Read, Eval, Print, Loop. Pull a command off stdin, route it to the hash table, print whatever came back.
Parsing Commands
#include <stdio.h>#include <string.h>
typedef enum { CMD_SET, CMD_GET, CMD_DEL, CMD_SAVE, CMD_LOAD, CMD_QUIT, CMD_UNKNOWN} CommandType;
typedef struct { CommandType type; char key[256]; char value[256];} Command;
Command parse_command(const char *line) { Command cmd = { .type = CMD_UNKNOWN };
if (strncmp(line, "SET ", 4) == 0) { cmd.type = CMD_SET; sscanf(line + 4, "%255s %255[^\n]", cmd.key, cmd.value); } else if (strncmp(line, "GET ", 4) == 0) { cmd.type = CMD_GET; sscanf(line + 4, "%255s", cmd.key); } else if (strncmp(line, "DEL ", 4) == 0) { cmd.type = CMD_DEL; sscanf(line + 4, "%255s", cmd.key); } else if (strcmp(line, "SAVE") == 0) { cmd.type = CMD_SAVE; } else if (strcmp(line, "LOAD") == 0) { cmd.type = CMD_LOAD; } else if (strcmp(line, "QUIT") == 0) { cmd.type = CMD_QUIT; }
return cmd;}The runner can't type at a REPL for you (it has no stdin), so here's parse_command on its own with a list of lines fed in. Predict what each one parses to before you run it. At least one result should bother you.
SET lonely parses as a SET with an empty value, so the REPL would happily print OK and store "". Commands are case-sensitive, and a trailing space turns SAVE into gibberish as far as strcmp is concerned. None of these is a crash, but all of them are the kind of surprise users file bugs about.
The REPL
void run_repl(HashTable *ht, const char *dbfile) { char line[512];
printf("> "); while (fgets(line, sizeof(line), stdin)) { line[strcspn(line, "\n")] = '\0';
Command cmd = parse_command(line);
switch (cmd.type) { case CMD_SET: ht_set(ht, cmd.key, cmd.value); printf("OK\n"); break; case CMD_GET: { char *val = ht_get(ht, cmd.key); printf("%s\n", val ? val : "(nil)"); break; } case CMD_DEL: if (ht_delete(ht, cmd.key) == 0) printf("OK\n"); else printf("(nil)\n"); break; case CMD_SAVE: { int n = ht_save(ht, dbfile); printf("Saved %d entries to disk\n", n); break; } case CMD_LOAD: { int n = ht_load(ht, dbfile); printf("Loaded %d entries from disk\n", n); break; } case CMD_QUIT: return; default: printf("Unknown command\n"); }
printf("> "); }}DEL on a key that isn't there prints (nil), matching what GET says for the same key; the design in lesson 1 only showed deleting a key that exists. Redis takes the same line, answering with the number of keys it actually removed. And SET ignores ht_set's return value here, so an out-of-memory failure would still print OK; a sturdier REPL checks it and prints an error instead.
LOAD merges rather than replaces: every entry in the file is ht_set on top of what's already in memory, so keys you've added since the last save survive, and keys you've changed get overwritten by the saved copy.
The Main Function
int main(int argc, char *argv[]) { const char *dbfile = "store.db"; if (argc > 1) dbfile = argv[1];
HashTable *ht = ht_create();
// Try to load existing data int loaded = ht_load(ht, dbfile); if (loaded >= 0) { printf("Loaded %d entries from %s\n", loaded, dbfile); }
run_repl(ht, dbfile);
ht_destroy(ht); return 0;}This capstone demonstrates the core pattern of C programming: define data structures, write functions that operate on them, manage memory explicitly, and compose everything into a working program. Every concept from the track - pointers, structs, malloc/free, file I/O - comes together here.
What You Have Built
Your key-value store is a tiny database. Redis started life on the same three pillars:
- An in-memory hash table for fast lookups
- File persistence for durability
- A text-based command protocol
You now know, at the bottom of the stack, how data storage systems work. Every higher-level language and library you'll ever pull in is built on top of these same primitives.
Challenges
Implement the DJB2 hash function and test it on a known input.