Merge branch 'rs'
This commit is contained in:
commit
87abd62615
|
@ -256,8 +256,7 @@ module Mongo
|
|||
# a document (as a hash) or array of documents to be inserted.
|
||||
#
|
||||
# @return [ObjectId, Array]
|
||||
# the _id of the inserted document or a list of _ids of all inserted documents.
|
||||
# Note: the object may have been modified by the database's PK factory, if it has one.
|
||||
# The _id of the inserted document or a list of _ids of all inserted documents.
|
||||
#
|
||||
# @option opts [Boolean, Hash] :safe (+false+)
|
||||
# run the operation in safe mode, which run a getlasterror command on the
|
||||
|
@ -298,7 +297,8 @@ module Mongo
|
|||
# @example remove only documents that have expired:
|
||||
# users.remove({:expire => {"$lte" => Time.now}})
|
||||
#
|
||||
# @return [True]
|
||||
# @return [Hash, true] Returns a Hash containing the last error object if running in safe mode.
|
||||
# Otherwise, returns true.
|
||||
#
|
||||
# @raise [Mongo::OperationFailure] an exception will be raised iff safe mode is enabled
|
||||
# and the operation fails.
|
||||
|
@ -317,15 +317,12 @@ module Mongo
|
|||
@logger.debug("MONGODB #{@db.name}['#{@name}'].remove(#{selector.inspect})") if @logger
|
||||
if safe
|
||||
@connection.send_message_with_safe_check(Mongo::Constants::OP_DELETE, message, @db.name, nil, safe)
|
||||
# the return value of send_message_with_safe_check isn't actually meaningful --
|
||||
# only the fact that it didn't raise an error is -- so just return true
|
||||
true
|
||||
else
|
||||
@connection.send_message(Mongo::Constants::OP_DELETE, message)
|
||||
end
|
||||
end
|
||||
|
||||
# Update a single document in this collection.
|
||||
# Update one or more documents in this collection.
|
||||
#
|
||||
# @param [Hash] selector
|
||||
# a hash specifying elements which must be present for a document to be updated. Note:
|
||||
|
@ -346,6 +343,9 @@ module Mongo
|
|||
# options set on this collection, its database object, or the current collection.
|
||||
# See the options for DB#get_last_error for details.
|
||||
#
|
||||
# @return [Hash, true] Returns a Hash containing the last error object if running in safe mode.
|
||||
# Otherwise, returns true.
|
||||
#
|
||||
# @core update update-instance_method
|
||||
def update(selector, document, options={})
|
||||
# Initial byte is 0.
|
||||
|
@ -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
|
||||
#
|
||||
|
|
|
@ -422,9 +422,7 @@ module Mongo
|
|||
#
|
||||
# @see DB#get_last_error for valid last error params.
|
||||
#
|
||||
# @return [Array]
|
||||
# An array whose indexes include [0] documents returned, [1] number of document received,
|
||||
# and [3] a cursor_id.
|
||||
# @return [Hash] The document returned by the call to getlasterror.
|
||||
def send_message_with_safe_check(operation, message, db_name, log_message=nil, last_error_params=false)
|
||||
message_with_headers = add_message_headers(operation, message)
|
||||
message_with_check = last_error_message(db_name, last_error_params)
|
||||
|
@ -439,11 +437,13 @@ module Mongo
|
|||
ensure
|
||||
checkin(sock)
|
||||
end
|
||||
|
||||
if num_received == 1 && (error = docs[0]['err'] || docs[0]['errmsg'])
|
||||
close if error == "not master"
|
||||
raise Mongo::OperationFailure, error
|
||||
raise Mongo::OperationFailure, docs[0]['code'].to_s + ': ' + error
|
||||
end
|
||||
[docs, num_received, cursor_id]
|
||||
|
||||
docs[0]
|
||||
end
|
||||
|
||||
# Sends a message to the database and waits for the response.
|
||||
|
|
|
@ -556,7 +556,7 @@ class TestCollection < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
context "Grouping" do
|
||||
setup do
|
||||
setup do
|
||||
@@test.remove
|
||||
@@test.save("a" => 1)
|
||||
@@test.save("b" => 1)
|
||||
|
@ -576,6 +576,23 @@ class TestCollection < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
context "Grouping with key" do
|
||||
setup do
|
||||
@@test.remove
|
||||
@@test.save("a" => 1, "pop" => 100)
|
||||
@@test.save("a" => 1, "pop" => 100)
|
||||
@@test.save("a" => 2, "pop" => 100)
|
||||
@@test.save("a" => 2, "pop" => 100)
|
||||
@initial = {"count" => 0, "foo" => 1}
|
||||
@reduce_function = "function (obj, prev) { prev.count += obj.pop; }"
|
||||
end
|
||||
|
||||
should "group" do
|
||||
result = @@test.group([:a], {}, @initial, @reduce_function, nil)
|
||||
assert result.all? { |r| r['count'] == 200 }
|
||||
end
|
||||
end
|
||||
|
||||
context "Grouping with a key function" do
|
||||
setup do
|
||||
@@test.remove
|
||||
|
@ -699,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]])
|
||||
|
|
|
@ -2,7 +2,7 @@ require './test/test_helper'
|
|||
include Mongo
|
||||
|
||||
class SafeTest < Test::Unit::TestCase
|
||||
context "Safe tests: " do
|
||||
context "Safe mode propogation: " do
|
||||
setup do
|
||||
@con = standard_connection(:safe => {:w => 1})
|
||||
@db = @con[MONGO_TEST_DB]
|
||||
|
@ -39,4 +39,30 @@ class SafeTest < Test::Unit::TestCase
|
|||
@col.update({:a => 2}, {:a => 1}, :safe => false)
|
||||
end
|
||||
end
|
||||
|
||||
context "Safe error objects" do
|
||||
setup do
|
||||
@con = standard_connection
|
||||
@db = @con[MONGO_TEST_DB]
|
||||
@col = @db['test']
|
||||
@col.remove
|
||||
@col.insert({:a => 1})
|
||||
@col.insert({:a => 1})
|
||||
@col.insert({:a => 1})
|
||||
end
|
||||
|
||||
should "return object on update" do
|
||||
response = @col.update({:a => 1}, {"$set" => {:a => 2}},
|
||||
:multi => true, :safe => true)
|
||||
|
||||
assert response['updatedExisting']
|
||||
assert_equal 3, response['n']
|
||||
end
|
||||
|
||||
should "return object on remove" do
|
||||
response = @col.remove({}, :safe => true)
|
||||
assert_equal 3, response['n']
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue