From a09c55096c7ab50011793b4f168410b34b65da30 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Tue, 28 Feb 2012 20:08:05 +0400 Subject: [PATCH] Add ability to connect using ServerAddress and MongoOptions, closes #15 --- project.clj | 3 ++- src/monger/core.clj | 55 ++++++++++++++++++++++++++++++++++++--- test/monger/test/core.clj | 51 ++++++++++++++++++++++++++++++------ 3 files changed, 96 insertions(+), 13 deletions(-) diff --git a/project.clj b/project.clj index ed71a0f..50a718b 100644 --- a/project.clj +++ b/project.clj @@ -22,4 +22,5 @@ [codox "0.3.4" :exclusions [org.clojure/clojure]]] :dev-resources-path "test/resources" :warn-on-reflection true - :codox { :exclude [monger.internal.pagination] }) + :codox { :exclude [monger.internal.pagination] } + :test-selectors {:focus (fn [v] (:focus v))}) diff --git a/src/monger/core.clj b/src/monger/core.clj index fbc1154..1eed3fc 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -10,11 +10,11 @@ (ns ^{:author "Michael S. Klishin" :doc "Thin idiomatic wrapper around MongoDB Java client. monger.core includes fundamental functions that work with connections & databases. Most of functionality - is in the monger.collection namespace."} + is in other monger.* namespaces, in particular monger.collection."} monger.core (:refer-clojure :exclude [count]) (:use [monger.conversion]) - (:import [com.mongodb Mongo DB WriteConcern DBObject DBCursor CommandResult] + (:import [com.mongodb Mongo DB WriteConcern DBObject DBCursor CommandResult Bytes MongoOptions ServerAddress] [com.mongodb.gridfs GridFS] [java.util Map])) @@ -49,6 +49,8 @@ " ([] (Mongo.)) + ([^ServerAddress server-address ^MongoOptions options] + (Mongo. server-address options)) ([{ :keys [host port] :or { host *mongodb-host*, port *mongodb-port* }}] (Mongo. ^String host ^Long port))) @@ -100,11 +102,56 @@ (do ~@body))) +(defn server-address + ([^String hostname] + (ServerAddress. hostname)) + ([^String hostname ^long port] + (ServerAddress. hostname port))) + + +(defn mongo-options + [& { :keys [connections-per-host threads-allowed-to-block-for-connection-multiplier + max-wait-time connect-timeout socket-timeout socket-keep-alive auto-connect-retry max-auto-connect-retry-time + safe w w-timeout fsync j] }] + (let [mo (MongoOptions.)] + (when connections-per-host + (set! (. mo connectionsPerHost) connections-per-host)) + (when threads-allowed-to-block-for-connection-multiplier + (set! (. mo threadsAllowedToBlockForConnectionMultiplier) threads-allowed-to-block-for-connection-multiplier)) + (when max-wait-time + (set! (. mo maxWaitTime) max-wait-time)) + (when connect-timeout + (set! (. mo connectTimeout) connect-timeout)) + (when socket-timeout + (set! (. mo socketTimeout) socket-timeout)) + (when socket-keep-alive + (set! (. mo socketKeepAlive) socket-keep-alive)) + (when auto-connect-retry + (set! (. mo autoConnectRetry) auto-connect-retry)) + (when max-auto-connect-retry-time + (set! (. mo maxAutoConnectRetryTime) max-auto-connect-retry-time)) + (when safe + (set! (. mo safe) safe)) + (when w + (set! (. mo w) w)) + (when w-timeout + (set! (. mo wtimeout) w-timeout)) + (when j + (set! (. mo j) j)) + (when fsync + (set! (. mo fsync) fsync)) + mo)) + (defn connect! "Connect to MongoDB, store connection in the *mongodb-connection* var" ^Mongo [& args] (def ^:dynamic *mongodb-connection* (apply connect args))) +(defn set-connection! + "Sets given MongoDB connection as default by altering *mongodb-connection* var" + ^Mongo [^Mongo conn] + (def ^:dynamic *mongodb-connection* conn)) + (defn set-db! "Sets *mongodb-database* var to given db, updates *mongodb-gridfs* var state. Recommended to be used for @@ -177,9 +224,9 @@ For :distinct, :count, :drop, :dropIndexes, :mapReduce we suggest to use monger/collection #distinct, #count, #drop, #dropIndexes, :mapReduce respectively. " ([^Map cmd] - (.command ^DB *mongodb-database* ^DBObject (to-db-object cmd))) + (.command ^DB *mongodb-database* ^DBObject (to-db-object cmd))) ([^DB database ^Map cmd] - (.command ^DB database ^DBObject (to-db-object cmd))) + (.command ^DB database ^DBObject (to-db-object cmd))) ) (defprotocol Countable diff --git a/test/monger/test/core.clj b/test/monger/test/core.clj index 82e083d..12fbfc5 100644 --- a/test/monger/test/core.clj +++ b/test/monger/test/core.clj @@ -2,8 +2,9 @@ (:require [monger core collection util result] [monger.test.helper :as helper] [monger.collection :as mgcol]) - (:import (com.mongodb Mongo DB WriteConcern)) - (:use [clojure.test])) + (:import [com.mongodb Mongo DB WriteConcern MongoOptions ServerAddress]) + (:use [clojure.test] + [monger.core :only [server-address mongo-options]])) (helper/connect!) @@ -22,6 +23,45 @@ (is (instance? com.mongodb.Mongo connection)))) +(deftest test-mongo-options-builder + (let [max-wait-time (* 1000 60 2) + ^MongoOptions result (monger.core/mongo-options :connections-per-host 3 :threads-allowed-to-block-for-connection-multiplier 50 + :max-wait-time max-wait-time :connect-timeout 10 :socket-timeout 10 :socket-keep-alive true + :auto-connect-retry true :max-auto-connect-retry-time 0 :safe true + :w 1 :w-timeout 20 :fsync true :j true)] + (is (= 3 (. result connectionsPerHost))) + (is (= 50 (. result threadsAllowedToBlockForConnectionMultiplier))) + (is (= max-wait-time (.maxWaitTime result))) + (is (= 10 (.connectTimeout result))) + (is (= 10 (.socketTimeout result))) + (is (.socketKeepAlive result)) + (is (.autoConnectRetry result)) + (is (= 0 (.maxAutoConnectRetryTime result))) + (is (.safe result)) + (is (= 1 (.w result))) + (is (= 20 (.wtimeout result))) + (is (.fsync result)) + (is (.j result)))) + +(deftest test-server-address + (let [host "127.0.0.1" + port 7878 + ^ServerAddress sa (server-address host port)] + (is (= host (.getHost sa))) + (is (= port (.getPort sa))))) + +(deftest use-existing-mongo-connection + (let [^MongoOptions opts (mongo-options :threads-allowed-to-block-for-connection-multiplier 300) + connection (Mongo. "127.0.0.1" opts)] + (monger.core/set-connection! connection) + (is (= monger.core/*mongodb-connection* connection)))) + +(deftest connect-to-mongo-with-extra-options + (let [^MongoOptions opts (mongo-options :threads-allowed-to-block-for-connection-multiplier 300) + ^ServerAddress sa (server-address "127.0.0.1" 27017)] + (monger.core/connect! sa opts))) + + (deftest get-database (let [connection (monger.core/connect) db (monger.core/get-db connection "monger-test")] @@ -33,11 +73,6 @@ (is (not (empty? dbs))) (is (dbs "monger-test")))) -;; (deftest get-database-with-valid-credentials -;; (let [connection (monger.core/connect) -;; db (monger.core/get-db connection "monger-test" "monger" "test_password")] -;; (is (instance? com.mongodb.DB db)))) - (deftest issuing-a-command "Some commands require administrative priviledges or complex data / checks or heavily depend on DB version. They will be ommited here." (let [collection "things"] @@ -54,4 +89,4 @@ (is (monger.result/ok? (monger.core/get-last-error))) (is (monger.result/ok? (monger.core/get-last-error db))) (is (monger.result/ok? (monger.core/get-last-error db WriteConcern/NORMAL))) - (is (monger.result/ok? (monger.core/get-last-error db 1 100 true))))) \ No newline at end of file + (is (monger.result/ok? (monger.core/get-last-error db 1 100 true)))))