From f1b94fd0edf322a26b9812980e887326653ba872 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 2 Jul 2013 15:47:05 +0200 Subject: [PATCH] Remove middleman context object --- lib/moped/collection.rb | 9 +++- lib/moped/cursor.rb | 4 +- lib/moped/database.rb | 4 +- lib/moped/query.rb | 83 ++++++++++++++++++++++++------------ lib/moped/readable.rb | 18 -------- lib/moped/session.rb | 12 +++--- lib/moped/session/context.rb | 62 --------------------------- 7 files changed, 74 insertions(+), 118 deletions(-) delete mode 100644 lib/moped/session/context.rb diff --git a/lib/moped/collection.rb b/lib/moped/collection.rb index 22edd6b..d68f8da 100644 --- a/lib/moped/collection.rb +++ b/lib/moped/collection.rb @@ -7,6 +7,7 @@ module Moped # # @since 1.0.0 class Collection + include Readable # @!attribute database # @return [ Database ] The database for the collection. @@ -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. @@ -154,5 +157,9 @@ def aggregate(*pipeline) def session database.session end + + def write_concern + session.write_concern + end end end diff --git a/lib/moped/cursor.rb b/lib/moped/cursor.rb index 9ef9833..3a3b913 100644 --- a/lib/moped/cursor.rb +++ b/lib/moped/cursor.rb @@ -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. @@ -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? diff --git a/lib/moped/database.rb b/lib/moped/database.rb index f5d6d1c..62f5956 100644 --- a/lib/moped/database.rb +++ b/lib/moped/database.rb @@ -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. diff --git a/lib/moped/query.rb b/lib/moped/query.rb index cc27ea1..3169428 100644 --- a/lib/moped/query.rb +++ b/lib/moped/query.rb @@ -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 @@ -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. @@ -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. @@ -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. @@ -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) diff --git a/lib/moped/readable.rb b/lib/moped/readable.rb index 73f898a..e11e31c 100644 --- a/lib/moped/readable.rb +++ b/lib/moped/readable.rb @@ -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 diff --git a/lib/moped/session.rb b/lib/moped/session.rb index f871b55..580545d 100644 --- a/lib/moped/session.rb +++ b/lib/moped/session.rb @@ -5,7 +5,6 @@ require "moped/collection" require "moped/cluster" require "moped/database" -require "moped/session/context" module Moped @@ -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. # @@ -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. @@ -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) diff --git a/lib/moped/session/context.rb b/lib/moped/session/context.rb deleted file mode 100644 index 0cfa809..0000000 --- a/lib/moped/session/context.rb +++ /dev/null @@ -1,62 +0,0 @@ -module Moped - class Session - - # @api private - class Context - - attr_reader :session - - def initialize(session) - @session = session - end - - def read_preference - session.read_preference - end - - def write_concern - session.write_concern - end - - def cluster - session.cluster - end - - def query(database, collection, selector, options = {}) - read_preference.with_node(cluster) do |node| - node.query(database, collection, selector, query_options(options)) - end - end - - def command(database, command) - read_preference.with_node(cluster) do |node| - node.command(database, command, query_options({})) - end - end - - def insert(database, collection, documents, options = {}) - cluster.with_primary do |node| - node.insert(database, collection, documents, write_concern, options) - end - end - - def update(database, collection, selector, change, options = {}) - cluster.with_primary do |node| - node.update(database, collection, selector, change, write_concern, options) - end - end - - def remove(database, collection, selector, options = {}) - cluster.with_primary do |node| - node.remove(database, collection, selector, write_concern, options) - end - end - - private - - def query_options(options) - read_preference.query_options(options) - end - end - end -end