API CHANGE: better, less redundant API for index_information

This commit is contained in:
Mike Dirolf 2009-06-02 11:24:52 -04:00
parent d87a7da617
commit 25e960441b
3 changed files with 26 additions and 44 deletions

View File

@ -190,14 +190,10 @@ EOS
}))["result"] }))["result"]
end end
# Return an array of hashes, one for each index. Each hash contains: # Get information on the indexes for the collection +collection_name+.
# # Returns a hash where the keys are index names (as returned by
# :name :: Index name # Collection#create_index and the values are lists of [key, direction]
# # pairs specifying the index (as passed to Collection#create_index).
# :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.
def index_information def index_information
@db.index_information(@name) @db.index_information(@name)
end end

View File

@ -425,31 +425,18 @@ module XGen
raise "Error with drop_index command: #{doc.inspect}" unless ok?(doc) raise "Error with drop_index command: #{doc.inspect}" unless ok?(doc)
end end
# Return an array of hashes, one for each index on +collection_name+. # Get information on the indexes for the collection +collection_name+.
# Normally called by Collection#index_information. Each hash contains: # Normally called by Collection#index_information. Returns a hash where
# # the keys are index names (as returned by Collection#create_index and
# :name :: Index name # the values are lists of [key, direction] pairs specifying the index
# # (as passed to Collection#create_index).
# :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+.
def index_information(collection_name) def index_information(collection_name)
sel = {:ns => full_coll_name(collection_name)} sel = {:ns => full_coll_name(collection_name)}
query(Collection.new(self, SYSTEM_INDEX_COLLECTION), Query.new(sel)).collect { |row| info = {}
h = {:name => row['name']} query(Collection.new(self, SYSTEM_INDEX_COLLECTION), Query.new(sel)).each { |index|
raise "Name of index on return from db was nil. Coll = #{full_coll_name(collection_name)}" unless h[:name] info[index['name']] = index['key'].to_a
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
end end
# Create a new index on +collection_name+. +field_or_spec+ # Create a new index on +collection_name+. +field_or_spec+

View File

@ -282,29 +282,28 @@ class DBAPITest < Test::Unit::TestCase
end end
def test_index_information def test_index_information
name = @@db.create_index(@@coll.name, 'a') assert_equal @@coll.index_information.length, 1
list = @@db.index_information(@@coll.name)
assert_equal @@coll.index_information, list
assert_equal 2, list.length
info = list[1] name = @@db.create_index(@@coll.name, 'a')
assert_equal name, 'a_1' info = @@db.index_information(@@coll.name)
assert_equal name, info[:name] assert_equal name, "a_1"
assert_equal 1, info[:keys]['a'] assert_equal @@coll.index_information, info
assert_equal 2, info.length
assert info.has_key? name
assert_equal info[name], [["a", ASCENDING]]
ensure ensure
@@db.drop_index(@@coll.name, name) @@db.drop_index(@@coll.name, name)
end end
def test_multiple_index_cols def test_multiple_index_cols
name = @@db.create_index(@@coll.name, [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]]) name = @@db.create_index(@@coll.name, [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]])
list = @@db.index_information(@@coll.name) info = @@db.index_information(@@coll.name)
assert_equal 2, list.length assert_equal 2, info.length
info = list[1]
assert_equal name, 'a_-1_b_1_c_-1' assert_equal name, 'a_-1_b_1_c_-1'
assert_equal name, info[:name] assert info.has_key? name
keys = info[:keys].keys assert_equal [['a', DESCENDING], ['b', ASCENDING], ['c', DESCENDING]], info[name]
assert_equal ['a', 'b', 'c'], keys.sort
ensure ensure
@@db.drop_index(@@coll.name, name) @@db.drop_index(@@coll.name, name)
end end