โ˜… Final Challenge

โ€บ Full Database Engine

75 pts

Combine everything into a complete database engine driven by a command parser โ€” the front door that turns text into the operations you've built:

  1. In-memory table with dynamic growth.
  2. Atomic, durable file persistence (write-temp + fsync + rename).
  3. Predicates and queries to filter rows.
  4. Update and delete operations.
  5. ID indexing for fast lookups โ€” kept consistent through every operation.
  6. Transactions with begin/commit/rollback.

You'll implement db_execute, a command parser that reads strings like INSERT Alice 30 and dispatches to the right operations. This simulates a real database's execution pipeline: parse the text, validate it, plan (here: pick the operation), execute, and expose results. It's a miniature of what happens to every SQL statement โ€” SQLite compiles SQL to bytecode for a virtual machine; our "bytecode" is just a dispatch on the verb.

All the building blocks from previous lessons are provided complete. Your work is the glue โ€” and the glue is where consistency lives: LOAD must rebuild the index for the new table; ROLLBACK must rebuild it too (the snapshot restored old row positions, so the index is stale โ€” this is Consistency from the ACID lesson, enforced by you); SELECT must free the previous result before storing a new one (no leaks in a long-running process).

Command Format

  • INSERT <name> <age> โ€” insert a row, record its ID in last_insert_id
  • SELECT <column> <op> <value> โ€” query with a predicate (op is one of == < > <= >=); store matches in db->result, freeing any previous result first
  • UPDATE <id> <name> <age> โ€” update a row by ID (via the index)
  • DELETE <id> โ€” delete a row by ID (via the index, with fix-up)
  • BEGIN / COMMIT / ROLLBACK โ€” transaction control; ROLLBACK rebuilds the index after restoring the snapshot
  • SAVE <path> โ€” atomically persist the table
  • LOAD <path> โ€” load a table from disk, replace the in-memory table, rebuild the index
  • COUNT โ€” succeed (row count is readable as db->data->count)

Return 0 on success, -1 for unknown/malformed commands or failed operations.

Log in to submit a solution and earn points.