Skip to content

Commit

Permalink
Make more Reducible Collections Counted
Browse files Browse the repository at this point in the history
Before some reducible collections like from the iterators namespace and
such created by the with-open-coll macro did not implement Counted. So
they could not directly counted and instead had to be converted into a
vector first.

Needed by #1382
  • Loading branch information
alexanderkiel committed Sep 16, 2024
1 parent 7a8d388 commit 3ec5807
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions cljfmt.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
5 changes: 4 additions & 1 deletion modules/coll/.clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
{:config-paths
["../../../.clj-kondo/root"]}
["../../../.clj-kondo/root"]

:lint-as
{blaze.coll.core/with-open-coll clojure.core/with-open}}
7 changes: 5 additions & 2 deletions modules/coll/src/blaze/coll/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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))))
15 changes: 13 additions & 2 deletions modules/coll/test/blaze/coll/core_test.clj
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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))))
3 changes: 1 addition & 2 deletions modules/cql/.clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions modules/db/src/blaze/db/impl/iterators.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions modules/db/test/blaze/db/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))]])))))))
Expand All @@ -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))]])))))))
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 3ec5807

Please sign in to comment.