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]
"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
[db pattern]
"Search for datoms using the EAVT index.")
@ -132,6 +138,10 @@
[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?
(<eavt [db pattern]
(let [[e a v] pattern
@ -1077,8 +1087,8 @@
[conn tx-data]
{:pre [(conn? conn)]}
(let [db (db conn)] ;; TODO: be careful with swapping atoms.
(s/in-transaction!
(:sqlite-connection db)
(in-transaction!
db
#(go-pair
(let [report (<? (<with db tx-data))]
(reset! (:current-db conn) (:db-after report))

View file

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