Sends a message to the database and waits for the response.
@param [Integer] operation a MongoDB opcode. @param [BSON::ByteBuffer] message a message to send to the database. @param [String] log_message this is currently a no-op and will be removed. @param [Socket] socket a socket to use in lieu of checking out a new one. @param [Boolean] command (false) indicate whether this is a command. If this is a command,
the message will be sent to the primary node.
@param [Symbol] read the read preference. @param [Boolean] exhaust (false) indicate whether the cursor should be exhausted. Set
this to true only when the OP_QUERY_EXHAUST flag is set.
@param [Boolean] compile_regex whether BSON regex objects should be compiled into Ruby regexes.
@return [Array]
An array whose indexes include [0] documents returned, [1] number of document received, and [3] a cursor_id.
# File lib/mongo/networking.rb, line 133 def receive_message(operation, message, log_message=nil, socket=nil, command=false, read=:primary, exhaust=false, compile_regex=true) request_id = add_message_headers(message, operation) packed_message = message.to_s opts = { :exhaust => exhaust, :compile_regex => compile_regex } result = '' begin send_message_on_socket(packed_message, socket) result = receive(socket, request_id, opts) rescue ConnectionFailure => ex socket.close checkin(socket) raise ex rescue SystemStackError, NoMemoryError, SystemCallError => ex close raise ex rescue Exception => ex if defined?(IRB) close if ex.class == IRB::Abort end raise ex end result end
Send a message to MongoDB, adding the necessary headers.
@param [Integer] operation a MongoDB opcode. @param [BSON::ByteBuffer] message a message to send to the database.
@option opts [Symbol] :connection (:writer) The connection to which
this message should be sent. Valid options are :writer and :reader.
@return [Integer] number of bytes sent
# File lib/mongo/networking.rb, line 33 def send_message(operation, message, opts={}) if opts.is_a?(String) warn "MongoClient#send_message no longer takes a string log message. " + "Logging is now handled within the Collection and Cursor classes." opts = {} end add_message_headers(message, operation) packed_message = message.to_s sock = nil pool = opts.fetch(:pool, nil) begin if pool #puts "send_message pool.port:#{pool.port}" sock = pool.checkout else sock ||= checkout_writer end send_message_on_socket(packed_message, sock) rescue SystemStackError, NoMemoryError, SystemCallError => ex close raise ex ensure if sock sock.checkin end end true end
Sends a message to the database, waits for a response, and raises an exception if the operation has failed.
@param [Integer] operation a MongoDB opcode. @param [BSON::ByteBuffer] message a message to send to the database. @param [String] db_name the name of the database. used on call to get_last_error. @param [String] log_message this is currently a no-op and will be removed. @param [Hash] write_concern write concern.
@see DB#get_last_error for valid last error params.
@return [Hash] The document returned by the call to getlasterror.
# File lib/mongo/networking.rb, line 76 def send_message_with_gle(operation, message, db_name, log_message=nil, write_concern=false) docs = num_received = cursor_id = '' add_message_headers(message, operation) last_error_message = build_get_last_error_message(db_name, write_concern) last_error_id = add_message_headers(last_error_message, Mongo::Constants::OP_QUERY) packed_message = message.append!(last_error_message).to_s sock = nil begin sock = checkout_writer send_message_on_socket(packed_message, sock) docs, num_received, cursor_id = receive(sock, last_error_id) checkin(sock) rescue ConnectionFailure, OperationFailure, OperationTimeout => ex checkin(sock) raise ex rescue SystemStackError, NoMemoryError, SystemCallError => ex close raise ex end if num_received == 1 error = docs[0]['err'] || docs[0]['errmsg'] if error && error.include?("not master") close raise ConnectionFailure.new(docs[0]['code'].to_s + ': ' + error, docs[0]['code'], docs[0]) elsif (!error.nil? && note = docs[0]['jnote'] || docs[0]['wnote']) # assignment code = docs[0]['code'] || Mongo::ErrorCode::BAD_VALUE # as of server version 2.5.5 raise WriteConcernError.new(code.to_s + ': ' + note, code, docs[0]) elsif error code = docs[0]['code'] || Mongo::ErrorCode::UNKNOWN_ERROR error = "wtimeout" if error == "timeout" raise WriteConcernError.new(code.to_s + ': ' + error, code, docs[0]) if error == "wtimeout" raise OperationFailure.new(code.to_s + ': ' + error, code, docs[0]) end end docs[0] end
Generated with the Darkfish Rdoc Generator 2.