MediumStateful DesignPython 3

Transactional KV Store

Implement nested in-memory transactions with read-your-writes, tombstones, commit, and rollback.

40m2 sample tests4 hidden tests

Implement TransactionalKV, an in-memory key/value store with nested transactions.

Requirements

  • set(key, value) writes a value.
  • get(key) returns the visible value or None.
  • delete(key) removes the visible value.
  • begin() opens a transaction layer.
  • commit() merges the current transaction into its parent layer or the base store.
  • rollback() discards the current transaction.
  • Reads inside a transaction must see writes and deletes from that transaction first.
  • Deleting a key inside a transaction must hide lower-layer values until rollback or commit.
  • Calling commit() or rollback() without an active transaction raises ValueError.

Example

python
1store = TransactionalKV() 2store.set("model", "fast") 3store.begin() 4store.set("model", "strong") 5assert store.get("model") == "strong" 6store.rollback() 7assert store.get("model") == "fast"

Constraints

  • Keep it single-process and in memory.
  • Preserve nested transaction semantics.
  • Don't use a database library.

Editor