From e5af694cc755483b7ca853d8a4ff484d74b8c9a4 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 06:04:11 +0400 Subject: [PATCH 01/12] Support explicit database parameter for monger.collection/insert --- src/monger/collection.clj | 7 +++++-- test/monger/test/collection.clj | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index ac73457..ccb18e1 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -32,11 +32,14 @@ (monger.collection/insert \"people\" { :name \"Joe\", :age 30, WriteConcern/SAFE }) " - ([^String collection, ^DBObject document] + ([^String collection ^DBObject document] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.insert ^DBCollection coll ^DBObject (to-db-object document) ^WriteConcern monger.core/*mongodb-write-concern*))) - ([^String collection, ^DBObject document, ^WriteConcern concern] + ([^String collection ^DBObject document ^WriteConcern concern] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.insert ^DBCollection coll ^DBObject (to-db-object document) ^WriteConcern concern))) + ([^DB db ^String collection ^DBObject document ^WriteConcern concern] + (let [^DBCollection coll (.getCollection db collection)] (.insert ^DBCollection coll ^DBObject (to-db-object document) ^WriteConcern concern)))) diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index e25dfa2..0deffd9 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -30,6 +30,13 @@ (is (monger.result/ok? (mgcol/insert "people" doc))) (is (= 1 (mgcol/count collection))))) +(deftest insert-a-basic-document-with-explicitly-passed-database-without-id-and-with-default-write-concern + (let [collection "people" + doc { :name "Joe", :age 30 }] + (dotimes [n 5] + (is (monger.result/ok? (mgcol/insert monger.core/*mongodb-database* "people" doc WriteConcern/SAFE)))) + (is (= 5 (mgcol/count collection))))) + (deftest insert-a-basic-document-without-id-and-with-explicit-write-concern (let [collection "people" doc { :name "Joe", :age 30 }] From afa516fff666abcce79fd2e1feda0a9ff9ca7f7b Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 06:31:28 +0400 Subject: [PATCH 02/12] Explicit database parameter for monger.collection/insert-batch, /count, /empty?, /any? --- src/monger/collection.clj | 14 ++++++++++++-- test/monger/test/collection.clj | 20 ++++++++++++++------ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index ccb18e1..9043727 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -58,6 +58,9 @@ (.insert ^DBCollection coll ^List (to-db-object documents) ^WriteConcern monger.core/*mongodb-write-concern*))) ([^String collection, ^List documents, ^WriteConcern concern] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.insert ^DBCollection coll ^List (to-db-object documents) ^WriteConcern concern))) + ([^DB db ^String collection, ^List documents, ^WriteConcern concern] + (let [^DBCollection coll (.getCollection db collection)] (.insert ^DBCollection coll ^List (to-db-object documents) ^WriteConcern concern)))) ;; @@ -201,6 +204,9 @@ (.count coll))) (^long [^String collection, ^Map conditions] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.count coll (to-db-object conditions)))) + (^long [^DB db ^String collection, ^Map conditions] + (let [^DBCollection coll (.getCollection db collection)] (.count coll (to-db-object conditions))))) (defn any? @@ -216,7 +222,9 @@ ([^String collection] (> (count collection) 0)) ([^String collection, ^Map conditions] - (> (count collection conditions) 0))) + (> (count collection conditions) 0)) + ([^DB db ^String collection, ^Map conditions] + (> (count db collection conditions) 0))) (defn empty? @@ -226,7 +234,9 @@ (mgcol/empty? \"things\") " ([^String collection] - (= (count collection) 0))) + (= (count collection) 0)) + ([^DB db ^String collection] + (= (count db collection {}) 0))) ;; monger.collection/update diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index 0deffd9..c5fb44a 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -75,6 +75,13 @@ (is (monger.result/ok? (mgcol/insert-batch "people" docs WriteConcern/NORMAL))) (is (= 2 (mgcol/count collection))))) +(deftest insert-a-batch-of-basic-documents-with-explicit-database-without-ids-and-with-explicit-write-concern + (let [collection "people" + docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]] + (dotimes [n 44] + (is (monger.result/ok? (mgcol/insert-batch monger.core/*mongodb-database* "people" docs WriteConcern/NORMAL)))) + (is (= 88 (mgcol/count collection))))) + @@ -91,12 +98,12 @@ { :language "Scala", :name "akka" }] ) (is (= 4 (mgcol/count collection))) (is (mgcol/any? collection)) - (is (= 3 (mgcol/count collection { :language "Clojure" }))) - (is (mgcol/any? collection { :language "Clojure" })) + (is (= 3 (mgcol/count monger.core/*mongodb-database* collection { :language "Clojure" }))) + (is (mgcol/any? monger.core/*mongodb-database* collection { :language "Clojure" })) (is (= 1 (mgcol/count collection { :language "Scala" }))) (is (mgcol/any? collection { :language "Scala" })) - (is (= 0 (mgcol/count collection { :language "Python" }))) - (is (not (mgcol/any? collection { :language "Python" }))))) + (is (= 0 (mgcol/count monger.core/*mongodb-database* collection { :language "Python" }))) + (is (not (mgcol/any? monger.core/*mongodb-database* collection { :language "Python" }))))) (deftest remove-all-documents-from-collection @@ -543,11 +550,12 @@ (let [collection "things" _ (mgcol/insert collection { :language "Clojure", :name "langohr" })] (is (mgcol/any? "things")) - (is (mgcol/any? "things" {:language "Clojure"})))) + (is (mgcol/any? monger.core/*mongodb-database* "things" {:language "Clojure"})))) (deftest empty-on-empty-collection (let [collection "things"] - (is (mgcol/empty? collection)))) + (is (mgcol/empty? collection)) + (is (mgcol/empty? monger.core/*mongodb-database* collection)))) (deftest empty-on-non-empty-collection (let [collection "things" From b76cffcd1ae6507c9d0911d33fdd9c46dfc3944f Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 06:58:02 +0400 Subject: [PATCH 03/12] Ditto for monger.collection/save --- src/monger/collection.clj | 7 +++++-- test/monger/test/collection.clj | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 9043727..564a085 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -290,11 +290,14 @@ (monger.collection/save \"people\" { :first_name \"Ian\" :last_name \"Gillan\" }) " - ([^String collection, ^Map document] + ([^String collection ^Map document] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.save coll (to-db-object document) monger.core/*mongodb-write-concern*))) - ([^String collection, ^Map document, ^WriteConcern write-concern] + ([^String collection ^Map document ^WriteConcern write-concern] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.save coll document write-concern))) + ([^DB db ^String collection ^Map document ^WriteConcern write-concern] + (let [^DBCollection coll (.getCollection db collection)] (.save coll document write-concern)))) diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index c5fb44a..08045c4 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -381,7 +381,7 @@ (let [collection "people" doc (mgcnv/to-db-object { :name "Joe", :age 30 })] (is (nil? (monger.util/get-id doc))) - (mgcol/save "people" doc) + (mgcol/save monger.core/*mongodb-database* "people" doc WriteConcern/SAFE) (is (not (nil? (monger.util/get-id doc)))))) From 3df399c2be2496c2b7ec3a2345cfad060f15ab82 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 07:26:21 +0400 Subject: [PATCH 04/12] Ditto for monger.collection/distinct --- src/monger/collection.clj | 12 +++++++++--- test/monger/test/collection.clj | 8 ++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 564a085..4a759fb 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -316,8 +316,11 @@ ([^String collection] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.remove coll (to-db-object {})))) - ([^String collection, ^Map conditions] + ([^String collection ^Map conditions] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.remove coll (to-db-object conditions)))) + ([^DB db ^String collection ^Map conditions] + (let [^DBCollection coll (.getCollection db collection)] (.remove coll (to-db-object conditions))))) @@ -459,11 +462,14 @@ (defn distinct "Finds distinct values for a key" - ([^String collection, ^String key] + ([^String collection ^String key] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.distinct coll ^String (to-db-object key)))) - ([^String collection, ^String key, ^Map query] + ([^String collection ^String key ^Map query] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.distinct coll ^String (to-db-object key) ^DBObject (to-db-object query)))) + ([^DB db ^String collection ^String key ^Map query] + (let [^DBCollection coll (.getCollection db collection)] (.distinct coll ^String (to-db-object key) ^DBObject (to-db-object query))))) diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index 08045c4..c96927e 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -488,7 +488,7 @@ { :state "IL" :quantity 3 :price 5.50 }] expected [{:_id "CA", :value 204.9} {:_id "IL", :value 39.5} {:_id "NY", :value 697.0}]] (deftest basic-inline-map-reduce-example - (mgcol/remove collection) + (mgcol/remove monger.core/*mongodb-database* collection {}) (is (mgres/ok? (mgcol/insert-batch collection batch))) (let [output (mgcol/map-reduce collection mapper reducer nil MapReduceCommand$OutputType/INLINE {}) results (mgcnv/from-db-object ^DBObject (.results ^MapReduceOutput output) true)] @@ -496,7 +496,7 @@ (is (= expected results)))) (deftest basic-map-reduce-example-that-replaces-named-collection - (mgcol/remove collection) + (mgcol/remove monger.core/*mongodb-database* collection {}) (is (mgres/ok? (mgcol/insert-batch collection batch))) (let [output (mgcol/map-reduce collection mapper reducer "mr_outputs" {}) results (mgcnv/from-db-object ^DBObject (.results ^MapReduceOutput output) true)] @@ -509,7 +509,7 @@ (.drop ^MapReduceOutput output))) (deftest basic-map-reduce-example-that-merged-results-into-named-collection - (mgcol/remove collection) + (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 }))) @@ -534,7 +534,7 @@ { :state "CA" :quantity 2 :price 2.95 } { :state "IL" :quantity 3 :price 5.50 }]] (mgcol/insert-batch collection batch) - (is (= ["CA" "IL" "NY"] (sort (mgcol/distinct collection :state)))) + (is (= ["CA" "IL" "NY"] (sort (mgcol/distinct monger.core/*mongodb-database* collection :state {})))) (is (= ["CA" "NY"] (sort (mgcol/distinct collection :state { :price { $gt 100.00 } })))))) From dccb4770f1f950295f805b985bb3550ab4a03938 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 07:28:37 +0400 Subject: [PATCH 05/12] Ditto for monger.collection/rename --- src/monger/collection.clj | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 4a759fb..8d87585 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -438,8 +438,11 @@ ([^String from, ^String to] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* from)] (.rename coll to))) - ([^String from, ^String to, ^Boolean drop-target] + ([^String from ^String to ^Boolean drop-target] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* from)] + (.rename coll to drop-target))) + ([^DB db ^String from ^String to ^Boolean drop-target] + (let [^DBCollection coll (.getCollection db from)] (.rename coll to drop-target)))) ;; From 76a3eafd5d4a2890b2066700ba4fdb66a7a9e06d Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 07:29:30 +0400 Subject: [PATCH 06/12] Ditto for monger.collection/drop --- src/monger/collection.clj | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 8d87585..1e01315 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -414,8 +414,8 @@ (defn create "Creates a collection with a given name and options." - [^String collection, ^Map options] - (.createCollection monger.core/*mongodb-database* collection (to-db-object options))) + ([^String collection ^Map options] + (.createCollection monger.core/*mongodb-database* collection (to-db-object options)))) (defn drop "Deletes collection from database. @@ -424,9 +424,12 @@ (monger.collection/drop \"collection-to-drop\") " - [^String collection] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] - (.drop coll))) + ([^String collection] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.drop coll))) + ([^DB db ^String collection] + (let [^DBCollection coll (.getCollection db collection)] + (.drop coll)))) (defn rename "Renames collection. From 025e8fce45027bede346c36d4f7ac0ed85cc0205 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 07:30:11 +0400 Subject: [PATCH 07/12] Ditto for monger.collection/create --- src/monger/collection.clj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 1e01315..ce955d9 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -415,7 +415,9 @@ (defn create "Creates a collection with a given name and options." ([^String collection ^Map options] - (.createCollection monger.core/*mongodb-database* collection (to-db-object options)))) + (.createCollection monger.core/*mongodb-database* collection (to-db-object options))) + ([^DB db ^String collection ^Map options] + (.createCollection db collection (to-db-object options)))) (defn drop "Deletes collection from database. From b30071a2b38ed2510b0098241f10808924fb464f Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 07:38:28 +0400 Subject: [PATCH 08/12] Ditto for monger.collection/exists?, /drop-index and /drop-indexes --- src/monger/collection.clj | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index ce955d9..d7cf322 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -387,14 +387,19 @@ (defn drop-index "Drops an index from this collection." - [^String collection, ^String name] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] - (.dropIndex coll name))) + ([^String collection ^String name] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.dropIndex coll name))) + ([^DB db ^String collection ^String name] + (let [^DBCollection coll (.getCollection db collection)] + (.dropIndex coll name)))) (defn drop-indexes "Drops an indices from this collection." - [^String collection] - (.dropIndexes ^DBCollection (.getCollection monger.core/*mongodb-database* collection))) + ([^String collection] + (.dropIndexes ^DBCollection (.getCollection monger.core/*mongodb-database* collection))) + ([^DB db ^String collection] + (.dropIndexes ^DBCollection (.getCollection db collection)))) ;; @@ -409,8 +414,10 @@ (monger.collection/exists? \"coll\") " - [^String collection] - (.collectionExists monger.core/*mongodb-database* collection)) + ([^String collection] + (.collectionExists monger.core/*mongodb-database* collection)) + ([^DB db ^String collection] + (.collectionExists db collection))) (defn create "Creates a collection with a given name and options." From 4d30f1625d681b839d06c2ccb4774f26aa1f6b19 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 07:39:51 +0400 Subject: [PATCH 09/12] Ditto for monger.collection/create-index --- src/monger/collection.clj | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index d7cf322..2501ac6 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -338,9 +338,12 @@ (monger.collection/create-index collection { \"language\" 1 }) " - [^String collection, ^Map keys] - (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] - (.createIndex coll (to-db-object keys)))) + ([^String collection ^Map keys] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.createIndex coll (to-db-object keys)))) + ([^DB db ^String collection ^Map keys] + (let [^DBCollection coll (.getCollection db collection)] + (.createIndex coll (to-db-object keys))))) ;; From df7802796ad25fc9693f81ab7a655b73beb3cf11 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 07:40:21 +0400 Subject: [PATCH 10/12] Cosmetics --- src/monger/collection.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 2501ac6..4741088 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -273,7 +273,7 @@ (monger.collection/update \"people\" { :first_name \"Yoko\" } { :first_name \"Yoko\" :last_name \"Ono\" } :upsert true) By default :upsert and :multi are false." - [^String collection, ^Map conditions, ^Map document, & { :keys [upsert multi write-concern] :or { upsert false, multi false, write-concern monger.core/*mongodb-write-concern* } }] + [^String collection ^Map conditions ^Map document & { :keys [upsert multi write-concern] :or { upsert false, multi false, write-concern monger.core/*mongodb-write-concern* } }] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.update coll (to-db-object conditions) (to-db-object document) upsert multi write-concern))) From 0bfe0582c02bd721238c8393ea05b5f098a1b27b Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 07:51:21 +0400 Subject: [PATCH 11/12] Ditto for monger.collection/find and friends --- src/monger/collection.clj | 60 ++++++++++++++++++++------------- test/monger/test/collection.clj | 3 +- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 4741088..edb646e 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -86,12 +86,16 @@ ([^String collection] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.find coll))) - ([^String collection, ^Map ref] + ([^String collection ^Map ref] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.find ^DBCollection coll ^DBObject (to-db-object ref)))) - ([^String collection, ^Map ref, ^List fields] + ([^String collection ^Map ref ^List fields] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection) map-of-fields (fields-to-db-object fields)] + (.find ^DBCollection coll ^DBObject (to-db-object ref) ^DBObject (to-db-object map-of-fields)))) + ([^DB db ^String collection ^Map ref ^List fields] + (let [^DBCollection coll (.getCollection db collection) + map-of-fields (fields-to-db-object fields)] (.find ^DBCollection coll ^DBObject (to-db-object ref) ^DBObject (to-db-object map-of-fields))))) (defn ^ISeq find-maps @@ -101,19 +105,23 @@ " ([^String collection] (map (fn [x] (from-db-object x true)) (find collection))) - ([^String collection, ^Map ref] + ([^String collection ^Map ref] (map (fn [x] (from-db-object x true)) (find collection ref))) - ([^String collection, ^Map ref, ^List fields] - (map (fn [x] (from-db-object x true)) (find collection ref fields)))) + ([^String collection ^Map ref ^List fields] + (map (fn [x] (from-db-object x true)) (find collection ref fields))) + ([^DB db ^String collection ^Map ref ^List fields] + (map (fn [x] (from-db-object x true)) (find db collection ref fields)))) (defn ^ISeq find-seq "Queries for objects in this collection, returns ISeq of DBObjects." ([^String collection] (seq (find collection))) - ([^String collection, ^Map ref] + ([^String collection ^Map ref] (seq (find collection ref))) - ([^String collection, ^Map ref, ^List fields] - (seq (find collection ref fields)))) + ([^String collection ^Map ref ^List fields] + (seq (find collection ref fields))) + ([^DB db ^String collection ^Map ref ^List fields] + (seq (find db collection ref fields)))) ;; ;; monger.collection/find-one @@ -131,21 +139,25 @@ (mgcol/find-one collection { :language \"Clojure\" } [:language]) " - ([^String collection, ^Map ref] + ([^String collection ^Map ref] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.findOne ^DBCollection coll ^DBObject (to-db-object ref)))) - ([^String collection, ^Map ref, ^List fields] + ([^String collection ^Map ref ^List fields] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection) map-of-fields (fields-to-db-object fields)] + (.findOne ^DBCollection coll ^DBObject (to-db-object ref) ^DBObject (to-db-object map-of-fields)))) + ([^DB db ^String collection ^Map ref ^List fields] + (let [^DBCollection coll (.getCollection db collection) + map-of-fields (fields-to-db-object fields)] (.findOne ^DBCollection coll ^DBObject (to-db-object ref) ^DBObject (to-db-object map-of-fields))))) (defn ^IPersistentMap find-one-as-map "Returns a single object converted to Map from this collection matching the query." - ([^String collection, ^Map ref] + ([^String collection ^Map ref] (from-db-object ^DBObject (find-one collection ref) true)) - ([^String collection, ^Map ref, keywordize] + ([^String collection ^Map ref keywordize] (from-db-object ^DBObject (find-one collection ref) keywordize)) - ([^String collection, ^Map ref, ^List fields, keywordize] + ([^String collection ^Map ref ^List fields keywordize] (from-db-object ^DBObject (find-one collection ref fields) keywordize))) @@ -165,18 +177,20 @@ ;; Note that _id field is always returned. (mgcol/find-one-by-id collection \"4ef45ab4744e9fd632640e2d\" [:language]) " - ([^String collection, id] + ([^String collection id] (find-one collection { :_id id })) - ([^String collection, id, ^List fields] - (find-one collection { :_id id } fields))) + ([^String collection id ^List fields] + (find-one collection { :_id id } fields)) + ([^DB db ^String collection id ^List fields] + (find-one db collection { :_id id } fields))) (defn ^IPersistentMap find-map-by-id "Returns a single object, converted to map with matching _id field." - ([^String collection, id] + ([^String collection id] (from-db-object ^DBObject (find-one-as-map collection { :_id id }) true)) - ([^String collection, id, keywordize] + ([^String collection id keywordize] (from-db-object ^DBObject (find-one-as-map collection { :_id id }) keywordize)) - ([^String collection, id, ^List fields, keywordize] + ([^String collection id ^List fields keywordize] (from-db-object ^DBObject (find-one-as-map collection { :_id id } fields) keywordize))) @@ -202,10 +216,10 @@ (^long [^String collection] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.count coll))) - (^long [^String collection, ^Map conditions] + (^long [^String collection ^Map conditions] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.count coll (to-db-object conditions)))) - (^long [^DB db ^String collection, ^Map conditions] + (^long [^DB db ^String collection ^Map conditions] (let [^DBCollection coll (.getCollection db collection)] (.count coll (to-db-object conditions))))) @@ -221,9 +235,9 @@ " ([^String collection] (> (count collection) 0)) - ([^String collection, ^Map conditions] + ([^String collection ^Map conditions] (> (count collection conditions) 0)) - ([^DB db ^String collection, ^Map conditions] + ([^DB db ^String collection ^Map conditions] (> (count db collection conditions) 0))) diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index c96927e..64e9078 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -320,7 +320,8 @@ { :language "Scala", :name "akka" }]) (is (= 1 (clojure.core/count (mgcol/find-maps collection { :language "Scala" })))) (is (= 3 (.count (mgcol/find-maps collection { :language "Clojure" })))) - (is (empty? (mgcol/find-maps collection { :language "Java" }))))) + (is (empty? (mgcol/find-maps collection { :language "Java" }))) + (is (empty? (mgcol/find-maps monger.core/*mongodb-database* collection { :language "Java" } [:language :name]))))) From 8f6c5cdf2a6c263cd22f86d14496489e38349e3a Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Sun, 29 Jan 2012 07:56:13 +0400 Subject: [PATCH 12/12] Add explicit GridFS instance parameter to monger.gridfs/remove, /remove-all and /all-files Unfortunately, we cannot do the same for functions that implement GridFS-related protocols. --- src/monger/gridfs.clj | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/monger/gridfs.clj b/src/monger/gridfs.clj index 8385152..90d8a73 100644 --- a/src/monger/gridfs.clj +++ b/src/monger/gridfs.clj @@ -3,7 +3,7 @@ (:require [monger.core] [clojure.java.io :as io]) (:use [monger.conversion]) - (:import [com.mongodb DBObject] + (:import [com.mongodb DB DBObject] [com.mongodb.gridfs GridFS GridFSInputFile] [java.io InputStream File])) @@ -30,17 +30,23 @@ ([] (remove {})) ([query] - (.remove ^GridFS monger.core/*mongodb-gridfs* ^DBObject (to-db-object query)))) + (.remove ^GridFS monger.core/*mongodb-gridfs* ^DBObject (to-db-object query))) + ([^GridFS fs query] + (.remove fs ^DBObject (to-db-object query)))) (defn remove-all - [] - (remove {})) + ([] + (remove {})) + ([^GridFS fs] + (remove fs {}))) (defn all-files ([] (.getFileList ^GridFS monger.core/*mongodb-gridfs*)) ([query] - (.getFileList ^GridFS monger.core/*mongodb-gridfs* query))) + (.getFileList ^GridFS monger.core/*mongodb-gridfs* query)) + ([^GridFS fs query] + (.getFileList fs query))) (defprotocol GridFSInputFileFactory