Skip to content

Commit

Permalink
[select] Use transients in select-reducing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-yakushev committed Oct 8, 2024
1 parent 86929ea commit e1f15ba
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/toucan2/jdbc/mysql_mariadb.clj
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
_ (log/debugf "update-returning-pks workaround: doing SELECT with conditions %s"
conditions-map)
parsed-args (update pipeline/*parsed-args* :kv-args merge conditions-map)
select-rf (pipeline/with-init conj [])
select-rf (pipeline/conj-with-init! [])
xform (map (model/select-pks-fn model))
pks (pipeline/transduce-query (xform select-rf)
:toucan.query-type/select.instances.fns
Expand Down
9 changes: 9 additions & 0 deletions src/toucan2/pipeline.clj
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,15 @@
([x] (rf x))
([x y] (rf x y))))

(defn ^:no-doc conj!-with-init
"Returns a reducing function with a zero-arity (initial value arity) that returns transient version of `init`, `conj!`s
values into it, and finally returns a persistent collection in 1-arity."
[init]
(fn
([] (transient init))
([acc] (persistent! acc))
([acc y] (conj! acc y))))

(m/defmulti default-rf
"The default reducing function for queries of `query-type`. Used for non-reducible operations
like [[toucan2.select/select]] or [[toucan2.execute/query]]."
Expand Down
6 changes: 3 additions & 3 deletions src/toucan2/select.clj
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
[f :conn connectable modelable-columns & kv-args? query?])}
[f & unparsed-args]
(let [f (comp realize/realize f)
rf (pipeline/with-init conj #{})
rf (pipeline/conj-with-init! #{})
xform (map f)]
(not-empty (pipeline/transduce-unparsed (xform rf) :toucan.query-type/select.instances.fns unparsed-args))))

Expand All @@ -98,7 +98,7 @@
[f :conn connectable modelable-columns & kv-args? query?])}
[f & unparsed-args]
(let [f (comp realize/realize f)
rf (pipeline/with-init conj [])
rf (pipeline/conj-with-init! [])
xform (map f)]
(not-empty (pipeline/transduce-unparsed (xform rf) :toucan.query-type/select.instances.fns unparsed-args))))

Expand Down Expand Up @@ -190,7 +190,7 @@
[f1 f2 & unparsed-args]
(let [f1 (comp realize/realize f1)
f2 (comp realize/realize f2)
rf (pipeline/with-init conj {})
rf (pipeline/conj-with-init! {})
xform (map (juxt f1 f2))]
(pipeline/transduce-unparsed (xform rf) :toucan.query-type/select.instances unparsed-args)))

Expand Down
4 changes: 2 additions & 2 deletions test/toucan2/execute_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
#_query-type :default
#_model :default]
[rf _conn _query-type _model [{k :key}, :as _compiled-query]]
(reduce rf (rf) [{k 1} {k 2} {k 3}]))
(transduce identity rf [{k 1} {k 2} {k 3}]))

(deftest ^:parallel wow-dont-even-need-to-use-jdbc-test
(is (= [{:a 1} {:a 2} {:a 3}]
Expand All @@ -203,7 +203,7 @@
#_query-type :default
#_model ::model.not-even-jdbc]
[rf _conn _query-type _model {k :key, :as _compiled-query}]
(reduce rf (rf) [{k 4} {k 5} {k 6}]))
(transduce identity rf [{k 4} {k 5} {k 6}]))

(deftest ^:parallel wow-dont-even-need-to-use-jdbc-custom-model-test
(is (= [{:a 4} {:a 5} {:a 6}]
Expand Down

0 comments on commit e1f15ba

Please sign in to comment.