From cd44c3c9185ca14964e903e4c5fa6adf6ef588e0 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Tue, 17 Nov 2009 13:20:57 -0500 Subject: [PATCH] Fixed index ordering --- README.rdoc | 1 + lib/mongo/collection.rb | 1 + lib/mongo/db.rb | 2 +- lib/mongo/util/ordered_hash.rb | 1 + test/test_collection.rb | 23 +++++++++++++++++++++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index a8e68b4..f49feec 100644 --- a/README.rdoc +++ b/README.rdoc @@ -355,6 +355,7 @@ Sean Cribbs, seancribbs on github Sunny Hirai * Suggested hashcode fix for Mongo::ObjectID +* Noted index ordering bug. = License diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 9bfdfe8..ce6aaa6 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -505,4 +505,5 @@ EOS indexes.join("_") end end + end diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index e6b7521..6bf606d 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -422,7 +422,7 @@ module Mongo sel = {:ns => full_collection_name(collection_name)} info = {} Cursor.new(Collection.new(self, SYSTEM_INDEX_COLLECTION), :selector => sel).each { |index| - info[index['name']] = index['key'].to_a + info[index['name']] = index['key'].map } info end diff --git a/lib/mongo/util/ordered_hash.rb b/lib/mongo/util/ordered_hash.rb index 3432770..2c9ac5b 100644 --- a/lib/mongo/util/ordered_hash.rb +++ b/lib/mongo/util/ordered_hash.rb @@ -69,6 +69,7 @@ class OrderedHash < Hash @ordered_keys.each { |k| yield k, self[k] } self end + alias :each_pair :each def values collect { |k, v| v } diff --git a/test/test_collection.rb b/test/test_collection.rb index 5f3aa4b..2ddf4cb 100644 --- a/test/test_collection.rb +++ b/test/test_collection.rb @@ -398,4 +398,27 @@ class TestCollection < Test::Unit::TestCase assert_equal 1, @collection.size end end + + context "Creating indexes " do + setup do + @collection = @@db.collection('test-collection') + end + + should "generate indexes in the proper order" do + @collection.expects(:insert_documents) do |sel, coll, safe| + assert_equal 'b_1_a_1', sel[:name] + end + @collection.create_index([['b', 1], ['a', 1]]) + end + + context "with an index created" do + setup do + @collection.create_index([['b', 1], ['a', 1]]) + end + + should "return properly ordered index information" do + assert_equal [['b', 1], ['a', 1]], @collection.index_information["b_1_a_1"] + end + end + end end