Skip to content

Commit

Permalink
Added transact-update to the Connection API, allowing for a new arg t…
Browse files Browse the repository at this point in the history
…ype for the transact function
  • Loading branch information
Paula Gearon committed Jan 12, 2021
1 parent 9b6cf6d commit c25983c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
37 changes: 21 additions & 16 deletions src/asami/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,27 @@
:tempids mapping of the temporary IDs in entities to the allocated nodes
:executor Executor to be used to run the CompletableFuture"
[{:keys [name state] :as connection} :- ConnectionType
{:keys [tx-data tx-triples executor] :as tx-info} :- TransactData]
(let [op (fn []
(let [tx-id (count (:history @state))
as-datom (fn [assert? [e a v]] (->Datom e a v tx-id assert?))
{:keys [graph history] :as db-before} (:db @state)
[triples removals tempids] (if tx-triples ;; is this inserting raw triples?
[tx-triples nil {}]
;; capture the old usage which didn't have an arg map
(entities/build-triples db-before (or tx-data tx-info)))
[db-before db-after] (storage/transact-data connection triples removals)]
{:db-before db-before
:db-after db-after
:tx-data (concat
(map (partial as-datom false) removals)
(map (partial as-datom true) triples))
:tempids tempids}))]
{:keys [tx-data tx-triples executor update-fn] :as tx-info} :- TransactData]
(let [op (if update-fn
(fn []
(let [[db-before db-after] (storage/transact-update connection update-fn)]
{:db-before db-before
:db-after db-after}))
(fn []
(let [tx-id (count (:history @state))
as-datom (fn [assert? [e a v]] (->Datom e a v tx-id assert?))
{:keys [graph history] :as db-before} (:db @state)
[triples removals tempids] (if tx-triples ;; is this inserting raw triples?
[tx-triples nil {}]
;; capture the old usage which didn't have an arg map
(entities/build-triples db-before (or tx-data tx-info)))
[db-before db-after] (storage/transact-data connection triples removals)]
{:db-before db-before
:db-after db-after
:tx-data (concat
(map (partial as-datom false) removals)
(map (partial as-datom true) triples))
:tempids tempids})))]
#?(:clj (CompletableFuture/supplyAsync (reify Supplier (get [_] (op)))
(or executor clojure.lang.Agent/soloExecutor))
:cljs (let [d (delay (op))]
Expand Down
20 changes: 14 additions & 6 deletions src/asami/memory.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
(< 0 c) (recur low mid)))))))


(declare as-of* as-of-t* since* since-t* graph* entity* db* transact-data*)
(declare as-of* as-of-t* since* since-t* graph* entity* db* transact-update* transact-data*)

;; graph is the wrapped graph
;; history is a seq of Databases, excluding this one
Expand All @@ -72,6 +72,7 @@
storage/Connection
(db [this] (db* this))
(delete-database [this]) ;; no-op for memory databases
(transact-update [this update-fn] (transact-update* this update-fn))
(transact-data [this asserts retracts] (transact-data* this asserts retracts)))


Expand Down Expand Up @@ -145,16 +146,23 @@
[database :- DatabaseType]
(:graph database))

(defn transact-data*
"Removes a series of tuples from the latest graph, and asserts new tuples into the graph.
Updates the connection to the new graph."
[conn asserts retracts]
(defn transact-update*
"Updates a graph with a function, updating the connection to the new graph.
The function accepts a graph and a transaction ID.
Returns a pair containing the old database and the new one."
[conn update-fn]
(let [{:keys [graph history] :as db-before} (db* conn)
next-graph (gr/graph-transact graph (count history) asserts retracts)
next-graph (update-fn graph (count history))
db-after (->MemoryDatabase next-graph (conj history db-before) (now))]
(reset! (:state conn) {:db db-after :history (conj (:history db-after) db-after)})
[db-before db-after]))

(defn transact-data*
"Removes a series of tuples from the latest graph, and asserts new tuples into the graph.
Updates the connection to the new graph."
[conn asserts retracts]
(transact-update* conn (fn [graph tx-id] (gr/graph-transact graph tx-id asserts retracts))))

(s/defn entity* :- {s/Any s/Any}
"Returns an entity based on an identifier, either the :db/id or a :db/ident if this is available. This eagerly retrieves the entity.
Objects may be nested, but references to top level objects will be nil in order to avoid loops."
Expand Down
2 changes: 2 additions & 0 deletions src/asami/storage.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
(defprotocol Connection
(db [this] "Retrieves the latest database from this connection")
(delete-database [this] "Removes all resources for a given connection")
(transact-update [this update-fn] "Updates a graph in the database with the provided function.
Function args are connection and transaction-id")
(transact-data [this asserts retracts] "Updates the database with provided data"))

(defprotocol Database
Expand Down

0 comments on commit c25983c

Please sign in to comment.