Implement a key-value database that stores to a file for durability.
How can this be made transactional?
How to make it thread-safe?
How to make it safe across processes?
Implement a key-value database that stores to a file for durability.
How can this be made transactional?
How to make it thread-safe?
How to make it safe across processes?
We wanted the writes to be thread-safe.
(def db (atom {}))
(def die? false)
(def t (doto (Thread. (fn []
(loop [old-state @db]
(when (not die?)
(Thread/sleep 1000)
(let [new-state @db]
(when (not= old-state new-state)
(spit "dbsave.edn" (pr-str new-state)))
(recur new-state))))))
(.start)))
(defn read-db []
(clojure.edn/read-string (slurp "dbsave.edn")))
(defn restore-db []
(reset! db (read-db)))
Solution 1