Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mongo/auth/user/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def generate(user, options)
end

def execute_operation(options)
client.send(:with_session, options) do |session|
client.with_session(options) do |session|
op = yield session
op.execute(next_primary(nil, session), context: Operation::Context.new(client: client, session: session))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/bulk_write/result_combiner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def result

def combine_counts!(result)
Result::FIELDS.each do |field|
if result.respond_to?(field) && (value = result.send(field))
if result.respond_to?(field) && (value = result.public_send(field))
results.merge!(field => (results[field] || 0) + value)
end
end
Expand Down
30 changes: 27 additions & 3 deletions lib/mongo/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ def monitoring
@monitoring
end
end
private :monitoring

# Determine if this client is equivalent to another object.
#
Expand Down Expand Up @@ -796,10 +795,10 @@ def use(name)
def with(new_options = nil)
clone.tap do |client|
opts = client.update_options(new_options || Options::Redacted.new)
Database.create(client)
client.reset_database!
# We can't use the same cluster if some options that would affect it
# have changed.
Cluster.create(client, monitoring: opts[:monitoring]) if cluster_modifying?(opts)
client.reset_cluster!(monitoring: opts[:monitoring]) if cluster_modifying?(opts)
end
end

Expand Down Expand Up @@ -855,6 +854,31 @@ def update_options(new_options)
end
end

# Replaces this client's database with a fresh instance built from the
# client's current options. Used by #with so a reconfigured client does
# not share its database with the client it was cloned from.
#
# @api private
def reset_database!
@database = Database.new(self, options[:database], options)
end

# Replaces this client's cluster with a fresh instance built from the
# client's current options. Used by #with so a reconfigured client does
# not share its cluster with the client it was cloned from.
#
# @param [ Monitoring | nil ] monitoring The monitoring instance to use
# with the new cluster. If nil, a new instance of Monitoring is created.
#
# @api private
def reset_cluster!(monitoring: nil)
@cluster = Cluster.new(
cluster.addresses.map(&:to_s),
monitoring || Monitoring.new,
cluster_options
)
end

# Get the read concern for this client.
#
# @example Get the client read concern.
Expand Down
24 changes: 0 additions & 24 deletions lib/mongo/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,30 +269,6 @@ def initialize(seeds, monitoring, options = Options::Redacted.new)
start_stop_srv_monitor
end

# Create a cluster for the provided client, for use when we don't want the
# client's original cluster instance to be the same.
#
# @example Create a cluster for the client.
# Cluster.create(client)
#
# @param [ Client ] client The client to create on.
# @param [ Monitoring | nil ] monitoring. The monitoring instance to use
# with the new cluster. If nil, a new instance of Monitoring will be
# created.
#
# @return [ Cluster ] The cluster.
#
# @since 2.0.0
# @api private
def self.create(client, monitoring: nil)
cluster = Cluster.new(
client.cluster.addresses.map(&:to_s),
monitoring || Monitoring.new,
client.cluster_options
)
client.instance_variable_set(:@cluster, cluster)
end

# @return [ Hash ] The options hash.
attr_reader :options

Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/cluster/sdam_flow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def update_rs_without_primary
def add_servers_from_desc(updated_desc)
added_servers = []
%w[hosts passives arbiters].each do |m|
updated_desc.send(m).each do |address_str|
updated_desc.public_send(m).each do |address_str|
if (server = cluster.add(address_str, monitor: false))
added_servers << server
end
Expand All @@ -402,7 +402,7 @@ def add_servers_from_desc(updated_desc)
# good primary).
def remove_servers_not_in_desc(updated_desc)
updated_desc_address_strs = %w[hosts passives arbiters].map do |m|
updated_desc.send(m)
updated_desc.public_send(m)
end.flatten
servers_list.each do |server|
next if updated_desc_address_strs.include?(address_str = server.address.to_s)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def create(opts = {})
operation = { create: name }.merge(options)
operation.delete(:write)
operation.delete(:write_concern)
client.send(:with_session, opts) do |session|
client.with_session(opts) do |session|
write_concern = if opts[:write_concern]
WriteConcern.get(opts[:write_concern])
else
Expand Down
12 changes: 8 additions & 4 deletions lib/mongo/collection/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ def operation_timeouts(opts = {})
end
end

# Executes the provided block within the context of a session, using
# this view's options merged with the given ones.
#
# @api private
def with_session(opts = {}, &block)
client.with_session(@options.merge(opts), &block)
end

private

def initialize_copy(other)
Expand All @@ -252,10 +260,6 @@ def new(options)
def view
self
end

def with_session(opts = {}, &block)
client.with_session(@options.merge(opts), &block)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/mongo/collection/view/aggregation/behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def perform_setup(view, options, forbid: [])
end

def server_selector
@view.send(:server_selector)
@view.server_selector
end

def aggregate_spec(session, read_preference = nil)
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/collection/view/map_reduce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def verbose(value = nil)
#
# @since 2.5.0
def execute
view.send(:with_session, @options) do |session|
view.with_session(@options) do |session|
write_concern = view.write_concern_with_session(session)
context = Operation::Context.new(client: client, session: session)
nro_write_with_retry(write_concern, context: context) do |connection, _txn_num, context|
Expand All @@ -241,7 +241,7 @@ def execute
OUT_ACTIONS = %i[replace merge reduce].freeze

def server_selector
@view.send(:server_selector)
@view.server_selector
end

def inline?
Expand Down
18 changes: 11 additions & 7 deletions lib/mongo/collection/view/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def timeout_ms(timeout_ms = nil)
# @api private
def read_concern
if options[:session] && options[:session].in_transaction?
options[:session].send(:txn_read_concern) || collection.client.read_concern
options[:session].txn_read_concern || collection.client.read_concern
else
collection.read_concern
end
Expand Down Expand Up @@ -749,12 +749,10 @@ def parallel_scan(cursor_count, options = {})
end
end

private

def collation(doc = nil)
configure(:collation, doc)
end

# The server selector for this view, derived from its read
# preference (or the collection/client default).
#
# @api private
def server_selector
@server_selector ||= if options[:session] && options[:session].in_transaction?
ServerSelector.get(read_preference || client.server_selector)
Expand All @@ -763,6 +761,12 @@ def server_selector
end
end

private

def collation(doc = nil)
configure(:collation, doc)
end

def validate_doc!(doc)
raise Error::InvalidDocument.new unless doc.respond_to?(:keys)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/crypt/auto_encrypter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def internal_client(client)
@internal_client ||= client.with(
auto_encryption_options: nil,
min_pool_size: 0,
monitoring: client.send(:monitoring)
monitoring: client.monitoring
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/crypt/handle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def set_bypass_query_analysis
# Send the logs from libmongocrypt to the Mongo::Logger
def set_logger_callback
@log_callback = proc do |level, msg|
@logger.send(level, msg)
@logger.public_send(level, msg)
end

Binding.setopt_log_handler(@mongocrypt, @log_callback)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def use_limit?
end

def limit
@view.send(:limit)
@view.limit
end

def register
Expand Down
18 changes: 0 additions & 18 deletions lib/mongo/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -644,24 +644,6 @@ def watch(pipeline = [], options = {})
)
end

# Create a database for the provided client, for use when we don't want the
# client's original database instance to be the same.
#
# @api private
#
# @example Create a database for the client.
# Database.create(client)
#
# @param [ Client ] client The client to create on.
#
# @return [ Database ] The database.
#
# @since 2.0.0
def self.create(client)
database = Database.new(client, client.options[:database], client.options)
client.instance_variable_set(:@database, database)
end

# @return [ Integer | nil ] Operation timeout that is for this database or
# for the corresponding client.
#
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/database/cursor_command_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def operation_timeouts(opts = {})
database.operation_timeouts(opts)
end

private

# Cursors do not support a limit when built from a command response.
#
# @api private
def limit
nil
end
Expand Down
46 changes: 26 additions & 20 deletions lib/mongo/grid/fs_bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,32 @@ def drop(opts = {})
chunks_collection.drop(timeout_ms: context.remaining_timeout_ms)
end

# Ensures the files and chunks collections have their required indexes,
# creating any that are missing.
#
# @param [ CsotTimeoutHolder | nil ] timeout_holder The timeout holder.
#
# @api private
def ensure_indexes!(timeout_holder = nil)
fc_idx = files_collection.find(
{},
limit: 1,
projection: { _id: 1 },
timeout_ms: timeout_holder&.remaining_timeout_ms
).first
create_index_if_missing!(files_collection, FSBucket::FILES_INDEX) if fc_idx.nil?

cc_idx = chunks_collection.find(
{},
limit: 1,
projection: { _id: 1 },
timeout_ms: timeout_holder&.remaining_timeout_ms
).first
return unless cc_idx.nil?

create_index_if_missing!(chunks_collection, FSBucket::CHUNKS_INDEX, unique: true)
end

private

# @param [ Hash ] opts The options.
Expand All @@ -516,26 +542,6 @@ def files_name
"#{prefix}.#{Grid::File::Info::COLLECTION}"
end

def ensure_indexes!(timeout_holder = nil)
fc_idx = files_collection.find(
{},
limit: 1,
projection: { _id: 1 },
timeout_ms: timeout_holder&.remaining_timeout_ms
).first
create_index_if_missing!(files_collection, FSBucket::FILES_INDEX) if fc_idx.nil?

cc_idx = chunks_collection.find(
{},
limit: 1,
projection: { _id: 1 },
timeout_ms: timeout_holder&.remaining_timeout_ms
).first
return unless cc_idx.nil?

create_index_if_missing!(chunks_collection, FSBucket::CHUNKS_INDEX, unique: true)
end

def create_index_if_missing!(collection, index_spec, options = {})
indexes_view = collection.indexes
begin
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/grid/stream/write.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def file_info
end

def ensure_indexes!
fs.send(:ensure_indexes!, @timeout_holder)
fs.ensure_indexes!(@timeout_holder)
end

def ensure_open!
Expand Down
14 changes: 9 additions & 5 deletions lib/mongo/index/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,20 @@ def operation_timeouts(opts = {})
end
end

# The query limit for an index view. Always -1; present so a Cursor
# can treat an index view like a collection view.
#
# @api private
def limit
-1
end

private

def drop_by_name(name, opts = {})
session_options = @options.dup
session_options[:session] = opts[:session] if opts[:session]
client.send(:with_session, session_options) do |session|
client.with_session(session_options) do |session|
spec = {
db_name: database.name,
coll_name: collection.name,
Expand Down Expand Up @@ -396,10 +404,6 @@ def initial_query_op(session)
Operation::Indexes.new(indexes_spec(session))
end

def limit
-1
end

def normalize_keys(spec)
return false if spec.is_a?(String)

Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/operation/shared/sessions_supported.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def apply_causal_consistency!(selector, connection)
def apply_causal_consistency_if_possible(selector, connection)
return if connection.description.standalone?

cc_doc = session.send(:causal_consistency_doc)
cc_doc = session.causal_consistency_doc
return unless cc_doc

rc_doc = (selector[:readConcern] || read_concern || {}).merge(cc_doc)
Expand Down
Loading
Loading