Design and implement an in-memory key–value store that supports basic operations plus transactions.
Implement the following operations:
get(key) -> value | null
key
, or
null
/
None
if it does not exist.
put(key, value)
key
to
value
.
delete(key)
key
if it exists.
Add transactional operations:
begin()
— starts a new transaction scope (transactions may be nested).
commit() -> bool
— commits the
current
transaction.
false
(or throws) if there is no active transaction.
rollback() -> bool
— rolls back the
current
transaction.
false
(or throws) if there is no active transaction.
After commit, changes in the committed transaction become visible in the parent transaction (or globally if committing the outermost transaction). After rollback, all changes made since the last begin() are undone.
Complexity requirement: Each operation should run in O(1) time on average (constant-time hash operations allowed). You may assume keys and values fit in memory.
Extend your design to support multi-threaded access:
get/put/delete/begin/commit/rollback
concurrently.
delete
interacts with transactions (e.g., deleting a key inside a transaction should be reversible by rollback).