Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Internal DB Specs #2179

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions modules/db-tx-log/src/blaze/db/tx_log/spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
#{"create" "put" "keep" "delete" "conditional-delete" "delete-history"
"patient-purge"})

(s/def :blaze.db.tx-cmd/type
:fhir.resource/type)

(s/def :blaze.db.tx-cmd/refs
(s/coll-of :blaze.fhir/literal-ref-tuple))

Expand All @@ -50,15 +47,15 @@

(defmethod tx-cmd "create" [_]
(s/keys :req-un [:blaze.db.tx-cmd/op
:blaze.db.tx-cmd/type
:fhir.resource/type
:blaze.resource/id
:blaze.resource/hash]
:opt-un [:blaze.db.tx-cmd/refs
:blaze.db.tx-cmd/if-none-exist]))

(defmethod tx-cmd "put" [_]
(s/keys :req-un [:blaze.db.tx-cmd/op
:blaze.db.tx-cmd/type
:fhir.resource/type
:blaze.resource/id
:blaze.resource/hash]
:opt-un [:blaze.db.tx-cmd/refs
Expand All @@ -67,14 +64,14 @@

(defmethod tx-cmd "keep" [_]
(s/keys :req-un [:blaze.db.tx-cmd/op
:blaze.db.tx-cmd/type
:fhir.resource/type
:blaze.resource/id
:blaze.resource/hash]
:opt-un [:blaze.db.tx-cmd/if-match]))

(defmethod tx-cmd "delete" [_]
(s/keys :req-un [:blaze.db.tx-cmd/op
:blaze.db.tx-cmd/type
:fhir.resource/type
:blaze.resource/id]
:opt-un [:blaze.db.tx-cmd/check-refs]))

Expand All @@ -83,14 +80,14 @@

(defmethod tx-cmd "conditional-delete" [_]
(s/keys :req-un [:blaze.db.tx-cmd/op
:blaze.db.tx-cmd/type]
:fhir.resource/type]
:opt-un [:blaze.db.tx-cmd/clauses
:blaze.db.tx-cmd/check-refs
:blaze.db.tx-cmd/allow-multiple]))

(defmethod tx-cmd "delete-history" [_]
(s/keys :req-un [:blaze.db.tx-cmd/op
:blaze.db.tx-cmd/type
:fhir.resource/type
:blaze.resource/id]))

(defmethod tx-cmd "patient-purge" [_]
Expand Down
5 changes: 4 additions & 1 deletion modules/db/.clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@

:consistent-alias
{:aliases
{blaze.db.impl.protocols p}}}}
{blaze.db.impl.index.rts-as-of rts
blaze.db.impl.protocols p
blaze.db.node.tx-indexer.expand expand
blaze.db.node.tx-indexer.verify verify}}}}
4 changes: 2 additions & 2 deletions modules/db/src/blaze/db/node.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
[blaze.db.node.transaction :as tx]
[blaze.db.node.tx-indexer :as tx-indexer]
[blaze.db.node.tx-indexer.util :as tx-u]
[blaze.db.node.tx-indexer.verify :as tx-indexer-verify]
[blaze.db.node.tx-indexer.verify :as verify]
[blaze.db.node.util :as node-util]
[blaze.db.node.validation :as validation]
[blaze.db.node.version :as version]
Expand Down Expand Up @@ -563,7 +563,7 @@
duration-seconds)

(reg-collector ::transaction-sizes
tx-indexer-verify/transaction-sizes)
verify/transaction-sizes)

(reg-collector ::tx-indexer/duration-seconds
tx-u/duration-seconds)
8 changes: 4 additions & 4 deletions modules/db/src/blaze/db/node/tx_indexer/verify.clj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
(update :new-resources conj [type id])
(update-in [:stats tid] inc-num-changes-and-total)))))

(defmethod verify "hold"
[_db-before _t res _tx-cmd]
res)

(defn- print-etags [ts]
(str/join "," (map (partial format "W/\"%d\"") ts)))

Expand Down Expand Up @@ -264,10 +268,6 @@
(-> (update :del-resources conj [type id])
(update-in [:stats tid :total] dec-0)))))))

(defmethod verify :default
[_db-before _t res _tx-cmd]
res)

(defn- verify-tx-cmds* [db-before t tx-cmds]
(reduce
(partial verify db-before t)
Expand Down
16 changes: 9 additions & 7 deletions modules/db/test/blaze/db/impl/index/rts_as_of_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
[clojure.spec.alpha :as s]))

(s/fdef rts/index-entries
:args (s/cat :tid :blaze.db/tid
:id :blaze.db/id-byte-string
:t :blaze.db/t
:hash :blaze.resource/hash
:num-changes nat-int?
:op keyword?
:purged-at (s/? :blaze.db/t))
:args (s/and
(s/cat :tid :blaze.db/tid
:id :blaze.db/id-byte-string
:t :blaze.db/t
:hash :blaze.resource/hash
:num-changes nat-int?
:op keyword?
:purged-at (s/? :blaze.db/t))
#(< (:t %) (:purged-at % Long/MAX_VALUE)))
:ret (s/coll-of :blaze.db.kv/put-entry))
14 changes: 14 additions & 0 deletions modules/db/test/blaze/db/node/tx_indexer/expand_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns blaze.db.node.tx-indexer.expand-spec
(:require
[blaze.db.kv.spec]
[blaze.db.node.tx-indexer.expand :as expand]
[blaze.db.node.tx-indexer.verify :as verify]
[blaze.db.node.tx-indexer.verify.spec]
[blaze.db.spec]
[blaze.db.tx-log.spec]
[clojure.spec.alpha :as s]
[cognitect.anomalies :as anom]))

(s/fdef expand/expand-tx-cmds
:args (s/cat :db-before :blaze.db/db :tx-cmds :blaze.db/tx-cmds)
:ret (s/or :terminal-tx-cmds ::verify/tx-cmds :anomaly ::anom/anomaly))
4 changes: 3 additions & 1 deletion modules/db/test/blaze/db/node/tx_indexer/expand_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[blaze.db.kv.mem-spec]
[blaze.db.node]
[blaze.db.node.tx-indexer.expand :as expand]
[blaze.db.node.tx-indexer.expand-spec]
[blaze.db.search-param-registry]
[blaze.db.test-util :refer [config with-system-data]]
[blaze.db.tx-cache]
Expand Down Expand Up @@ -63,7 +64,8 @@
count := 1
[0 :op] := "hold"
[0 :type] := "Patient"
[0 :id] := "2"))))
[0 :id] := "2"
[0 :if-none-exist] := [["identifier" "120426"]]))))

(deftest expand-tx-cmds-conditional-delete-test
(testing "success"
Expand Down
65 changes: 65 additions & 0 deletions modules/db/test/blaze/db/node/tx_indexer/verify/spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
(ns blaze.db.node.tx-indexer.verify.spec
(:require
[blaze.db.node.tx-indexer.verify :as-alias verify]
[blaze.db.tx-log.spec]
[blaze.fhir.hash.spec]
[blaze.fhir.spec.spec]
[clojure.spec.alpha :as s]))

(s/def ::verify/op
#{"create" "hold" "put" "keep" "delete" "delete-history" "purge"})

(defmulti terminal-tx-cmd "Terminal transaction command after expansion." :op)

(defmethod terminal-tx-cmd "create" [_]
(s/keys :req-un [::verify/op
:fhir.resource/type
:blaze.resource/id
:blaze.resource/hash]
:opt-un [:blaze.db.tx-cmd/refs
:blaze.db.tx-cmd/if-none-exist]))

(defmethod terminal-tx-cmd "hold" [_]
(s/keys :req-un [::verify/op
:fhir.resource/type
:blaze.resource/id
:blaze.db.tx-cmd/if-none-exist]))

(defmethod terminal-tx-cmd "put" [_]
(s/keys :req-un [::verify/op
:fhir.resource/type
:blaze.resource/id
:blaze.resource/hash]
:opt-un [:blaze.db.tx-cmd/refs
:blaze.db.tx-cmd/if-match
:blaze.db.tx-cmd/if-none-match]))

(defmethod terminal-tx-cmd "keep" [_]
(s/keys :req-un [::verify/op
:fhir.resource/type
:blaze.resource/id
:blaze.resource/hash]
:opt-un [:blaze.db.tx-cmd/if-match]))

(defmethod terminal-tx-cmd "delete" [_]
(s/keys :req-un [::verify/op
:fhir.resource/type
:blaze.resource/id]
:opt-un [:blaze.db.tx-cmd/check-refs]))

(defmethod terminal-tx-cmd "delete-history" [_]
(s/keys :req-un [::verify/op
:fhir.resource/type
:blaze.resource/id]))

(defmethod terminal-tx-cmd "purge" [_]
(s/keys :req-un [::verify/op
:fhir.resource/type
:blaze.resource/id]
:opt-un [:blaze.db.tx-cmd/check-refs]))

(s/def ::verify/tx-cmd
(s/multi-spec terminal-tx-cmd :op))

(s/def ::verify/tx-cmds
(s/coll-of ::verify/tx-cmd :kind vector?))
14 changes: 14 additions & 0 deletions modules/db/test/blaze/db/node/tx_indexer/verify_spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns blaze.db.node.tx-indexer.verify-spec
(:require
[blaze.db.impl.index.rts-as-of-spec]
[blaze.db.kv.spec]
[blaze.db.node.tx-indexer.verify :as verify]
[blaze.db.node.tx-indexer.verify.spec]
[blaze.db.spec]
[blaze.db.tx-log.spec]
[clojure.spec.alpha :as s]
[cognitect.anomalies :as anom]))

(s/fdef verify/verify-tx-cmds
:args (s/cat :db-before :blaze.db/db :t :blaze.db/t :tx-cmds ::verify/tx-cmds)
:ret (s/or :entries (s/coll-of :blaze.db.kv/put-entry) :anomaly ::anom/anomaly))
3 changes: 2 additions & 1 deletion modules/db/test/blaze/db/node/tx_indexer/verify_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
[blaze.db.kv.mem-spec]
[blaze.db.node]
[blaze.db.node.tx-indexer.verify :as verify]
[blaze.db.node.tx-indexer.verify-spec]
[blaze.db.test-util :refer [config with-system-data]]
[blaze.db.tx-cache]
[blaze.db.tx-log.local]
Expand Down Expand Up @@ -56,7 +57,7 @@
(deftest verify-tx-cmds-test
(testing "two commands with the same identity aren't allowed"
(let [hash (hash/generate patient-0)
op-gen (sg/such-that (complement #{"conditional-delete"})
op-gen (sg/such-that (complement #{"conditional-delete" "patient-purge"})
(s/gen :blaze.db.tx-cmd/op))
cmd (fn [op] {:op op :type "Patient" :id "0" :hash hash})]
(with-system [{:blaze.db/keys [node]} config]
Expand Down