Skip to content

Commit

Permalink
Clearing out connections that have been deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
Paula Gearon committed Oct 10, 2021
1 parent aec11ef commit 92364f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/asami/memory.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
(< 0 c) (recur low mid)))))))


(declare as-of* as-of-t* as-of-time* since* since-t* graph* entity* next-tx* db* transact-update* transact-data*)
(declare as-of* as-of-t* as-of-time* since* since-t* graph* entity*
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 @@ -63,7 +64,7 @@
(get-name [this] name)
(next-tx [this] (next-tx* this))
(db [this] (db* this))
(delete-database [this] true) ;; no-op for memory databases
(delete-database [this] (delete-database* this))
(release [this]) ;; no-op for memory databases
(transact-update [this update-fn] (transact-update* this update-fn))
(transact-data [this updates! asserts retracts] (transact-data* this updates! asserts retracts))
Expand All @@ -73,7 +74,6 @@
(def empty-graph mem/empty-graph)
(def empty-multi-graph multi/empty-multi-graph)


(s/defn new-connection :- ConnectionType
"Creates a memory Connection object"
[name :- s/Str
Expand All @@ -90,6 +90,13 @@
[connection :- ConnectionType]
(:db @(:state connection)))

(s/defn delete-database* :- s/Bool
"Reverts the state of a connection to an empty database, resetting the initialization time."
[{:keys [state] :as connection} :- ConnectionType]
(let [db (->MemoryDatabase (-> state deref :history first :graph) [] (now) 0)]
(reset! state {:db db :history [db]})
true))

(s/defn as-database :- DatabaseType
"Creates a Database around an existing Graph.
graph: The graph to build a database around. "
Expand Down
18 changes: 18 additions & 0 deletions test/asami/api_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,24 @@
(delete-database db-io)
(delete-database db-new))))

(deftest test-database-delete
(testing "Are deleted memory databases cleared"
(let [db-name "asami:mem://test-del"
conn (connect db-name)
{d :db-after} @(transact conn {:tx-data io-entities})
r (set (q '[:find ?e ?a ?v :where [?e ?a ?v]] d))]
(is (= 13 (count r)))

(delete-database db-name)

(let [d2 (db conn)
r2 (set (q '[:find ?e ?a ?v :where [?e ?a ?v]] d2))
r3 (set (q '[:find ?e ?a ?v :where [?e ?a ?v]] conn))
r-old (set (q '[:find ?e ?a ?v :where [?e ?a ?v]] d))]
(is (empty? r2))
(is (empty? r3))
(is (= 13 (count r-old)))))))

(deftest test-update-unowned
(testing "Doing an update on an attribute that references a top level entity"
(let [c (connect "asami:mem://testupdate")
Expand Down

0 comments on commit 92364f4

Please sign in to comment.