Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
clj-util-datomic-peer/src/main/clojure/de/mpg/shh/util_datomic_peer/transaction.clj
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
68 lines (60 sloc)
2.39 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns de.mpg.shh.util-datomic-peer.transaction | |
(:require [clojure.tools.logging :refer [info error]] | |
[clojure.string :as str] | |
[datomic.api :as d]) | |
(:import [java.util.concurrent ExecutionException] | |
[datomic.db DbId])) | |
(defn wrap-tx | |
[tx-future] | |
(try | |
{:tx-result (deref tx-future)} | |
(catch ExecutionException eex | |
{:error (.getMessage (.getCause eex))}))) | |
(defn transact-entity-and-resolve-id | |
[tx-future temp-eid] | |
(let [tx-result (wrap-tx tx-future)] | |
(if (contains? tx-result :error) | |
tx-result | |
(assoc tx-result :eid (d/resolve-tempid (:db-after (:tx-result tx-result)) (:tempids (:tx-result tx-result)) temp-eid))))) | |
(defn resolve-tx-map-temp-id | |
[id-map accumulator [k v]] | |
(if (and (= DbId (type v)) | |
(contains? id-map v)) | |
(assoc accumulator k (get id-map v)) | |
(assoc accumulator k v))) | |
(defn resolve-tx-datom-temp-id | |
[id-map accumulator v] | |
(if (and (= DbId (type v)) | |
(contains? id-map v)) | |
(conj accumulator (get id-map v)) | |
(conj accumulator v))) | |
(defn resolve-tx-temp-ids | |
[id-map tx] | |
(if (map? tx) | |
(reduce (partial resolve-tx-map-temp-id id-map) {} tx) | |
(reduce (partial resolve-tx-datom-temp-id id-map) [] tx))) | |
(defn resolve-id-map | |
[tx-result accumulator [k v]] | |
(if (= DbId (type v)) | |
(let [resolved-id (d/resolve-tempid (:db-after tx-result) (:tempids tx-result) v)] | |
(if (nil? resolved-id) | |
(assoc accumulator k v) | |
(assoc accumulator k v | |
v resolved-id))) | |
(assoc accumulator k v))) | |
(defn apply-tx-with-db | |
"apply the transaction to the given db using with" | |
[accumulator item] | |
(let [resolved-item (vec (map (partial resolve-tx-temp-ids (:id-map accumulator)) item)) | |
tx-result (d/with (:db accumulator) resolved-item) | |
resolved-id-map (reduce (partial resolve-id-map tx-result) {} (:id-map accumulator))] | |
(assoc accumulator :db (:db-after tx-result) | |
:id-map resolved-id-map))) | |
(defn apply-tx | |
"apply the transaction to the given db" | |
[accumulator item] | |
(let [resolved-item (vec (map (partial resolve-tx-temp-ids (:id-map accumulator)) item)) | |
tx-result (.get (d/transact (:conn accumulator) resolved-item)) | |
resolved-id-map (reduce (partial resolve-id-map tx-result) {} (:id-map accumulator))] | |
(assoc accumulator :db (:db-after tx-result) | |
:id-map resolved-id-map))) |