Browse files

Put counter back in until token finalized

This reverts commit 2e27bc0.
  • Loading branch information...
1 parent 7c19cd3 commit 542738c5a00e351c1f4fa274efea845bdea5efb0 @durran durran committed Feb 8, 2010
Showing with 47 additions and 53 deletions.
  1. +18 −23 lib/mongoid/collection.rb
  2. +29 −30 spec/unit/mongoid/collection_spec.rb
View
41 lib/mongoid/collection.rb
@@ -8,7 +8,7 @@
module Mongoid #:nodoc
class Collection
include Collections::Mimic
- attr_reader :name
+ attr_reader :counter, :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, :find_one]))
+ proxy(:directed, (Collections::Operations::READ - [:find]))
# 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,8 +38,14 @@ class Collection
# Return:
#
# Either a +Master+ or +Slaves+ collection.
- def directed(token = nil)
- (token || slaves.empty?) ? master : slaves
+ def directed
+ if under_max_counter? || slaves.empty?
+ @counter = @counter + 1
+ master
+ else
+ @counter = 0
+ slaves
+ end
end
# Find documents from the database given a selector and options.
@@ -48,43 +54,27 @@ def directed(token = nil)
#
# 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 = {}, token = nil)
- cursor = Mongoid::Cursor.new(self, directed(token).find(selector, options))
+ def find(selector = {}, options = {})
+ cursor = Mongoid::Cursor.new(self, directed.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 = name
+ @name, @counter = name, 0
end
# Return the object responsible for reading documents from the database.
@@ -107,5 +97,10 @@ 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
View
59 spec/unit/mongoid/collection_spec.rb
@@ -42,16 +42,43 @@
describe "#directed" do
- context "when a write token exists" do
+ context "when the counter is less than the maximum" do
+
+ before do
+ collection.instance_variable_set(:@counter, 0)
+ end
it "delegates to the master" do
- collection.directed("token").should == master
+ 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
end
end
context "when the slave does not exist" do
before do
+ collection.instance_variable_set(:@counter, 10)
slaves.expects(:empty?).returns(true)
end
@@ -67,7 +94,6 @@
@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
@@ -84,31 +110,4 @@
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 542738c

Please sign in to comment.