Make mongo-options take a map instead of pseudo-kwards, extract mongo-options-builder
This commit is contained in:
parent
59427e29bb
commit
f846cd0a51
2 changed files with 19 additions and 8 deletions
|
|
@ -88,10 +88,11 @@
|
||||||
([^String hostname ^Long port]
|
([^String hostname ^Long port]
|
||||||
(ServerAddress. hostname port)))
|
(ServerAddress. hostname port)))
|
||||||
|
|
||||||
(defn mongo-options
|
(defn ^MongoClientOptions$Builder mongo-options-builder
|
||||||
[& { :keys [connections-per-host threads-allowed-to-block-for-connection-multiplier
|
[{: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
|
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] }]
|
description write-concern cursor-finalizer-enabled read-preference
|
||||||
|
required-replica-set-name] :or [auto-connect-retry true]}]
|
||||||
(let [mob (MongoClientOptions$Builder.)]
|
(let [mob (MongoClientOptions$Builder.)]
|
||||||
(when connections-per-host
|
(when connections-per-host
|
||||||
(.connectionsPerHost mob connections-per-host))
|
(.connectionsPerHost mob connections-per-host))
|
||||||
|
|
@ -107,14 +108,24 @@
|
||||||
(.socketKeepAlive mob socket-keep-alive))
|
(.socketKeepAlive mob socket-keep-alive))
|
||||||
(when auto-connect-retry
|
(when auto-connect-retry
|
||||||
(.autoConnectRetry mob auto-connect-retry))
|
(.autoConnectRetry mob auto-connect-retry))
|
||||||
|
;; deprecated
|
||||||
(when max-auto-connect-retry-time
|
(when max-auto-connect-retry-time
|
||||||
(.maxAutoConnectRetryTime mob max-auto-connect-retry-time))
|
(.maxAutoConnectRetryTime mob max-auto-connect-retry-time))
|
||||||
|
(when read-preference
|
||||||
|
(.readPreference mob read-preference))
|
||||||
(when description
|
(when description
|
||||||
(.description mob description))
|
(.description mob description))
|
||||||
(when write-concern
|
(when write-concern
|
||||||
(.writeConcern mob write-concern))
|
(.writeConcern mob write-concern))
|
||||||
(when cursor-finalizer-enabled
|
(when cursor-finalizer-enabled
|
||||||
(.cursorFinalizerEnabled mob 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)))
|
(.build mob)))
|
||||||
|
|
||||||
(defn disconnect
|
(defn disconnect
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,12 @@
|
||||||
(mg/disconnect conn)))
|
(mg/disconnect conn)))
|
||||||
|
|
||||||
(deftest connect-to-mongo-with-default-host-and-explicit-port
|
(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))))
|
(is (instance? com.mongodb.MongoClient connection))))
|
||||||
|
|
||||||
|
|
||||||
(deftest connect-to-mongo-with-default-port-and-explicit-host
|
(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))))
|
(is (instance? com.mongodb.MongoClient connection))))
|
||||||
|
|
||||||
(deftest test-server-address
|
(deftest test-server-address
|
||||||
|
|
@ -32,13 +32,13 @@
|
||||||
(is (= port (.getPort sa)))))
|
(is (= port (.getPort sa)))))
|
||||||
|
|
||||||
(deftest use-existing-mongo-connection
|
(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)
|
connection (MongoClient. "127.0.0.1" opts)
|
||||||
db (mg/get-db connection "monger-test")]
|
db (mg/get-db connection "monger-test")]
|
||||||
(mg/disconnect connection)))
|
(mg/disconnect connection)))
|
||||||
|
|
||||||
(deftest connect-to-mongo-with-extra-options
|
(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)
|
^ServerAddress sa (server-address "127.0.0.1" 27017)
|
||||||
conn (mg/connect sa opts)]
|
conn (mg/connect sa opts)]
|
||||||
(mg/disconnect conn)))
|
(mg/disconnect conn)))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue