Fix MongoDB::Collection #insert_documents, #update and #remove and MongoDB::Connection #receive_header and #last_error_message: usage strings as much as possible instead of byte arrays, otherwise performance really suffers.

This commit is contained in:
Hongli Lai (Phusion) 2010-09-12 22:24:20 +02:00 committed by Kyle Banker
parent 264bddbeee
commit 0585aa1aae
2 changed files with 11 additions and 9 deletions

View File

@ -284,10 +284,10 @@ module Mongo
# @core remove remove-instance_method
def remove(selector={}, opts={})
# Initial byte is 0.
message = BSON::ByteBuffer.new([0, 0, 0, 0])
message = BSON::ByteBuffer.new("\0\0\0\0")
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
message.put_int(0)
message.put_array(BSON::BSON_CODER.serialize(selector, false, true).to_a)
message.put_binary(BSON::BSON_CODER.serialize(selector, false, true).to_s)
@logger.debug("MONGODB #{@db.name}['#{@name}'].remove(#{selector.inspect})") if @logger
if opts[:safe]
@ -322,14 +322,14 @@ module Mongo
# @core update update-instance_method
def update(selector, document, options={})
# Initial byte is 0.
message = BSON::ByteBuffer.new([0, 0, 0, 0])
message = BSON::ByteBuffer.new("\0\0\0\0")
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
update_options = 0
update_options += 1 if options[:upsert]
update_options += 2 if options[:multi]
message.put_int(update_options)
message.put_array(BSON::BSON_CODER.serialize(selector, false, true).to_a)
message.put_array(BSON::BSON_CODER.serialize(document, false, true).to_a)
message.put_binary(BSON::BSON_CODER.serialize(selector, false, true).to_s)
message.put_binary(BSON::BSON_CODER.serialize(document, false, true).to_s)
@logger.debug("MONGODB #{@db.name}['#{@name}'].update(#{selector.inspect}, #{document.inspect})") if @logger
if options[:safe]
@connection.send_message_with_safe_check(Mongo::Constants::OP_UPDATE, message, @db.name, nil, options[:safe])
@ -679,9 +679,11 @@ module Mongo
# +check_keys+ setting.
def insert_documents(documents, collection_name=@name, check_keys=true, safe=false)
# Initial byte is 0.
message = BSON::ByteBuffer.new([0, 0, 0, 0])
message = BSON::ByteBuffer.new("\0\0\0\0")
BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{collection_name}")
documents.each { |doc| message.put_array(BSON::BSON_CODER.serialize(doc, check_keys, true).to_a) }
documents.each do |doc|
message.put_binary(BSON::BSON_CODER.serialize(doc, check_keys, true).to_s)
end
raise InvalidOperation, "Exceded maximum insert size of 16,000,000 bytes" if message.size > 16_000_000
@logger.debug("MONGODB #{@db.name}['#{collection_name}'].insert(#{documents.inspect})") if @logger

View File

@ -746,7 +746,7 @@ module Mongo
def receive_header(sock)
header = BSON::ByteBuffer.new
header.put_array(receive_message_on_socket(16, sock).unpack("C*"))
header.put_binary(receive_message_on_socket(16, sock))
unless header.size == STANDARD_HEADER_SIZE
raise "Short read for DB response header: " +
"expected #{STANDARD_HEADER_SIZE} bytes, saw #{header.size}"
@ -815,7 +815,7 @@ module Mongo
opts.assert_valid_keys(:w, :wtimeout, :fsync)
cmd.merge!(opts)
end
message.put_array(BSON::BSON_CODER.serialize(cmd, false).unpack("C*"))
message.put_binary(BSON::BSON_CODER.serialize(cmd, false).to_s)
add_message_headers(Mongo::Constants::OP_QUERY, message)
end