From 9c83ca6b3eca9478c7731a20e74b68e7bc03b0b8 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Mon, 31 Jan 2011 15:51:39 -0500 Subject: [PATCH] minor style and doc fixes. warning about logging performance issues. --- lib/mongo/collection.rb | 6 +++--- lib/mongo/connection.rb | 18 +++++++++++++----- lib/mongo/cursor.rb | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index be6718f3c4..0a1b69b1af 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -321,7 +321,7 @@ def remove(selector={}, opts={}) message.put_int(0) message.put_binary(BSON::BSON_CODER.serialize(selector, false, true).to_s) - @connection.instrument( :remove, :database => @db.name, :collection => @name, :selector => selector ) do + @connection.instrument(:remove, :database => @db.name, :collection => @name, :selector => selector) do if safe @connection.send_message_with_safe_check(Mongo::Constants::OP_DELETE, message, @db.name, nil, safe) else @@ -368,7 +368,7 @@ def update(selector, document, opts={}) message.put_binary(BSON::BSON_CODER.serialize(selector, false, true).to_s) message.put_binary(BSON::BSON_CODER.serialize(document, false, true).to_s) - @connection.instrument( :update, :database => @db.name, :collection => @name, :selector => selector, :document => document ) do + @connection.instrument(:update, :database => @db.name, :collection => @name, :selector => selector, :document => document) do if safe @connection.send_message_with_safe_check(Mongo::Constants::OP_UPDATE, message, @db.name, nil, safe) else @@ -840,7 +840,7 @@ def insert_documents(documents, collection_name=@name, check_keys=true, safe=fal end raise InvalidOperation, "Exceded maximum insert size of 16,000,000 bytes" if message.size > 16_000_000 - @connection.instrument( :insert, :database => @db.name, :collection => collection_name, :documents => documents ) do + @connection.instrument(:insert, :database => @db.name, :collection => collection_name, :documents => documents) do if safe @connection.send_message_with_safe_check(Mongo::Constants::OP_INSERT, message, @db.name, nil, safe) else diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 489145ab10..cfac20eb61 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -61,7 +61,8 @@ class Connection # on initialization. # @option opts [Boolean] :slave_ok (false) Must be set to +true+ when connecting # to a single, slave node. - # @option opts [Logger, #debug] :logger (nil) Logger instance to receive driver operation log. + # @option opts [Logger, #debug] :logger (nil) A Logger instance for debugging driver ops. Note that + # logging negatively impacts performance; therefore, it should not be used for high-performance apps. # @option opts [Integer] :pool_size (1) The maximum number of socket connections allowed per # connection pool. Note: this setting is relevant only for multi-threaded applications. # @option opts [Float] :timeout (5.0) When all of the connections a pool are checked out, @@ -534,8 +535,10 @@ def checkin_writer(socket) end end - # execute the block and log the operation as described by name/payload - def instrument( name, payload = {}, &blk ) + # Execute the block and log the operation described by name + # and payload. + # TODO: Not sure if this should take a block. + def instrument(name, payload = {}, &blk) res = yield log_operation(name, payload) res @@ -572,7 +575,12 @@ def setup(opts) @primary = nil @primary_pool = nil - @logger = opts[:logger] || nil + @logger = opts[:logger] || nil + + if @logger + @logger.debug("MongoDB logging. Please note that logging negatively impacts performance " + + "and should be disabled for high-performance production apps.") + end should_connect = opts.fetch(:connect, true) connect if should_connect @@ -596,7 +604,7 @@ def format_pair(host, port) ## Logging methods - def log_operation( name, payload ) + def log_operation(name, payload) return unless @logger msg = "#{payload[:database]}['#{payload[:collection]}'].#{name}(" msg += payload.values_at(:selector, :document, :documents, :fields ).compact.map(&:inspect).join(', ') + ")" diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 71a8ea321a..b49bc478de 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -378,7 +378,7 @@ def send_initial_query false else message = construct_query_message - @connection.instrument( :find, instrument_payload ) do + @connection.instrument(:find, instrument_payload) do results, @n_received, @cursor_id = @connection.receive_message( Mongo::Constants::OP_QUERY, message, nil, @socket, @command) @returned += @n_received