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
(:refer-clojure :exclude [count])
(: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]
[java.util Map]))
@ -237,7 +237,11 @@
(extend-protocol Countable
DBCursor
(count [^DBCursor this]
(.count this)))
(.count this))
MapReduceOutput
(count [^MapReduceOutput this]
(.count (.results this))))
(defn ^DBObject get-last-error
"Returns the the error (if there is one) from the previous operation on this connection.

View file

@ -60,10 +60,10 @@
(mgcol/remove monger.core/*mongodb-database* collection {})
(is (mgres/ok? (mgcol/insert-batch collection batch)))
(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 })))
(let [output (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 })))
(let [^MapReduceOutput output (mgcol/map-reduce collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})]
(mgres/ok? output)
(is (= 4 (monger.core/count (.results ^MapReduceOutput output))))
(is (= 4 (monger.core/count output)))
(is (= ["CA" "IL" "NY" "OR"]
(map :_id (mgcol/find-maps "merged_mr_outputs"))))
(.drop ^MapReduceOutput output))))