From 92364f4e6e830e68a6e1ea1a017bf744ad6dfac0 Mon Sep 17 00:00:00 2001 From: Paula Gearon Date: Sun, 10 Oct 2021 17:26:54 -0400 Subject: [PATCH] Clearing out connections that have been deleted --- src/asami/memory.cljc | 13 ++++++++++--- test/asami/api_test.cljc | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/asami/memory.cljc b/src/asami/memory.cljc index f10f7ec..0157199 100644 --- a/src/asami/memory.cljc +++ b/src/asami/memory.cljc @@ -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 @@ -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)) @@ -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 @@ -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. " diff --git a/test/asami/api_test.cljc b/test/asami/api_test.cljc index a033ed0..1931d4c 100644 --- a/test/asami/api_test.cljc +++ b/test/asami/api_test.cljc @@ -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")