Extend monger.core/count to MapReduceOutput

This commit is contained in:
Michael S. Klishin 2012-04-10 19:16:21 +04:00
parent 8f18d30e43
commit a6d15dbbe1
2 changed files with 9 additions and 5 deletions

View file

@ -14,7 +14,7 @@
monger.core monger.core
(:refer-clojure :exclude [count]) (:refer-clojure :exclude [count])
(:use [monger.conversion]) (:use [monger.conversion])
(:import [com.mongodb Mongo DB WriteConcern DBObject DBCursor CommandResult Bytes MongoOptions ServerAddress] (:import [com.mongodb Mongo DB WriteConcern DBObject DBCursor CommandResult Bytes MongoOptions ServerAddress MapReduceOutput]
[com.mongodb.gridfs GridFS] [com.mongodb.gridfs GridFS]
[java.util Map])) [java.util Map]))
@ -237,7 +237,11 @@
(extend-protocol Countable (extend-protocol Countable
DBCursor DBCursor
(count [^DBCursor this] (count [^DBCursor this]
(.count this))) (.count this))
MapReduceOutput
(count [^MapReduceOutput this]
(.count (.results this))))
(defn ^DBObject get-last-error (defn ^DBObject get-last-error
"Returns the the error (if there is one) from the previous operation on this connection. "Returns the the error (if there is one) from the previous operation on this connection.

View file

@ -61,9 +61,9 @@
(is (mgres/ok? (mgcol/insert-batch collection batch))) (is (mgres/ok? (mgcol/insert-batch collection batch)))
(mgcol/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {}) (mgcol/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})
(is (mgres/ok? (mgcol/insert collection { :state "OR" :price 17.95 :quantity 4 }))) (is (mgres/ok? (mgcol/insert collection { :state "OR" :price 17.95 :quantity 4 })))
(let [output (mgcol/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})] (let [^MapReduceOutput output (mgcol/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})]
(mgres/ok? output) (mgres/ok? output)
(is (= 4 (monger.core/count (.results ^MapReduceOutput output)))) (is (= 4 (monger.core/count output)))
(is (= ["CA" "IL" "NY" "OR"] (is (= ["CA" "IL" "NY" "OR"]
(map :_id (mgcol/find-maps "merged_mr_outputs")))) (map :_id (mgcol/find-maps "merged_mr_outputs"))))
(.drop ^MapReduceOutput output)))) (.drop ^MapReduceOutput output))))