From a6d15dbbe1a5ec18a2dcb71b0ece319372e9cc17 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Tue, 10 Apr 2012 19:16:21 +0400 Subject: [PATCH] Extend monger.core/count to MapReduceOutput --- src/monger/core.clj | 8 ++++++-- test/monger/test/map_reduce.clj | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/monger/core.clj b/src/monger/core.clj index b2f0c06..5a0825a 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -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. diff --git a/test/monger/test/map_reduce.clj b/test/monger/test/map_reduce.clj index e7e6cc6..7204fa8 100644 --- a/test/monger/test/map_reduce.clj +++ b/test/monger/test/map_reduce.clj @@ -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))))