From b4d54481799fd396a6dea94ab1dead9f12195b8a Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Tue, 9 Nov 2010 13:34:28 -0500 Subject: [PATCH] Create index concurrency fix --- lib/mongo/collection.rb | 13 +++++++------ test/collection_test.rb | 14 +++++++++----- test/db_test.rb | 4 ---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 22be76c..ad2309f 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -433,17 +433,18 @@ module Mongo } selector.merge!(opts) - insert_documents([selector], Mongo::DB::SYSTEM_INDEX_COLLECTION, false, false) - response = @db.get_last_error + begin + insert_documents([selector], Mongo::DB::SYSTEM_INDEX_COLLECTION, false, true) - if response['err'] - if response['code'] == 11000 && selector[:dropDups] + rescue Mongo::OperationFailure => e + if selector[:dropDups] && e.message =~ /^11000/ # NOP. If the user is intentionally dropping dups, we can ignore duplicate key errors. else raise Mongo::OperationFailure, "Failed to create index #{selector.inspect} with the following error: " + - "#{response['err']}" + "#{e.message}" end end + name end @@ -630,7 +631,7 @@ module Mongo # Rename this collection. # # Note: If operating in auth mode, the client must be authorized as an admin to - # perform this operation. + # perform this operation. # # @param [String] new_name the new name for this collection # diff --git a/test/collection_test.rb b/test/collection_test.rb index fe66e00..e1c6f1c 100644 --- a/test/collection_test.rb +++ b/test/collection_test.rb @@ -130,11 +130,6 @@ class TestCollection < Test::Unit::TestCase end end - def test_safe_responses - response = @@test.insert({:a => 1}, :safe => true) - assert_equal false, response - end - def test_maximum_insert_size docs = [] 16.times do @@ -721,6 +716,15 @@ class TestCollection < Test::Unit::TestCase @collection.create_index([['b', 1], ['a', 1]]) end + should "allow multiple calls to create_index" do + + end + + should "allow creation of multiple indexes" do + assert @collection.create_index([['a', 1]]) + assert @collection.create_index([['a', 1]]) + end + context "with an index created" do setup do @collection.create_index([['b', 1], ['a', 1]]) diff --git a/test/db_test.rb b/test/db_test.rb index 62829aa..b4850fd 100644 --- a/test/db_test.rb +++ b/test/db_test.rb @@ -175,10 +175,6 @@ class DBTest < Test::Unit::TestCase assert_nil @@db.previous_error end - def test_get_last_error_helper - p @@db.get_last_error - end - def test_check_command_response command = {:forceerror => 1} assert_raise OperationFailure do