Skip to content

Commit

Permalink
Multi-key indexes respect key order, instead of using alphabetical or…
Browse files Browse the repository at this point in the history
…der.

Oops.
  • Loading branch information
christophermaier committed Apr 17, 2011
1 parent 3cc45ec commit d0be2f8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
12 changes: 6 additions & 6 deletions src/somnium/congomongo/coerce.clj
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@
See also somnium.congomongo/add-index!"
[fields]
(clojure->mongo #^IPersistentMap (apply conj
(sorted-map)
(for [f fields]
(if (vector? f)
f
[f 1])))))
(clojure->mongo #^IPersistentMap (apply array-map
(flatten
(for [f fields]
(if (vector? f)
f
[f 1]))))))
27 changes: 27 additions & 0 deletions test/somnium/congomongo/test/coerce.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns somnium.congomongo.test.coerce
(:use somnium.congomongo.coerce)
(:use clojure.test)
(:import (com.mongodb BasicDBObject) ))

;; insertion order is important for BasicDBObjects, since they extend
;; LinkedHashMap. The only way to test this is to compare their JSON
;; serializations, accessible through .toString
(deftest test-coerce-index-fields
(are [fields bdo] (is (= (.toString (coerce-index-fields fields))
(.toString bdo)))
[:a :b :c] (doto (BasicDBObject.)
(.put "a" 1)
(.put "b" 1)
(.put "c" 1))
[:c :b :a] (doto (BasicDBObject.)
(.put "c" 1)
(.put "b" 1)
(.put "a" 1))
[:a [:b -1] :c] (doto (BasicDBObject.)
(.put "a" 1)
(.put "b" -1)
(.put "c" 1))
[:c :b [:a -1]] (doto (BasicDBObject.)
(.put "c" 1)
(.put "b" 1)
(.put "a" -1))))
28 changes: 2 additions & 26 deletions test/somnium/test/congomongo.clj
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +169,6 @@
(is (some #(= (into {} (% "key")) {"x" 1})
(get-indexes :points)))))

(defn- index-keys-in-same-order
[a b]
(every? identity (map #(= %1 %2) (.keySet a) (.keySet b))))


(deftest test-coerce-index-fields
(let [objects-have-same-data (fn [a b] (= a b))]
(let [bdo (doto (BasicDBObject.)
(.put "a" 1)
(.put "b" 1)
(.put "c" 1))
index (coerce-index-fields [:a :b :c])]
(is (objects-have-same-data bdo index))
(is (index-keys-in-same-order bdo index)))
(let [bdo (doto (BasicDBObject.)
(.put "a" 1)
(.put "b" -1)
(.put "c" 1))
index (coerce-index-fields [:a [:b -1] :c])]
(is (objects-have-same-data bdo index))
(is (index-keys-in-same-order bdo index)))))

(defn- get-named-index [coll name]
(first (filter #(= (get % "name") name)
(get-indexes coll))))
Expand All @@ -205,8 +183,7 @@
(.put "a" 1)
(.put "b" 1)
(.put "c" 1))]
(is (= actual-index expected-index))
(is (index-keys-in-same-order actual-index expected-index)))
(is (= (.toString actual-index) (.toString expected-index))))

(add-index! :testing-indexes [:a [:b -1] :c])
(let [auto-generated-index-name "a_1_b_-1_c_1"
Expand All @@ -216,8 +193,7 @@
(.put "a" 1)
(.put "b" -1)
(.put "c" 1))]
(is (= actual-index expected-index))
(is (index-keys-in-same-order actual-index expected-index)))))
(is (= (.toString actual-index) (.toString expected-index))))))

(deftest index-name
(with-test-mongo
Expand Down

0 comments on commit d0be2f8

Please sign in to comment.