Initial implementation of inserts with write concerns and batch inserts

This commit is contained in:
Michael S. Klishin 2011-08-14 06:51:14 +04:00
parent 97f537d176
commit 3bf2284611
2 changed files with 82 additions and 3 deletions

View file

@ -19,14 +19,28 @@
;; THE SOFTWARE.
(ns monger.collection
(:import (com.mongodb Mongo DB))
)
(:import (com.mongodb Mongo DB WriteResult DBObject WriteConcern) (java.util List))
(:require [monger core convertion errors]))
;;
;; API
;;
;; monger.collection/insert
(defn ^WriteResult insert
([^DB db, ^String collection, ^DBObject doc]
(.insert (.getCollection db collection) (monger.convertion/to-db-object doc) WriteConcern/NORMAL))
([^DB db, ^String collection, ^DBObject doc, ^WriteConcern concern]
(.insert (.getCollection db collection) (monger.convertion/to-db-object doc) concern)))
(defn ^WriteResult insert-batch
([^DB db, ^String collection, ^List docs]
(.insert (.getCollection db collection) (monger.convertion/to-db-object docs) WriteConcern/NORMAL))
([^DB db, ^String collection, ^List docs, ^WriteConcern concern]
(.insert (.getCollection db collection) (monger.convertion/to-db-object docs) concern)))
;; monger.collection/find
;; monger.collection/group
@ -39,5 +53,12 @@
;; monger.collection/update-multi
;; monger.collection/remove
(defn ^WriteResult remove
([^DB db, ^String collection]
(.remove (.getCollection db collection) (monger.convertion/to-db-object {})))
([^DB db, ^String collection, ^DBObject conditions]
(.remove (.getCollection db collection) (monger.convertion/to-db-object conditions)))
)
;; monger.collection/ensure-index
;; monger.collection/drop-index

View file

@ -1,7 +1,65 @@
(set! *warn-on-reflection* true)
(ns monger.test.collection
(:require [monger core collection])
(:import [com.mongodb WriteResult WriteConcern])
(:require [monger core collection errors] [clojure stacktrace])
(:use [clojure.test]))
;;
;; insert
;;
(deftest insert-a-basic-document-without-id-and-with-default-write-concern
(let [connection (monger.core/connect)
db (monger.core/get-db connection "monger-test")
collection "people"
doc { :name "Joe", :age 30 }]
(monger.collection/remove db collection)
(is (monger.errors/ok? (monger.collection/insert db "people" doc)))
(is (= 1 (monger.collection/count db collection)))))
(deftest insert-a-basic-document-without-id-and-with-explicit-write-concern
(let [connection (monger.core/connect)
db (monger.core/get-db connection "monger-test")
collection "people"
doc { :name "Joe", :age 30 }]
(monger.collection/remove db collection)
(is (monger.errors/ok? (monger.collection/insert db "people" doc WriteConcern/SAFE)))
(is (= 1 (monger.collection/count db collection)))))
;;
;; insert
;;
(deftest insert-a-batch-of-basic-documents-without-ids-and-with-default-write-concern
(let [connection (monger.core/connect)
db (monger.core/get-db connection "monger-test")
collection "people"
docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]]
(monger.collection/remove db collection)
(is (monger.errors/ok? (monger.collection/insert-batch db "people" docs)))
(is (= 2 (monger.collection/count db collection)))))
(deftest insert-a-batch-of-basic-documents-without-ids-and-with-explicit-write-concern
(let [connection (monger.core/connect)
db (monger.core/get-db connection "monger-test")
collection "people"
docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]]
(monger.collection/remove db collection)
(is (monger.errors/ok? (monger.collection/insert-batch db "people" docs WriteConcern/NORMAL)))
(is (= 2 (monger.collection/count db collection)))))
;;
;; count
;;
(deftest get-collection-size
(let [connection (monger.core/connect)
db (monger.core/get-db connection "monger-test")]