Skip to content

Commit

Permalink
Merge pull request #44 from apa512/connect-in-query
Browse files Browse the repository at this point in the history
Import `connect` into query, connect docstring improvements
  • Loading branch information
danielcompton committed Jul 1, 2015
2 parents 5ed7ec8 + dabbbb3 commit 2cde5a1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ A RethinkDB client for Clojure. Tested with 1.16.x but should with work all vers
## Usage

```clojure
(require '[rethinkdb.core :refer [connect close]])
(require '[rethinkdb.query :as r])

(with-open [conn (connect :host "127.0.0.1" :port 28015 :db "test")]
(with-open [conn (r/connect :host "127.0.0.1" :port 28015 :db "test")]
(r/run (r/db-create "test") conn)

(-> (r/db "test")
Expand Down
5 changes: 4 additions & 1 deletion src/rethinkdb/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
(send-int out n 4)
(send-str out auth-key)))

(defn close [conn]
(defn close
"Closes RethinkDB database connection, stops all running queries
and waits for response before returning"
[conn]
(let [{:keys [socket out in waiting]} @conn]
(doseq [token waiting]
(send-stop-query conn token))
Expand Down
11 changes: 11 additions & 0 deletions src/rethinkdb/query.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(:require [clojure.data.json :as json]
[clojure.walk :refer [postwalk postwalk-replace]]
[rethinkdb.net :refer [send-start-query] :as net]
[rethinkdb.core :as core]
[rethinkdb.query-builder :refer [term parse-term]]))

(defmacro fn [args & [body]]
Expand All @@ -13,6 +14,16 @@
new-terms (postwalk-replace new-replacements body)]
(term :FUNC [new-args new-terms])))

;;; Import connect

(def connect
"Creates a database connection to a RethinkDB host
[& {:keys [host port token auth-key]
:or {host \"127.0.0.1\"
port 28015
token 0
auth-key \"\"}}" core/connect)

;;; Cursors

(defn close [cursor]
Expand Down
19 changes: 9 additions & 10 deletions test/rethinkdb/connection_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns rethinkdb.connection-test
(:require [clj-time.core :as t]
[clojure.test :refer :all]
[rethinkdb.core :refer :all]
[rethinkdb.query :as r]))

;;; Reference performance
Expand All @@ -19,7 +18,7 @@
(def test-db "cljrethinkdb_test")

(defn setup [test-fn]
(with-open [conn (connect)]
(with-open [conn (r/connect)]
(if (some #{test-db} (r/run (r/db-list) conn))
(r/run (r/db-drop test-db) conn))
(r/run (r/db-create test-db) conn)
Expand All @@ -35,20 +34,20 @@
;; Uncomment to run test
(deftest connection-speed-test
(println "performance (connection per query)")
(let [conn connect]
(let [conn r/connect]
(time
(doseq [n (range 100)]
(with-open [conn (connect)]
(with-open [conn (r/connect)]
(r/run query conn)))))

(println "performance (reusing connection")
(time
(with-open [conn (connect)]
(with-open [conn (r/connect)]
(doseq [n (range 100)]
(r/run query conn))))

(println "performance (parallel, one connection)")
(with-open [conn (connect)]
(with-open [conn (r/connect)]
(time
(doall
(pmap (fn [v] (r/run query conn))
Expand All @@ -59,16 +58,16 @@
nil)

(println "multiple connection test")
(let [conn1 (connect)
conn2 (connect)
conn3 (connect)]
(let [conn1 (r/connect)
conn2 (r/connect)
conn3 (r/connect)]
(r/run query conn1)
(future
(do
(r/run query conn2)
(.close conn1)))
(future
(with-open [conn (connect)]
(with-open [conn (r/connect)]
(r/run query conn)))
(future
(.close conn2))
Expand Down
15 changes: 9 additions & 6 deletions test/rethinkdb/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
(ns rethinkdb.core-test
(:require [clj-time.core :as t]
[clojure.test :refer :all]
[rethinkdb.core :refer :all]
[rethinkdb.query :as r]))


(def conn (connect))
(def conn (r/connect))
(def test-db "cljrethinkdb_test")

(defmacro db-run [& body]
Expand Down Expand Up @@ -54,7 +53,7 @@
(r/eq (r/get-field row :name) name)))))

(deftest core-test
(with-open [conn (connect)]
(with-open [conn (r/connect)]
(testing "manipulating databases"
(is (= 1 (:dbs_created (r/run (r/db-create "cljrethinkdb_tmp") conn))))
(is (= 1 (:dbs_dropped (r/run (r/db-drop "cljrethinkdb_tmp") conn))))
Expand Down Expand Up @@ -103,11 +102,11 @@
(is (= (run (with-name "Pikachu")) [(first pokemons)])))

(testing "run a query with an implicit database"
(with-open [conn-implicit-db (connect :db test-db)]
(with-open [conn-implicit-db (r/connect :db test-db)]
(is (= (set (-> (r/table :pokedex) (r/run conn-implicit-db)))
(set pokemons))))
(testing "precedence of db connections"
(with-open [conn-implicit-db (connect :db "nonexistent_db")]
(with-open [conn-implicit-db (r/connect :db "nonexistent_db")]
(is (= (set (-> (r/db test-db) (r/table :pokedex) (r/run conn-implicit-db)))
(set pokemons))))))

Expand All @@ -119,7 +118,7 @@
(r/sum [3 4]) 7))

(testing "feeds"
(let [tmp-conn (connect)
(let [tmp-conn (r/connect)
changes (future
(-> (r/db test-db)
(r/table :pokedex)
Expand Down Expand Up @@ -265,4 +264,8 @@
(r/get-field :name)))))))
(.close conn)))

(deftest query-conn
(is (do (r/connect)
true)))

(use-fixtures :once setup)

0 comments on commit 2cde5a1

Please sign in to comment.