Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Remove middleman context object
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Jul 2, 2013
1 parent 8baf352 commit f1b94fd
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 118 deletions.
9 changes: 8 additions & 1 deletion lib/moped/collection.rb
Expand Up @@ -7,6 +7,7 @@ module Moped
#
# @since 1.0.0
class Collection
include Readable

# @!attribute database
# @return [ Database ] The database for the collection.
Expand Down Expand Up @@ -120,7 +121,9 @@ def initialize(database, name)
# @since 1.0.0
def insert(documents, flags = nil)
docs = documents.is_a?(Array) ? documents : [ documents ]
session.context.insert(database.name, name, docs, flags: flags || [])
cluster.with_primary do |node|
node.insert(database.name, name, docs, write_concern, flags: flags || [])
end
end

# Call aggregate function over the collection.
Expand Down Expand Up @@ -154,5 +157,9 @@ def aggregate(*pipeline)
def session
database.session
end

def write_concern
session.write_concern
end
end
end
4 changes: 2 additions & 2 deletions lib/moped/cursor.rb
Expand Up @@ -4,6 +4,7 @@ module Moped
#
# @api private
class Cursor
include Readable

# @attribute [r] get_more_op The get more message.
# @attribute [r] kill_cursor_op The kill cursor message.
Expand Down Expand Up @@ -130,10 +131,9 @@ def load_docs
@options[:flags] |= [:no_cursor_timeout] if @options[:no_timeout]
options = @options.clone
options[:limit] = request_limit
read_preference = session.context.read_preference

reply, @node = read_preference.with_node(session.cluster) do |node|
[ node.query(@database, @collection, @selector, read_preference.query_options(options)), node ]
[ node.query(@database, @collection, @selector, query_options(options)), node ]
end

@limit -= reply.count if limited?
Expand Down
4 changes: 3 additions & 1 deletion lib/moped/database.rb
Expand Up @@ -69,7 +69,9 @@ def collection_names
#
# @since 1.0.0
def command(command)
read(Protocol::Command.new(name, command, query_options))
read_preference.with_node(cluster) do |node|
node.command(name, command, query_options({}))
end
end

# Drop the database.
Expand Down
83 changes: 56 additions & 27 deletions lib/moped/query.rb
Expand Up @@ -115,15 +115,19 @@ def explain
#
# @since 1.0.0
def first
reply = session.context.query(
operation.database,
operation.collection,
operation.selector,
fields: operation.fields,
flags: operation.flags,
skip: operation.skip,
limit: -1
)
reply = read_preference.with_node(cluster) do |node|
node.query(
operation.database,
operation.collection,
operation.selector,
query_options(
fields: operation.fields,
flags: operation.flags,
skip: operation.skip,
limit: -1
)
)
end
reply.documents.first
end
alias :one :first
Expand Down Expand Up @@ -272,12 +276,15 @@ def modify(change, options = {})
#
# @since 1.0.0
def remove
session.context.remove(
operation.database,
operation.collection,
operation.basic_selector,
flags: [ :remove_first ]
)
cluster.with_primary do |node|
node.remove(
operation.database,
operation.collection,
operation.basic_selector,
write_concern,
flags: [ :remove_first ]
)
end
end

# Remove multiple documents matching the query's selector.
Expand All @@ -289,11 +296,14 @@ def remove
#
# @since 1.0.0
def remove_all
session.context.remove(
operation.database,
operation.collection,
operation.basic_selector
)
cluster.with_primary do |node|
node.remove(
operation.database,
operation.collection,
operation.basic_selector,
write_concern
)
end
end

# Set the fields to include or exclude from the query.
Expand Down Expand Up @@ -368,13 +378,16 @@ def tailable
#
# @since 1.0.0
def update(change, flags = nil)
session.context.update(
operation.database,
operation.collection,
operation.selector["$query"] || operation.selector,
change,
flags: flags
)
cluster.with_primary do |node|
node.update(
operation.database,
operation.collection,
operation.selector["$query"] || operation.selector,
change,
write_concern,
flags: flags
)
end
end

# Update multiple documents matching the query's selector.
Expand Down Expand Up @@ -409,6 +422,22 @@ def upsert(change)
update(change, [ :upsert ])
end

def write_concern
session.write_concern
end

def read_preference
session.read_preference
end

def cluster
session.cluster
end

def query_options(options)
read_preference.query_options(options)
end

private

def initialize_copy(other)
Expand Down
18 changes: 0 additions & 18 deletions lib/moped/readable.rb
Expand Up @@ -22,24 +22,6 @@ def cluster
session.cluster
end

# Execute a read operation on the correct node.
#
# @api private
#
# @example Execute a read.
# database.read(operation)
#
# @param [ Protocol::Command ] operation The read operation.
#
# @return [ Object ] The result of the operation.
#
# @since 2.0.0
def read(operation)
read_preference.with_node(cluster) do |node|
Operation::Read.new(operation).execute(node)
end
end

# Convenience method for getting the read preference from the session.
#
# @api private
Expand Down
12 changes: 5 additions & 7 deletions lib/moped/session.rb
Expand Up @@ -5,7 +5,6 @@
require "moped/collection"
require "moped/cluster"
require "moped/database"
require "moped/session/context"

module Moped

Expand Down Expand Up @@ -33,10 +32,11 @@ module Moped
# @since 1.0.0
class Session

# @attribute [r] cluster The session cluster.
# @attribute [r] context The session context.
# @attribute [r] options The session options.
attr_reader :cluster, :context, :options
# @!attribute cluster
# @return [ Cluster ] The cluster of nodes.
# @!attribute options
# @return [ Hash ] The configuration options.
attr_reader :cluster, :options

# Return +collection+ from the current database.
#
Expand Down Expand Up @@ -196,7 +196,6 @@ def logout
def initialize(seeds, options = {})
@options = options
@cluster = Cluster.new(seeds, options)
@context = Context.new(self)
end

# Create a new session with +options+ and use new socket connections.
Expand Down Expand Up @@ -372,7 +371,6 @@ def initialize_copy(_)
@read_preference = nil
@write_concern = nil
@current_database = nil
@context = Context.new(self)
end

def set_current_database(database)
Expand Down
62 changes: 0 additions & 62 deletions lib/moped/session/context.rb

This file was deleted.

0 comments on commit f1b94fd

Please sign in to comment.