Skip to content

Commit

Permalink
Now reattaching deleted connections if they are reused
Browse files Browse the repository at this point in the history
  • Loading branch information
Paula Gearon committed Oct 10, 2021
1 parent 92364f4 commit 8946abf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
17 changes: 16 additions & 1 deletion src/asami/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
database was created, false if it already exists."
[uri :- s/Str]
(boolean
(if-not (@connections uri)
(when-not (@connections uri)
(swap! connections assoc uri (connection-for uri)))))

(s/defn connect :- ConnectionType
Expand Down Expand Up @@ -124,6 +124,17 @@
(swap! connections assoc uri c)
c)))

(defn check-attachment
"Checks if a connection is attached to the connections map.
If not, then connect. Returns the connection if previously connected,
false if it needed to be reconnected."
[connection]
(let [url (storage/get-url connection)]
(or (@connections url)
(do
(swap! connections assoc url connection)
false))))

(def db storage/db)
(def as-of storage/as-of)
(def as-of-t storage/as-of-t)
Expand Down Expand Up @@ -179,6 +190,10 @@
:tempids mapping of the temporary IDs in entities to the allocated nodes"
[{:keys [name state] :as connection} :- ConnectionType
{:keys [tx-data tx-triples executor update-fn] :as tx-info} :- TransactData]

;; Detached databases need to be reattached when transacted into
(check-attachment connection)

(let [op (if update-fn
(fn []
(let [[db-before db-after] (storage/transact-update connection update-fn)]
Expand Down
4 changes: 4 additions & 0 deletions src/asami/durable/store.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,14 @@
(let [[asserts retracts] (generator-fn graph)]
(graph/graph-transact graph tx-id asserts retracts updates!))))))

(s/defn get-url* :- s/Str
[{:keys [name]} :- ConnectionType]
(str "asami:local://" name))

(defrecord DurableConnection [name tx-manager grapha nodea lock]
storage/Connection
(get-name [this] name)
(get-url [this] (get-url* this))
(next-tx [this] (common/tx-count tx-manager))
(db [this] (db* this))
(delete-database [this] (delete-database* this))
Expand Down
15 changes: 12 additions & 3 deletions src/asami/memory.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
[asami.internal :refer [now instant?]]
[asami.index :as mem]
[asami.multi-graph :as multi]
[asami.graph :as gr]
[asami.graph :as gr :refer [GraphType]]
[asami.query :as query]
[zuko.schema :refer [Triple]]
[asami.entities.general :as entity :refer [GraphType]]
[asami.entities.reader :as reader]
[schema.core :as s :include-macros true]))

Expand Down Expand Up @@ -38,7 +37,7 @@


(declare as-of* as-of-t* as-of-time* since* since-t* graph* entity*
next-tx* db* delete-database* transact-update* transact-data*)
get-url* next-tx* db* delete-database* transact-update* transact-data*)

;; graph is the wrapped graph
;; history is a seq of Databases, excluding this one
Expand All @@ -62,6 +61,7 @@
(defrecord MemoryConnection [name state]
storage/Connection
(get-name [this] name)
(get-url [this] (get-url* this))
(next-tx [this] (next-tx* this))
(db [this] (db* this))
(delete-database [this] (delete-database* this))
Expand All @@ -81,6 +81,15 @@
(let [db (->MemoryDatabase gr [] (now) 0)]
(->MemoryConnection name (atom {:db db :history [db]}))))

(s/defn get-url* :- s/Str
[{:keys [name state]} :- ConnectionType]
(let [first-graph (-> state deref :history first :graph)
gtype (condp = first-graph
empty-graph "mem"
empty-multi-graph "multi"
(throw (ex-info (str "Unknown graph type:" (type first-graph)) {:graph first-graph})))]
(str "asami:" gtype "://" name)))

(s/defn next-tx* :- s/Num
[connection :- ConnectionType]
(count (:history @(:state connection))))
Expand Down
1 change: 1 addition & 0 deletions src/asami/storage.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

(defprotocol Connection
(get-name [this] "Retrieves the name of the database")
(get-url [this] "Retrieves the url of the database. Based on the name.")
(next-tx [this] "Returns the next transaction ID that this connection will use")
(get-lock [this] "Returns a lock that ensures that this Connection can only be updated by a single thread at a time")
(db [this] "Retrieves the latest database from this connection")
Expand Down
13 changes: 10 additions & 3 deletions test/asami/api_test.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns asami.api-test
"Tests the public query functionality"
(:require [asami.core :refer [q show-plan create-database connect db transact
entity as-of since import-data export-data delete-database]]
(:require [asami.core :as a :refer [q show-plan create-database connect db transact
entity as-of since import-data export-data delete-database]]
[asami.index :as i]
[asami.graph :as graph]
[asami.multi-graph :as m]
Expand Down Expand Up @@ -722,7 +722,14 @@
r-old (set (q '[:find ?e ?a ?v :where [?e ?a ?v]] d))]
(is (empty? r2))
(is (empty? r3))
(is (= 13 (count r-old)))))))
(is (= 13 (count r-old))))

(is (nil? (get @a/connections db-name)))
(let [{dx :db-after} @(transact conn {:tx-triples [[:mem/node-1 :property "value"]
[:mem/node-2 :property "other"]]})
rx (q '[:find ?e ?a ?v :where [?e ?a ?v]] dx)]
(is (identical? conn (get @a/connections db-name)))
(is (= 2 (count rx)))))))

(deftest test-update-unowned
(testing "Doing an update on an attribute that references a top level entity"
Expand Down

0 comments on commit 8946abf

Please sign in to comment.