RUBY-192; Updates for Collection#ensure_index
This commit is contained in:
parent
bc5dcdfc7f
commit
ae202d590e
|
@ -415,6 +415,7 @@ module Mongo
|
|||
opts[:dropDups] = opts.delete(:drop_dups) if opts[:drop_dups]
|
||||
field_spec = parse_index_spec(spec)
|
||||
name = opts.delete(:name) || generate_index_name(field_spec)
|
||||
name = name.to_s if name
|
||||
|
||||
generate_indexes(field_spec, name, opts)
|
||||
name
|
||||
|
@ -437,18 +438,15 @@ module Mongo
|
|||
#
|
||||
# @return [String] the name of the index.
|
||||
def ensure_index(spec, opts={})
|
||||
valid = BSON::OrderedHash.new
|
||||
now = Time.now.utc.to_i
|
||||
field_spec = parse_index_spec(spec)
|
||||
|
||||
field_spec.each do |key, value|
|
||||
cache_key = generate_index_name({key => value})
|
||||
timeout = @cache[cache_key] || 0
|
||||
valid[key] = value if timeout <= now
|
||||
end
|
||||
name = opts.delete(:name) || generate_index_name(field_spec)
|
||||
name = name.to_s if name
|
||||
|
||||
name = opts.delete(:name) || generate_index_name(valid)
|
||||
generate_indexes(valid, name, opts) if valid.any?
|
||||
if !@cache[name] || @cache[name] <= now
|
||||
generate_indexes(field_spec, name, opts)
|
||||
end
|
||||
|
||||
# Reset the cache here in case there are any errors inserting. Best to be safe.
|
||||
@cache[name] = now + @cache_time
|
||||
|
@ -461,7 +459,7 @@ module Mongo
|
|||
#
|
||||
# @core indexes
|
||||
def drop_index(name)
|
||||
@cache[name] = nil
|
||||
@cache[name.to_s] = nil
|
||||
@db.drop_index(@name, name)
|
||||
end
|
||||
|
||||
|
|
|
@ -403,7 +403,7 @@ module Mongo
|
|||
def drop_index(collection_name, index_name)
|
||||
oh = BSON::OrderedHash.new
|
||||
oh[:deleteIndexes] = collection_name
|
||||
oh[:index] = index_name
|
||||
oh[:index] = index_name.to_s
|
||||
doc = command(oh, :check_response => false)
|
||||
ok?(doc) || raise(MongoDBError, "Error with drop_index command: #{doc.inspect}")
|
||||
end
|
||||
|
|
|
@ -600,6 +600,23 @@ class TestCollection < Test::Unit::TestCase
|
|||
@@test.drop_index("a_1")
|
||||
end
|
||||
|
||||
def test_ensure_index_timeout
|
||||
@@db.cache_time = 2
|
||||
coll = @@db['ensure_test']
|
||||
coll.expects(:generate_indexes).twice
|
||||
coll.ensure_index([['a', 1]])
|
||||
|
||||
# These will be cached
|
||||
coll.ensure_index([['a', 1]])
|
||||
coll.ensure_index([['a', 1]])
|
||||
coll.ensure_index([['a', 1]])
|
||||
coll.ensure_index([['a', 1]])
|
||||
|
||||
sleep(3)
|
||||
# This won't be, so generate_indexes will be called twice
|
||||
coll.ensure_index([['a', 1]])
|
||||
end
|
||||
|
||||
context "Grouping" do
|
||||
setup do
|
||||
@@test.remove
|
||||
|
@ -693,6 +710,17 @@ class TestCollection < Test::Unit::TestCase
|
|||
@geo = @@db.collection('geo')
|
||||
end
|
||||
|
||||
should "create index using symbols" do
|
||||
@collection.create_index :foo, :name => :bar
|
||||
@geo.create_index :goo, :name => :baz
|
||||
assert @collection.index_information['bar']
|
||||
@collection.drop_index :bar
|
||||
assert_nil @collection.index_information['bar']
|
||||
assert @geo.index_information['baz']
|
||||
@geo.drop_index(:baz)
|
||||
assert_nil @geo.index_information['baz']
|
||||
end
|
||||
|
||||
should "create a geospatial index" do
|
||||
@geo.save({'loc' => [-100, 100]})
|
||||
@geo.create_index([['loc', Mongo::GEO2D]])
|
||||
|
|
|
@ -113,7 +113,6 @@ class CollectionTest < Test::Unit::TestCase
|
|||
|
||||
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
||||
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
||||
|
||||
end
|
||||
|
||||
should "call generate_indexes for each key when calling ensure_indexes" do
|
||||
|
|
Loading…
Reference in New Issue