cleanups; updated query/option;

This commit is contained in:
Timo Sulg 2013-09-21 19:17:58 +02:00
parent 2dca992496
commit 7e7f3aba4c
2 changed files with 29 additions and 16 deletions

View file

@ -1,9 +1,7 @@
(ns monger.cursor (ns monger.cursor
"Helpers function for dbCursor object: creating new cursor, "Helper-functions for dbCursor object:
CRUD functionality for cursor options * to initialize new cursor,
Related documentation guides: * for CRUD functionality of options of dbCursor"
* ...
"
(:import [com.mongodb DBCursor Bytes] (:import [com.mongodb DBCursor Bytes]
[java.util List Map] [java.util List Map]
[java.lang Integer] [java.lang Integer]
@ -11,7 +9,7 @@
(:require [monger.conversion :refer [to-db-object from-db-object as-field-selector]])) (:require [monger.conversion :refer [to-db-object from-db-object as-field-selector]]))
(defn ^DBCursor make-db-cursor (defn ^DBCursor make-db-cursor
"initializes new db-collection." "initializes new db-cursor."
([^String collection] (make-db-cursor collection {} {})) ([^String collection] (make-db-cursor collection {} {}))
([^String collection ^Map ref] (make-db-cursor collection ref {})) ([^String collection ^Map ref] (make-db-cursor collection ref {}))
([^String collection ^Map ref fields] ([^String collection ^Map ref fields]
@ -43,12 +41,13 @@
(get cursor-options (keyword opt) 0)))) (get cursor-options (keyword opt) 0))))
(defmulti add-options (fn [db-cur opts] (class opts))) (defmulti add-options (fn [db-cur opts] (class opts)))
(defmethod add-options Map [^DBCursor db-cur options] (defmethod add-options Map [^DBCursor db-cur options]
"Applies cursor options with switch values, where true means switch on "Changes options by using map of settings, which key specifies name of settings
and false removes specified options from current cursor. and boolean value specifies new state of the setting.
example: (add-options db-cur {:notimeout true, :tailable false}) usage:
returns cursor." (add-options db-cur {:notimeout true, :tailable false})
returns:
^DBCursor object."
(doseq [[opt value] (seq options)] (doseq [[opt value] (seq options)]
(if (= true value) (if (= true value)
(add-option! db-cur opt) (add-option! db-cur opt)
@ -56,22 +55,35 @@
db-cur) db-cur)
(defmethod add-options List [^DBCursor db-cur options] (defmethod add-options List [^DBCursor db-cur options]
"Takes list of options to add current key" "Takes list of options and activates these options
usage:
(add-options db-cur [:notimeout :tailable])
returns:
^DBCursor object"
(doseq [opt (seq options)] (doseq [opt (seq options)]
(add-option! db-cur opt)) (add-option! db-cur opt))
db-cur) db-cur)
(defmethod add-options Integer [^DBCursor db-cur, option] (defmethod add-options Integer [^DBCursor db-cur, option]
"Takes com.mongodb.Byte value and adds it to current settings." "Takes com.mongodb.Byte value and adds it to current settings.
usage:
(add-options db-cur com.mongodb.Bytes/QUERYOPTION_NOTIMEOUT)
returns:
^DBCursor object"
(.addOption db-cur option) (.addOption db-cur option)
db-cur) db-cur)
(defmethod add-options Keyword [^DBCursor db-cur, option] (defmethod add-options Keyword [^DBCursor db-cur, option]
"Takes just one keyword as name of settings and applies it to the db-cursor.
usage:
(add-options db-cur :notimeout)
returns:
^DBCursor object"
(add-option! db-cur option) (add-option! db-cur option)
db-cur) db-cur)
(defmethod add-options :default [^DBCursor db-cur, options] (defmethod add-options :default [^DBCursor db-cur, options]
(println "add-options dont support type for options: " (class options)) "Using add-options with not supported type of options just passes unchanged cursor"
db-cur) db-cur)
(defn ^DBCursor reset-options (defn ^DBCursor reset-options

View file

@ -14,7 +14,8 @@
monger.query monger.query
(:refer-clojure :exclude [select find sort]) (:refer-clojure :exclude [select find sort])
(:require [monger.core] (:require [monger.core]
[monger.internal pagination]) [monger.internal pagination]
[monger.cursor :as cursor :refer [add-options]])
(:import [com.mongodb DB DBCollection DBObject DBCursor ReadPreference] (:import [com.mongodb DB DBCollection DBObject DBCursor ReadPreference]
[java.util List]) [java.util List])
(:use [monger conversion operators])) (:use [monger conversion operators]))
@ -75,7 +76,7 @@
(when read-preference (when read-preference
(.setReadPreference cursor read-preference)) (.setReadPreference cursor read-preference))
(when options (when options
(.setOptions cursor options)) (add-options cursor options))
(map (fn [x] (from-db-object x keywordize-fields)) (map (fn [x] (from-db-object x keywordize-fields))
cursor))) cursor)))