From b61d5f00cf35c597d547aa171fd6a0f6068f3efc Mon Sep 17 00:00:00 2001 From: "Alexander K. Hudek" Date: Thu, 24 Jul 2014 03:26:15 -0400 Subject: [PATCH] Update transaction code. --- project.clj | 2 +- src/views/db/core.clj | 29 ++++++++++++++--------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/project.clj b/project.clj index 1caaf2f..000feda 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject views "0.1.0" +(defproject views "0.2.0-SNAPSHOT" :description "You underestimate the power of the SQL side" :url "https://github.com/diligenceengine/views" diff --git a/src/views/db/core.clj b/src/views/db/core.clj index 7adebff..7a59603 100644 --- a/src/views/db/core.clj +++ b/src/views/db/core.clj @@ -35,20 +35,19 @@ (throw e))))) ;; otherwise rethrow (defmacro with-view-transaction - [config binding & forms] - (let [tvar (first binding), this-ns (nth binding 2)] - `(if (:deltas ~(second binding)) ;; check if we are in a nested transaction - (let [~tvar ~(second binding), ~this-ns ~(nth binding 3)] ~@forms) + [binding & forms] + (let [tvar (first binding), vc (second binding)] + `(if (:deltas ~vc) ;; check if we are in a nested transaction + (let [~tvar ~vc] ~@forms) (do-transaction-fn-with-retries - (fn [] - (let [base-subscribed-views# (:base-subscribed-views ~config) - deltas# (atom []) - result# (j/with-db-transaction [t# ~(second binding) :isolation :serializable] - (let [~tvar (assoc t# :deltas deltas#) - ~this-ns ~(nth binding 3)] - ~@forms))] - (broadcast-deltas base-subscribed-views# @deltas# ~(nth binding 3)) - result#)))))) + (fn [] + (let [base-subscribed-views# (:base-subscribed-views ~vc) + deltas# (atom []) + result# (j/with-db-transaction [t# (:db ~vc) :isolation :serializable] + (let [~tvar (assoc ~vc :deltas deltas# :db t#)] + ~@forms))] + (broadcast-deltas base-subscribed-views# @deltas# (:namespace ~vc)) + result#)))))) (defn vexec! "Used to perform arbitrary insert/update/delete actions on the database, @@ -71,10 +70,10 @@ a collection of view-maps. - broadcast-deltas takes ... ." - [{:keys [db schema base-subscribed-views templates namespace] :as conf} action-map] + [{:keys [db schema base-subscribed-views templates namespace deltas] :as conf} action-map] (let [subbed-views (subscribed-views base-subscribed-views namespace) transaction-fn #(vd/do-view-transaction schema db subbed-views action-map templates)] - (if-let [deltas (:deltas db)] ;; inside a transaction we just collect deltas and do not retry + (if deltas ;; inside a transaction we just collect deltas and do not retry (let [{:keys [new-deltas result-set]} (transaction-fn)] (swap! deltas into new-deltas) result-set)