diff --git a/cljfmt.edn b/cljfmt.edn index f90509483..a3d97da8b 100644 --- a/cljfmt.edn +++ b/cljfmt.edn @@ -6,6 +6,7 @@ if-ok [[:block 1]] try-one [[:block 2]] when-ok [[:block 1]] + with-open-coll [[:block 1]] do-sync [[:block 1]] do-async [[:block 1]] has-form [[:block 1]] diff --git a/modules/coll/.clj-kondo/config.edn b/modules/coll/.clj-kondo/config.edn index 78c4a3953..b2035b3a4 100644 --- a/modules/coll/.clj-kondo/config.edn +++ b/modules/coll/.clj-kondo/config.edn @@ -1,2 +1,5 @@ {:config-paths - ["../../../.clj-kondo/root"]} + ["../../../.clj-kondo/root"] + + :lint-as + {blaze.coll.core/with-open-coll clojure.core/with-open}} diff --git a/modules/coll/src/blaze/coll/core.clj b/modules/coll/src/blaze/coll/core.clj index 13553569d..5ca80e9fb 100644 --- a/modules/coll/src/blaze/coll/core.clj +++ b/modules/coll/src/blaze/coll/core.clj @@ -21,7 +21,7 @@ [coll] (identical? ::empty (reduce #(reduced %2) ::empty coll))) -(defn- inc-rf [sum _] (inc ^long sum)) +(defn inc-rf [sum _] (inc ^long sum)) (defn eduction "Like `clojure.core/eduction` but implements Counted instead of Iterable." @@ -67,4 +67,7 @@ IReduceInit (reduce [_ rf# init#] (with-open ~bindings - (reduce rf# init# ~coll))))) + (reduce rf# init# ~coll))) + Counted + (count [coll#] + (.reduce coll# inc-rf 0)))) diff --git a/modules/coll/test/blaze/coll/core_test.clj b/modules/coll/test/blaze/coll/core_test.clj index f78058ea4..1c18a73e7 100644 --- a/modules/coll/test/blaze/coll/core_test.clj +++ b/modules/coll/test/blaze/coll/core_test.clj @@ -1,11 +1,14 @@ (ns blaze.coll.core-test (:require - [blaze.coll.core :as coll] + [blaze.coll.core :as coll :refer [with-open-coll]] [blaze.test-util :as tu] [clojure.spec.test.alpha :as st] - [clojure.test :as test :refer [are deftest is testing]])) + [clojure.test :as test :refer [are deftest is testing]]) + (:import + [java.lang AutoCloseable])) (st/instrument) +(set! *warn-on-reflection* true) (test/use-fixtures :each tu/fixture) @@ -93,3 +96,11 @@ [::x ::y] 0 ::x [::x ::y] 1 ::y [::x ::y] 2 ::not-found))) + +(deftest with-open-coll-test + (let [state (volatile! false) + coll (with-open-coll [_ (reify AutoCloseable (close [_] (vreset! state true)))] + (coll/eduction (map inc) (range 10)))] + (is (= 10 (count coll))) + (is (= (range 1 11) (vec coll))) + (is (true? @state)))) diff --git a/modules/cql/.clj-kondo/config.edn b/modules/cql/.clj-kondo/config.edn index ca7000645..054ef5f7b 100644 --- a/modules/cql/.clj-kondo/config.edn +++ b/modules/cql/.clj-kondo/config.edn @@ -8,8 +8,7 @@ "../../module-test-util/resources/clj-kondo.exports/blaze/module-test-util"] :lint-as - {blaze.db.impl.macros/with-open-coll clojure.core/with-open - blaze.elm.compiler.macros/defunop clojure.core/defn + {blaze.elm.compiler.macros/defunop clojure.core/defn blaze.elm.compiler.macros/defbinop clojure.core/defn blaze.elm.compiler.macros/defternop clojure.core/defn blaze.elm.compiler.macros/defnaryop clojure.core/defn diff --git a/modules/db/src/blaze/db/impl/iterators.clj b/modules/db/src/blaze/db/impl/iterators.clj index 8e23cb072..c50ca5dd6 100644 --- a/modules/db/src/blaze/db/impl/iterators.clj +++ b/modules/db/src/blaze/db/impl/iterators.clj @@ -10,10 +10,11 @@ (:require [blaze.byte-buffer :as bb] [blaze.byte-string :as bs] + [blaze.coll.core :as coll] [blaze.db.impl.util :as u] [blaze.db.kv :as kv]) (:import - [clojure.lang IReduceInit])) + [clojure.lang Counted IReduceInit])) (set! *warn-on-reflection* true) (set! *unchecked-math* :warn-on-boxed) @@ -164,17 +165,25 @@ (defn- coll ([snapshot column-family xform] - (reify IReduceInit + (reify + IReduceInit (reduce [_ rf init] (with-open [iter (kv/new-iterator snapshot column-family)] (kv/seek-to-first! iter) - (reduce-iter! iter kv/next! (xform (completing rf)) init))))) + (reduce-iter! iter kv/next! (xform (completing rf)) init))) + Counted + (count [coll] + (.reduce coll coll/inc-rf 0)))) ([snapshot column-family xform start-key] - (reify IReduceInit + (reify + IReduceInit (reduce [_ rf init] (with-open [iter (kv/new-iterator snapshot column-family)] (kv/seek! iter (bs/to-byte-array start-key)) - (reduce-iter! iter kv/next! (xform (completing rf)) init)))))) + (reduce-iter! iter kv/next! (xform (completing rf)) init))) + Counted + (count [coll] + (.reduce coll coll/inc-rf 0))))) (defn- coll-prev [snapshot column-family xform start-key] (reify IReduceInit diff --git a/modules/db/test/blaze/db/api_test.clj b/modules/db/test/blaze/db/api_test.clj index 2bd0b205b..206faf683 100644 --- a/modules/db/test/blaze/db/api_test.clj +++ b/modules/db/test/blaze/db/api_test.clj @@ -1152,7 +1152,7 @@ [[:put {:fhir/type :fhir/Patient :id "0"}]]] (testing "has one list entry" - (is (= 1 (count (vec (d/type-list (d/db node) "Patient"))))) + (is (= 1 (count (d/type-list (d/db node) "Patient")))) (is (= 1 (d/type-total (d/db node) "Patient")))))) (testing "a node with two patients in two transactions" @@ -1233,11 +1233,11 @@ [[:put {:fhir/type :fhir/Observation :id "0"}]]] (testing "has one patient list entry" - (is (= 1 (count (vec (d/type-list (d/db node) "Patient"))))) + (is (= 1 (count (d/type-list (d/db node) "Patient")))) (is (= 1 (d/type-total (d/db node) "Patient")))) (testing "has one observation list entry" - (is (= 1 (count (vec (d/type-list (d/db node) "Observation"))))) + (is (= 1 (count (d/type-list (d/db node) "Observation")))) (is (= 1 (d/type-total (d/db node) "Observation")))))) (testing "the database is immutable" @@ -4382,7 +4382,7 @@ :end #fhir/dateTime"2001-07"}}]]] (let [db (d/db node) - num-encounter #(count (vec (d/type-query db "Encounter" %)))] + num-encounter #(count (d/type-query db "Encounter" %))] (are [year n] (= n (num-encounter [["date" (format "gt%d-01-01" year)] ["date" (format "lt%d-01-01" (inc year))]])) 1999 2 @@ -4428,7 +4428,7 @@ [tx-ops] (let [db (d/db node) - num-encounter #(count (vec (d/type-query db "Encounter" %)))] + num-encounter #(count (d/type-query db "Encounter" %))] (= (num-encounter [["date" (str year)]]) (num-encounter [["date" (format "sa%d-12-31" (dec year))] ["date" (format "eb%d-01-01" (inc year))]]))))))) @@ -4441,7 +4441,7 @@ [tx-ops] (let [db (d/db node) - num-encounter #(count (vec (d/type-query db "Encounter" %)))] + num-encounter #(count (d/type-query db "Encounter" %))] (= (num-encounter [["date" (str "ap" year)]]) (num-encounter [["date" (format "ge%d-01-01" year)] ["date" (format "lt%d-01-01" (inc year))]]))))))) @@ -6355,7 +6355,7 @@ (let [db (d/db node) patient (d/resource-handle db "Patient" "0")] - (is (coll/empty? (vec (d/rev-include db patient))))))) + (is (coll/empty? (d/rev-include db patient)))))) (testing "with three resources" (with-system-data [{:blaze.db/keys [node]} config] @@ -6684,7 +6684,7 @@ [[[:put {:fhir/type :fhir/Patient :id "0"}]]] (with-open [batch-db (d/new-batch-db (d/db node))] - (is (= 1 (count (vec (d/type-list batch-db "Patient"))))) + (is (= 1 (count (d/type-list batch-db "Patient")))) (is (= 1 (d/type-total batch-db "Patient")))))) (testing "compile-type-query" @@ -6714,7 +6714,7 @@ [[[:put {:fhir/type :fhir/Patient :id "0"}]]] (with-open [batch-db (d/new-batch-db (d/db node))] - (is (= 1 (count (vec (d/system-list batch-db))))) + (is (= 1 (count (d/system-list batch-db)))) (is (= 1 (d/system-total batch-db)))))) (testing "compile-compartment-query"