2009-02-05 19:24:59 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require File.join(File.dirname(__FILE__), '_common.rb')
|
2009-02-27 14:23:13 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2009-02-27 14:23:13 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
db = Mongo::Mongo.new(DEFAULT_HOST, DEFAULT_PORT).db(DEFAULT_DB)
|
2009-02-05 19:24:59 +00:00
|
|
|
x = db.collection('x')
|
|
|
|
y = db.collection('y')
|
|
|
|
|
2009-02-06 15:48:38 +00:00
|
|
|
def sorted_index_info(c)
|
|
|
|
c.index_information.sort { |a,b| a[:name] <=> b[:name] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def sorted_info_keys(info)
|
|
|
|
info[:keys].keys.sort.collect { |key| "#{key}_1" }.join("_")
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_keys(c, expected)
|
|
|
|
keys = sorted_index_info(c).collect {|info| sorted_info_keys(info)}
|
|
|
|
if keys == expected
|
|
|
|
''
|
|
|
|
else
|
|
|
|
"#{c.name} indices should start out #{expected.inspect} but is #{keys.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-05 21:00:45 +00:00
|
|
|
if $DEBUG
|
2009-02-06 15:57:34 +00:00
|
|
|
x.drop # also drops indexes
|
2009-02-05 19:47:33 +00:00
|
|
|
x.insert('field1' => 'f1', 'field2' => 'f2')
|
2009-02-05 21:00:45 +00:00
|
|
|
x.create_index('field1_1', 'field1')
|
|
|
|
x.create_index('field2_1', 'field2')
|
2009-02-06 15:57:34 +00:00
|
|
|
y.drop
|
2009-02-05 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2009-02-06 15:48:38 +00:00
|
|
|
# There should only be two indices on x, and none on y. We raise an error if
|
|
|
|
# the preconditions are not met. (They were not, on our testing harness, for a
|
|
|
|
# while due to Mongo behavior.)
|
|
|
|
err = []
|
|
|
|
err << check_keys(x, ['field1_1', 'field2_1'])
|
|
|
|
err << check_keys(y, [])
|
|
|
|
raise "\n#{err.join("\n")}" unless err == ['', '']
|
2009-02-05 19:24:59 +00:00
|
|
|
|
2009-02-05 21:00:45 +00:00
|
|
|
x.drop_index('field1_1')
|
2009-02-06 15:48:38 +00:00
|
|
|
sorted_index_info(x).each { |info| puts sorted_info_keys(info) }
|
2009-02-05 19:24:59 +00:00
|
|
|
|
2009-02-27 14:23:13 +00:00
|
|
|
y.create_index([['a', ASCENDING], ['b', ASCENDING], ['c', ASCENDING]])
|
|
|
|
y.create_index('d')
|
2009-02-06 15:48:38 +00:00
|
|
|
sorted_index_info(y).each { |info| puts sorted_info_keys(info) }
|