Initial version of the monger.query DSL

Here is what it looks like:

(with-collection "docs"
  (find { :inception_year { $lt 2000 $gte 2011 } })
  (fields { :inception_year 1 :name 1 })
  (skip 10)
  (limit 20)
  (batch-size 50)
  (hint "my-index-name")
  (snapshot))
This commit is contained in:
Michael S. Klishin 2011-11-14 15:15:43 +04:00
parent c40b0e25c1
commit bd133c1afc
2 changed files with 172 additions and 72 deletions

89
src/monger/query.clj Normal file
View file

@ -0,0 +1,89 @@
(ns monger.query
(:refer-clojure :exclude [select find sort])
(:require monger.core)
(:import [com.mongodb DB DBCollection DBObject DBCursor]
[java.util List])
(:use [monger conversion operators]))
;;
;; Implementation
;;
(def ^{ :dynamic true } *query-collection*)
(defn empty-query
[^DBCollection coll]
{
:collection coll
:query {}
:sort {}
:fields []
:skip 0
:limit 0
:batch-size 256
:hint nil
:snapshot false
})
(defn- fields-to-db-object
[^List fields]
(to-db-object (zipmap fields (repeat 1))))
(defn exec
[{ :keys [collection query fields skip limit sort batch-size hint snapshot] :or { limit 0 batch-size 256 skip 0 } }]
(let [cursor (doto ^DBCursor (.find ^DBCollection collection (to-db-object query) (fields-to-db-object fields))
(.limit limit)
(.skip skip)
(.sort (to-db-object sort))
(.batchSize batch-size)
(.hint ^DBObject (to-db-object hint))
)]
(if snapshot
(.snapshot cursor))
(map (fn [x] (from-db-object x true))
(seq cursor))))
;;
;; API
;;
(defn find
[m query]
(merge m { :query query }))
(defn fields
[m flds]
(merge m { :fields flds }))
(defn sort
[m srt]
(merge m { :sort srt }))
(defn skip
[m ^long n]
(merge m { :skip n }))
(defn limit
[m ^long n]
(merge m { :limit n }))
(defn batch-size
[m ^long n]
(merge m { :batch-size n }))
(defn hint
[m h]
(merge m { :hint h }))
(defn snapshot
[m]
(merge m { :snapshot true }))
(defmacro with-collection
[^String coll & body]
`(binding [*query-collection* (if (string? ~coll)
(.getCollection ^DB monger.core/*mongodb-database* ~coll)
~coll)]
(let [query# (-> (empty-query *query-collection*) ~@body)]
(exec query#))))

View file

@ -1,7 +1,7 @@
(set! *warn-on-reflection* true)
(ns monger.test.querying
(:refer-clojure :exclude [select find])
(:refer-clojure :exclude [select find sort])
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure]
[org.bson.types ObjectId]
[java.util Date])
@ -10,8 +10,7 @@
[monger.result :as mgres])
(:use [clojure.test]
[monger.test.fixtures]
;; [monger.query]
[monger.conversion]))
[monger conversion query operators]))
(defn purge-locations-collection
@ -78,18 +77,30 @@
;; < ($lt), <= ($lte), > ($gt), >= ($gte)
;; (deftest query-using-dsl-and-$lt-operator
;; (let [coll "docs"
;; doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
;; doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
;; doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
;; _ (mgcol/insert-batch coll [doc1 doc2])
;; lt-result (in-collection "docs"
;; (find { :inception_year { "$lt" 2000 } })]
;; (is (= [doc2] lt-result))))
(deftest query-using-dsl-and-$lt-operator-1
(let [coll "docs"
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
_ (mgcol/insert-batch coll [doc1 doc2])
lt-result (with-collection "docs"
(find { :inception_year { $lt 2000 } })
(limit 2))]
(is (= [doc2] lt-result))))
(deftest query-using-dsl-and-$lt-operator-2
(let [coll "docs"
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
_ (mgcol/insert-batch coll [doc1 doc2])
lt-result (with-collection "docs"
(find { :inception_year { $lt 2000 } }))]
(is (= [doc2] lt-result))))
(deftest query-with-find-maps-using-$lt-operator
(deftest query-with-find-maps-using-$lt-operator
(let [coll "docs"
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
@ -105,9 +116,9 @@
(is (= [doc1] gte-result))))
;; $all
;; $all
(deftest query-with-find-maps-using-$all
(deftest query-with-find-maps-using-$all
(let [coll "docs"
doc1 { :_id (ObjectId.) :title "Clojure" :tags ["functional" "homoiconic" "syntax-oriented" "dsls" "concurrency features" "jvm"] }
doc2 { :_id (ObjectId.) :title "Java" :tags ["object-oriented" "jvm"] }
@ -121,9 +132,9 @@
(is (= 2 (count result3)))))
;; $exists
;; $exists
(deftest query-with-find-one-as-map-using-$exists
(deftest query-with-find-one-as-map-using-$exists
(let [coll "docs"
doc1 { :_id (ObjectId.) :published-by "Jill The Blogger" :draft false :title "X announces another Y" }
doc2 { :_id (ObjectId.) :draft true :title "Z announces a Y competitor" }
@ -133,9 +144,9 @@
(is (= doc1 result1))
(is (= doc2 result2))))
;; $mod
;; $mod
(deftest query-with-find-one-as-map-using-$mod
(deftest query-with-find-one-as-map-using-$mod
(let [coll "docs"
doc1 { :_id (ObjectId.) :counter 25 }
doc2 { :_id (ObjectId.) :counter 32 }
@ -149,9 +160,9 @@
(is (empty? result3))))
;; $ne
;; $ne
(deftest query-with-find-one-as-map-using-$ne
(deftest query-with-find-one-as-map-using-$ne
(let [coll "docs"
doc1 { :_id (ObjectId.) :counter 25 }
doc2 { :_id (ObjectId.) :counter 32 }