Permalink
Browse files

Removing counter from collection

  • Loading branch information...
1 parent 1df1368 commit 2e27bc0d9222390774135769b651ba232e1e333a @durran durran committed Feb 8, 2010
Showing with 53 additions and 47 deletions.
  1. +23 −18 lib/mongoid/collection.rb
  2. +30 −29 spec/unit/mongoid/collection_spec.rb
View
@@ -8,7 +8,7 @@
module Mongoid #:nodoc
class Collection
include Collections::Mimic
- attr_reader :counter, :name
+ attr_reader :name
# All write operations should delegate to the master connection. These
# operations mimic the methods on a Mongo:Collection.
@@ -25,7 +25,7 @@ class Collection
# Example:
#
# <tt>collection.find({ :name => "Al" })</tt>
- proxy(:directed, (Collections::Operations::READ - [:find]))
+ proxy(:directed, (Collections::Operations::READ - [:find, :find_one]))
# Determines where to send the next read query. If the slaves are not
# defined then send to master. If the read counter is under the configured
@@ -38,14 +38,8 @@ class Collection
# Return:
#
# Either a +Master+ or +Slaves+ collection.
- def directed
- if under_max_counter? || slaves.empty?
- @counter = @counter + 1
- master
- else
- @counter = 0
- slaves
- end
+ def directed(token = nil)
+ (token || slaves.empty?) ? master : slaves
end
# Find documents from the database given a selector and options.
@@ -54,27 +48,43 @@ def directed
#
# selector: A +Hash+ selector that is the query.
# options: The options to pass to the db.
+ # token: String from a previous write.
#
# Example:
#
# <tt>collection.find({ :test => "value" })</tt>
- def find(selector = {}, options = {})
- cursor = Mongoid::Cursor.new(self, directed.find(selector, options))
+ def find(selector = {}, options = {}, token = nil)
+ cursor = Mongoid::Cursor.new(self, directed(token).find(selector, options))
if block_given?
yield cursor; cursor.close
else
cursor
end
end
+ # Find the first from the database given a selector and options.
+ #
+ # Options:
+ #
+ # selector: A +Hash+ selector that is the query.
+ # options: The options to pass to the db.
+ # toekn: String from a previous write.
+ #
+ # Example:
+ #
+ # <tt>collection.find({ :test => "value" })</tt>
+ def find_one(selector = nil, options = {}, token = nil)
+ directed(token).find_one(selector, options)
+ end
+
# Initialize a new Mongoid::Collection, setting up the master, slave, and
# name attributes. Masters will be used for writes, slaves for reads.
#
# Example:
#
# <tt>Mongoid::Collection.new(masters, slaves, "test")</tt>
def initialize(name)
- @name, @counter = name, 0
+ @name = name
end
# Return the object responsible for reading documents from the database.
@@ -97,10 +107,5 @@ def slaves
def master
@master ||= Collections::Master.new(Mongoid.master, @name)
end
-
- protected
- def under_max_counter?
- @counter < Mongoid.max_successive_reads
- end
end
end
@@ -42,43 +42,16 @@
describe "#directed" do
- context "when the counter is less than the maximum" do
-
- before do
- collection.instance_variable_set(:@counter, 0)
- end
+ context "when a write token exists" do
it "delegates to the master" do
- collection.directed.should == master
- end
-
- it "increments the counter" do
- collection.directed
- collection.counter.should == 1
- end
- end
-
- context "when the counter is at the max" do
-
- before do
- slaves.expects(:empty?).returns(false)
- collection.instance_variable_set(:@counter, 10)
- end
-
- it "delegates to the slave" do
- collection.directed.should == slaves
- end
-
- it "resets the counter" do
- collection.directed
- collection.counter.should == 0
+ collection.directed("token").should == master
end
end
context "when the slave does not exist" do
before do
- collection.instance_variable_set(:@counter, 10)
slaves.expects(:empty?).returns(true)
end
@@ -94,6 +67,7 @@
@cursor = stub.quacks_like(Mongoid::Cursor.allocate)
master.expects(:find).with({ :test => "value" }, {}).returns(@mongo_cursor)
Mongoid::Cursor.expects(:new).with(collection, @mongo_cursor).returns(@cursor)
+ slaves.expects(:empty?).returns(true)
end
it "finds are returns a cursor" do
@@ -110,4 +84,31 @@
end
end
end
+
+ describe "#find_one" do
+
+ context "when a token exists" do
+
+ before do
+ master.expects(:find_one).with({ :test => "value" }, {})
+ slaves.stubs(:empty?).returns(false)
+ end
+
+ it "delegates to master" do
+ collection.find_one({ :test => "value"}, {}, "token")
+ end
+ end
+
+ context "when a token does not exist" do
+
+ before do
+ slaves.expects(:find_one).with({ :test => "value" }, {})
+ slaves.stubs(:empty?).returns(false)
+ end
+
+ it "delegates to slave" do
+ collection.find_one({ :test => "value"})
+ end
+ end
+ end
end

0 comments on commit 2e27bc0

Please sign in to comment.