Enclose cursor operations in a with-yielding call.

The with-yielding call will produce a lazy-seq in a
separate thread that will generate content from the
cursor.  The cursor will then be safely closed if the
seq is garbage collected or an error occurs.
This commit is contained in:
Tim Clemons 2013-11-19 17:45:12 -08:00
parent b72968dfb9
commit d51c61d798
2 changed files with 15 additions and 11 deletions

View file

@ -7,7 +7,8 @@
[org.mongodb/mongo-java-driver "2.11.2"] [org.mongodb/mongo-java-driver "2.11.2"]
[com.novemberain/validateur "1.5.0"] [com.novemberain/validateur "1.5.0"]
[clojurewerkz/support "0.19.0"] [clojurewerkz/support "0.19.0"]
[ragtime/ragtime.core "0.3.4"]] [ragtime/ragtime.core "0.3.4"]
[clj-yield "1.1"]]
:test-selectors {:default (fn [m] :test-selectors {:default (fn [m]
(and (not (:performance m)) (and (not (:performance m))
(not (:edge-features m)) (not (:edge-features m))

View file

@ -18,7 +18,8 @@
[monger.cursor :as cursor :refer [add-options]]) [monger.cursor :as cursor :refer [add-options]])
(:import [com.mongodb DB DBCollection DBObject DBCursor ReadPreference] (:import [com.mongodb DB DBCollection DBObject DBCursor ReadPreference]
[java.util List]) [java.util List])
(:use [monger conversion operators])) (:use [monger conversion operators]
[yield]))
;; ;;
@ -65,6 +66,7 @@
(defn exec (defn exec
[{ :keys [^DBCollection collection query fields skip limit sort batch-size hint snapshot read-preference keywordize-fields options] :or { limit 0 batch-size 256 skip 0 } }] [{ :keys [^DBCollection collection query fields skip limit sort batch-size hint snapshot read-preference keywordize-fields options] :or { limit 0 batch-size 256 skip 0 } }]
(with-yielding [y-out 1]
(with-open [cursor (doto (.find collection (to-db-object query) (as-field-selector fields)) (with-open [cursor (doto (.find collection (to-db-object query) (as-field-selector fields))
(.limit limit) (.limit limit)
(.skip skip) (.skip skip)
@ -77,9 +79,10 @@
(.setReadPreference cursor read-preference)) (.setReadPreference cursor read-preference))
(when options (when options
(add-options cursor options)) (add-options cursor options))
(map (fn [x] (from-db-object x keywordize-fields)) (loop []
cursor))) (when (.hasNext cursor)
(yield y-out (from-db-object (.next cursor) keywordize-fields))
(recur))))))
;; ;;
;; API ;; API
;; ;;