Skip to content

Commit

Permalink
minor style and doc fixes. warning about logging performance issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Jan 31, 2011
1 parent 8a72965 commit 9c83ca6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/mongo/collection.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions lib/mongo/connection.rb
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(', ') + ")"
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/cursor.rb
Expand Up @@ -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
Expand Down

0 comments on commit 9c83ca6

Please sign in to comment.