diff --git a/src/clojure/monger/core.clj b/src/clojure/monger/core.clj index 1194b7f..11f6b55 100644 --- a/src/clojure/monger/core.clj +++ b/src/clojure/monger/core.clj @@ -88,10 +88,11 @@ ([^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 - description write-concern cursor-finalizer-enabled] :or [auto-connect-retry true] }] +(defn ^MongoClientOptions$Builder mongo-options-builder + [{: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 + description write-concern cursor-finalizer-enabled read-preference + required-replica-set-name] :or [auto-connect-retry true]}] (let [mob (MongoClientOptions$Builder.)] (when connections-per-host (.connectionsPerHost mob connections-per-host)) @@ -107,14 +108,24 @@ (.socketKeepAlive mob socket-keep-alive)) (when auto-connect-retry (.autoConnectRetry mob auto-connect-retry)) + ;; deprecated (when max-auto-connect-retry-time (.maxAutoConnectRetryTime mob max-auto-connect-retry-time)) + (when read-preference + (.readPreference mob read-preference)) (when description (.description mob description)) (when write-concern (.writeConcern mob write-concern)) (when cursor-finalizer-enabled (.cursorFinalizerEnabled mob cursor-finalizer-enabled)) + (when required-replica-set-name + (.requiredReplicaSetName mob required-replica-set-name)) + mob)) + +(defn ^MongoClientOptions mongo-options + [opts] + (let [mob (mongo-options-builder opts)] (.build mob))) (defn disconnect diff --git a/test/monger/test/core_test.clj b/test/monger/test/core_test.clj index 2e09e40..f3965b9 100644 --- a/test/monger/test/core_test.clj +++ b/test/monger/test/core_test.clj @@ -16,12 +16,12 @@ (mg/disconnect conn))) (deftest connect-to-mongo-with-default-host-and-explicit-port - (let [connection (mg/connect { :port 27017 })] + (let [connection (mg/connect {:port 27017})] (is (instance? com.mongodb.MongoClient connection)))) (deftest connect-to-mongo-with-default-port-and-explicit-host - (let [connection (mg/connect { :host "127.0.0.1" })] + (let [connection (mg/connect {:host "127.0.0.1"})] (is (instance? com.mongodb.MongoClient connection)))) (deftest test-server-address @@ -32,13 +32,13 @@ (is (= port (.getPort sa))))) (deftest use-existing-mongo-connection - (let [^MongoClientOptions opts (mongo-options :threads-allowed-to-block-for-connection-multiplier 300) + (let [^MongoClientOptions opts (mongo-options {:threads-allowed-to-block-for-connection-multiplier 300}) connection (MongoClient. "127.0.0.1" opts) db (mg/get-db connection "monger-test")] (mg/disconnect connection))) (deftest connect-to-mongo-with-extra-options - (let [^MongoClientOptions opts (mongo-options :threads-allowed-to-block-for-connection-multiplier 300) + (let [^MongoClientOptions opts (mongo-options {:threads-allowed-to-block-for-connection-multiplier 300}) ^ServerAddress sa (server-address "127.0.0.1" 27017) conn (mg/connect sa opts)] (mg/disconnect conn)))