From 0530b165f654f4e99ebdbb8d91f0815ad76bc305 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Tue, 3 Apr 2012 16:42:48 +0400 Subject: [PATCH] Add support for index options + unique indexes for monger.collection/ensure-index and monger.collection/create-index --- ChangeLog.md | 6 ++++++ project.clj | 6 +++++- src/monger/collection.clj | 17 ++++++++++++----- test/monger/test/collection.clj | 7 ++++--- test/monger/test/stress.clj | 2 +- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index df8a8e1..237b55f 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,11 @@ ## Changes between 1.0.0-beta2 and 1.0.0-beta3 +### Index Options support for monger.collection/ensure-index and /create-index + +`monger.collection/ensure-index` and `/create-index` now accept index options as additional argument. +**Breaking change**: 3-arity versions of those functions now become 4-arity versions. + + ### Support serialization of Clojure ratios Documents that contain Clojure ratios (for example, `26/5`) now can be converted to DBObject instances diff --git a/project.clj b/project.clj index c01d13b..186a7b3 100644 --- a/project.clj +++ b/project.clj @@ -5,7 +5,11 @@ :dependencies [[org.clojure/clojure "1.3.0"] [org.mongodb/mongo-java-driver "2.7.3"] [com.novemberain/validateur "1.0.0"]] - :test-selectors {:focus (fn [v] (:focus v))} + :test-selectors {:default (complement :performance) + :focus :focus + :indexing :indexing + :performance :performance + :all (constantly true)} :codox {:exclude [monger.internal.pagination]} :mailing-list {:name "clojure-monger", :archive "https://groups.google.com/group/clojure-monger", diff --git a/src/monger/collection.clj b/src/monger/collection.clj index be130ff..0cb26d6 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -388,16 +388,20 @@ EXAMPLES - ;; Will create an index on \"language\" field + ;; Will create an index on the \"language\" field (monger.collection/create-index collection { \"language\" 1 }) + (monger.collection/create-index collection { \"language\" 1 } { :unique true :name \"unique_language\" }) " ([^String collection ^Map keys] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.createIndex coll (to-db-object keys)))) - ([^DB db ^String collection ^Map keys] + ([^String collection ^Map keys options] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.createIndex coll (to-db-object keys) (to-db-object options)))) + ([^DB db ^String collection ^Map keys ^Map options] (let [^DBCollection coll (.getCollection db collection)] - (.createIndex coll (to-db-object keys))))) + (.createIndex coll (to-db-object keys) (to-db-object options))))) ;; @@ -416,9 +420,12 @@ ([^String collection, ^Map keys] (let [coll ^DBCollection (.getCollection monger.core/*mongodb-database* collection)] (.ensureIndex ^DBCollection coll ^DBObject (to-db-object keys)))) - ([^String collection, ^Map keys, ^String name] + ([^String collection, ^Map keys ^Map options] (let [coll ^DBCollection (.getCollection monger.core/*mongodb-database* collection)] - (.ensureIndex coll ^DBObject (to-db-object keys) ^String name)))) + (.ensureIndex ^DBCollection coll ^DBObject (to-db-object keys) (to-db-object options)))) + ([^String collection ^Map keys ^String name ^Boolean unique?] + (let [coll ^DBCollection (.getCollection monger.core/*mongodb-database* collection)] + (.ensureIndex coll ^DBObject (to-db-object keys) ^String name unique?)))) ;; diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index 1ac1650..5916839 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -75,7 +75,7 @@ ;; indexes ;; -(deftest index-operations +(deftest ^{:indexing true} index-operations (let [collection "libraries"] (mgcol/drop-indexes collection) (is (= "_id_" @@ -86,10 +86,11 @@ (:name (second (mgcol/indexes-on collection))))) (mgcol/drop-index collection "language_1") (is (nil? (second (mgcol/indexes-on collection)))) - (mgcol/ensure-index collection { "language" 1 }) + (mgcol/ensure-index collection { "language" 1 } {:unique true}) (is (= "language_1" (:name (second (mgcol/indexes-on collection))))) - (mgcol/ensure-index collection { "language" 1 }))) + (mgcol/ensure-index collection { "language" 1 }) + (mgcol/ensure-index collection { "language" 1 } { :unique true }))) ;; diff --git a/test/monger/test/stress.clj b/test/monger/test/stress.clj index e0d981b..9efd301 100644 --- a/test/monger/test/stress.clj +++ b/test/monger/test/stress.clj @@ -30,7 +30,7 @@ (monger.core/set-default-write-concern! WriteConcern/NORMAL) -(deftest insert-large-batches-of-documents-without-object-ids +(deftest ^{:performance true} insert-large-batches-of-documents-without-object-ids (doseq [n [1000 10000 100000]] (let [collection "things" docs (map (fn [i]