Implement index operations

This commit is contained in:
Michael S. Klishin 2011-09-10 07:45:47 +04:00
parent 2ab84f2593
commit b90834fdc5
2 changed files with 73 additions and 5 deletions

View file

@ -157,10 +157,54 @@
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.remove coll (to-db-object conditions)))))
;; monger.collection/ensure-index
;; monger.collection/drop-index
;;
;; monger.collection/create-index
;;
(defn create-index
[^String collection, ^Map keys]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.createIndex coll (to-db-object keys))))
;;
;; monger.collection/ensure-index
;;
(defn ensure-index
([^String collection, ^Map keys]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.ensureIndex coll (to-db-object keys))))
([^String collection, ^Map keys, ^String name]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.ensureIndex coll (to-db-object keys) name))))
;;
;; monger.collection/indexes-on
;;
(defn indexes-on
[^String collection]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(from-db-object (.getIndexInfo coll) true)))
;;
;; monger.collection/drop-index
;;
(defn drop-index
[^String collection, ^String name]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.dropIndex coll name)))
(defn drop-indexes
[^String collection]
(.dropIndexes ^DBCollection (.getCollection monger.core/*mongodb-database* collection)))
;;
;; Implementation

View file

@ -1,8 +1,10 @@
(set! *warn-on-reflection* true)
(ns monger.test.collection
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject] [java.util Date])
(:require [monger core collection result util convertion] [clojure stacktrace])
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure]
[java.util Date])
(:require [monger core collection result util convertion]
[clojure stacktrace])
(:use [clojure.test]))
(monger.util/with-ns 'monger.core
@ -239,7 +241,7 @@
;;
;; monger.collection/find
;; find
;;
(deftest find-multiple-documents-when-collection-is-empty
@ -358,3 +360,25 @@
(is (= 1 (monger.collection/count collection)))
(is (= (modified-doc (monger.collection/find-by-id collection doc-id))))
(monger.collection/remove collection)))
;;
;; indexes
;;
(deftest index-operations
(let [collection "libraries"]
(monger.collection/drop-indexes collection)
(is (= "_id_"
(:name (first (monger.collection/indexes-on collection)))))
(is (nil? (second (monger.collection/indexes-on collection))))
(monger.collection/create-index collection { "language" 1 })
(is (= "language_1"
(:name (second (monger.collection/indexes-on collection)))))
(monger.collection/drop-index collection "language_1")
(is (nil? (second (monger.collection/indexes-on collection))))
(monger.collection/ensure-index collection { "language" 1 })
(is (= "language_1"
(:name (second (monger.collection/indexes-on collection)))))
(monger.collection/ensure-index collection { "language" 1 })))