Follow-up: lift in-transaction! to DB; make transactions exclusive.

This commit is contained in:
Nick Alexander 2016-08-04 16:27:36 -07:00
parent f408cfd314
commit 52af06ce28
2 changed files with 22 additions and 12 deletions

View file

@ -67,6 +67,12 @@
[db] [db]
"TODO: document this interface.") "TODO: document this interface.")
(in-transaction!
[db chan-fn]
"Evaluate the given pair-chan `chan-fn` in an exclusive transaction. If it returns non-nil,
commit the transaction; otherwise, rollback the transaction. Returns a pair-chan resolving to
the pair-chan returned by `chan-fn`.")
(<eavt (<eavt
[db pattern] [db pattern]
"Search for datoms using the EAVT index.") "Search for datoms using the EAVT index.")
@ -132,6 +138,10 @@
[db] [db]
(inc (:current-tx db))) (inc (:current-tx db)))
(in-transaction! [db chan-fn]
(s/in-transaction!
(:sqlite-connection db) chan-fn))
;; TODO: use q for searching? Have q use this for searching for a single pattern? ;; TODO: use q for searching? Have q use this for searching for a single pattern?
(<eavt [db pattern] (<eavt [db pattern]
(let [[e a v] pattern (let [[e a v] pattern
@ -1077,8 +1087,8 @@
[conn tx-data] [conn tx-data]
{:pre [(conn? conn)]} {:pre [(conn? conn)]}
(let [db (db conn)] ;; TODO: be careful with swapping atoms. (let [db (db conn)] ;; TODO: be careful with swapping atoms.
(s/in-transaction! (in-transaction!
(:sqlite-connection db) db
#(go-pair #(go-pair
(let [report (<? (<with db tx-data))] (let [report (<? (<with db tx-data))]
(reset! (:current-db conn) (:db-after report)) (reset! (:current-db conn) (:db-after report))

View file

@ -59,15 +59,15 @@
when no more results exist. Consume with <?." when no more results exist. Consume with <?."
[db [sql & bindings :as rest] chan] [db [sql & bindings :as rest] chan]
(go-safely [c chan] (go-safely [c chan]
(let [result (<! (-each db sql bindings (let [result (<! (-each db sql bindings
(fn [row] (fn [row]
(put! c [row nil]))))] (put! c [row nil]))))]
;; We assume that a failure will result in the promise ;; We assume that a failure will result in the promise
;; channel being rejected and no further row callbacks ;; channel being rejected and no further row callbacks
;; being called. ;; being called.
(when (second result) (when (second result)
(put! result c)) (put! result c))
(close! c)))) (close! c))))
(defn all-rows (defn all-rows
[db [sql & bindings :as rest]] [db [sql & bindings :as rest]]
@ -76,7 +76,7 @@
(defn in-transaction! [db chan-fn] (defn in-transaction! [db chan-fn]
(go (go
(try (try
(<? (execute! db ["BEGIN TRANSACTION"])) (<? (execute! db ["BEGIN EXCLUSIVE TRANSACTION"]))
(let [[v e] (<! (chan-fn))] (let [[v e] (<! (chan-fn))]
(if v (if v
(do (do