โ Final Challenge
โบ Full Database Engine
75 ptsCombine everything into a complete database engine driven by a command parser โ the front door that turns text into the operations you've built:
- In-memory table with dynamic growth.
- Atomic, durable file persistence (write-temp + fsync + rename).
- Predicates and queries to filter rows.
- Update and delete operations.
- ID indexing for fast lookups โ kept consistent through every operation.
- 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 inlast_insert_idSELECT <column> <op> <value>โ query with a predicate (opis one of==<><=>=); store matches indb->result, freeing any previous result firstUPDATE <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 snapshotSAVE <path>โ atomically persist the tableLOAD <path>โ load a table from disk, replace the in-memory table, rebuild the indexCOUNTโ succeed (row count is readable asdb->data->count)
Return 0 on success, -1 for unknown/malformed commands or failed operations.
Log in to submit a solution and earn points.