diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index d54d7f0..2d23b91 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -190,14 +190,10 @@ EOS }))["result"] end - # Return an array of hashes, one for each index. Each hash contains: - # - # :name :: Index name - # - # :keys :: Hash whose keys are the names of the fields that make up - # the key and values are integers. - # - # :ns :: Namespace; same as this collection's name. + # Get information on the indexes for the collection +collection_name+. + # Returns a hash where the keys are index names (as returned by + # Collection#create_index and the values are lists of [key, direction] + # pairs specifying the index (as passed to Collection#create_index). def index_information @db.index_information(@name) end diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 78bb5b1..69c9494 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -425,31 +425,18 @@ module XGen raise "Error with drop_index command: #{doc.inspect}" unless ok?(doc) end - # Return an array of hashes, one for each index on +collection_name+. - # Normally called by Collection#index_information. Each hash contains: - # - # :name :: Index name - # - # :keys :: Hash whose keys are the names of the fields that make up - # the key and values are integers. - # - # :ns :: Namespace; same as +collection_name+. + # Get information on the indexes for the collection +collection_name+. + # Normally called by Collection#index_information. Returns a hash where + # the keys are index names (as returned by Collection#create_index and + # the values are lists of [key, direction] pairs specifying the index + # (as passed to Collection#create_index). def index_information(collection_name) sel = {:ns => full_coll_name(collection_name)} - query(Collection.new(self, SYSTEM_INDEX_COLLECTION), Query.new(sel)).collect { |row| - h = {:name => row['name']} - raise "Name of index on return from db was nil. Coll = #{full_coll_name(collection_name)}" unless h[:name] - - h[:keys] = row['key'] - raise "Keys for index on return from db was nil. Coll = #{full_coll_name(collection_name)}" unless h[:keys] - - h[:ns] = row['ns'] - raise "Namespace for index on return from db was nil. Coll = #{full_coll_name(collection_name)}" unless h[:ns] - h[:ns].sub!(/.*\./, '') - raise "Error: ns != collection" unless h[:ns] == collection_name - - h + info = {} + query(Collection.new(self, SYSTEM_INDEX_COLLECTION), Query.new(sel)).each { |index| + info[index['name']] = index['key'].to_a } + info end # Create a new index on +collection_name+. +field_or_spec+ diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 8547aac..8403bd5 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -282,29 +282,28 @@ class DBAPITest < Test::Unit::TestCase end def test_index_information - name = @@db.create_index(@@coll.name, 'a') - list = @@db.index_information(@@coll.name) - assert_equal @@coll.index_information, list - assert_equal 2, list.length + assert_equal @@coll.index_information.length, 1 - info = list[1] - assert_equal name, 'a_1' - assert_equal name, info[:name] - assert_equal 1, info[:keys]['a'] + name = @@db.create_index(@@coll.name, 'a') + info = @@db.index_information(@@coll.name) + assert_equal name, "a_1" + assert_equal @@coll.index_information, info + assert_equal 2, info.length + + assert info.has_key? name + assert_equal info[name], [["a", ASCENDING]] ensure @@db.drop_index(@@coll.name, name) end def test_multiple_index_cols name = @@db.create_index(@@coll.name, [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]]) - list = @@db.index_information(@@coll.name) - assert_equal 2, list.length + info = @@db.index_information(@@coll.name) + assert_equal 2, info.length - info = list[1] assert_equal name, 'a_-1_b_1_c_-1' - assert_equal name, info[:name] - keys = info[:keys].keys - assert_equal ['a', 'b', 'c'], keys.sort + assert info.has_key? name + assert_equal [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]], info[name] ensure @@db.drop_index(@@coll.name, name) end