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"]
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

View File

@ -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+

View File

@ -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