forked from mongodb/mongoid
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of master and slaves read/write split
- Loading branch information
Showing
14 changed files
with
566 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# encoding: utf-8 | ||
require "mongoid/collections/cyclic_iterator" | ||
require "mongoid/collections/reader" | ||
require "mongoid/collections/writer" | ||
|
||
module Mongoid #:nodoc | ||
class Collection | ||
|
||
# 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) | ||
# Get all the master db -> Mongoid.master | ||
# Get all the slave dbs -> Mongoid.slaves | ||
@name = name | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# encoding: utf-8 | ||
module Mongoid #:nodoc: | ||
module Collections #:nodoc: | ||
class CyclicIterator | ||
|
||
attr_reader :counter | ||
|
||
# Performs iteration over an array, if the array gets to the end then loop | ||
# back to the first. | ||
# | ||
# Example: | ||
# | ||
# <tt>CyclicIterator.new([ first, second ])</tt> | ||
def initialize(array) | ||
@array, @counter = array, -1 | ||
end | ||
|
||
# Get the next element in the array. If the element is the last in the | ||
# array then return the first. | ||
# | ||
# Example: | ||
# | ||
# <tt>iterator.next</tt> | ||
# | ||
# Returns: | ||
# | ||
# The next element in the array. | ||
def next | ||
(@counter == @array.size - 1) ? @counter = 0 : @counter = @counter + 1 | ||
@array[@counter] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# encoding: utf-8 | ||
module Mongoid #:nodoc: | ||
module Collections #:nodoc: | ||
class Reader | ||
|
||
attr_reader :iterator | ||
|
||
delegate \ | ||
:count, | ||
:distinct, | ||
:find, | ||
:find_one, | ||
:group, | ||
:index_information, | ||
:map_reduce, | ||
:mapreduce, | ||
:options, | ||
:size, :to => :collection | ||
|
||
# Create the new database reader. Will create a collection from the | ||
# slave databases and cycle through them on each read. | ||
# | ||
# Example: | ||
# | ||
# <tt>Reader.new(slaves, "mongoid_people")</tt> | ||
def initialize(slaves, name) | ||
@iterator = CyclicIterator.new( | ||
slaves.collect { |db| db.collection(name) } | ||
) | ||
end | ||
|
||
protected | ||
def collection | ||
@iterator.next | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# encoding: utf-8 | ||
module Mongoid #:nodoc: | ||
module Collections #:nodoc: | ||
class Writer | ||
attr_reader :collection | ||
|
||
delegate \ | ||
:<<, | ||
:create_index, | ||
:drop, | ||
:drop_index, | ||
:drop_indexes, | ||
:insert, | ||
:remove, | ||
:rename, | ||
:save, | ||
:update, :to => :collection | ||
|
||
# Create the new database writer. Will create a collection from the | ||
# master database. | ||
# | ||
# Example: | ||
# | ||
# <tt>Writer.new(master, "mongoid_people")</tt> | ||
def initialize(master_db, name) | ||
@collection = master_db.collection(name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "spec_helper" | ||
|
||
describe Mongoid::Collection do | ||
|
||
let(:master) do | ||
stub.quacks_like(Mongo::DB.allocate) | ||
end | ||
|
||
let(:slave_one) do | ||
stub.quacks_like(Mongo::DB.allocate) | ||
end | ||
|
||
let(:slave_two) do | ||
stub.quacks_like(Mongo::DB.allocate) | ||
end | ||
|
||
let(:slaves) do | ||
[ slave_one, slave_two ] | ||
end | ||
|
||
describe "#initialize" do | ||
|
||
let(:collection) do | ||
Mongoid::Collection.new(master, slaves, "mongoid_test") | ||
end | ||
|
||
it "sets the master db" | ||
|
||
it "sets the slave dbs" | ||
|
||
it "sets the name" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require "spec_helper" | ||
|
||
describe Mongoid::Collections::CyclicIterator do | ||
|
||
before do | ||
@first = stub | ||
@second = stub | ||
@third = stub | ||
@fourth = stub | ||
end | ||
|
||
let(:array) do | ||
[ @first, @second, @third, @fourth ] | ||
end | ||
|
||
describe "#initialize" do | ||
|
||
let(:iterator) do | ||
Mongoid::Collections::CyclicIterator.new(array) | ||
end | ||
|
||
it "defaults the counter to -1" do | ||
iterator.counter.should == -1 | ||
end | ||
end | ||
|
||
describe "#next" do | ||
|
||
context "when the iterator has just been created" do | ||
|
||
let(:iterator) do | ||
Mongoid::Collections::CyclicIterator.new(array) | ||
end | ||
|
||
it "returns the first element" do | ||
iterator.next.should == @first | ||
end | ||
end | ||
|
||
context "when the iterator is in the middle" do | ||
|
||
let(:iterator) do | ||
Mongoid::Collections::CyclicIterator.new(array) | ||
end | ||
|
||
before do | ||
2.times { iterator.next } | ||
end | ||
|
||
it "returns the next element given the index" do | ||
iterator.next.should == @third | ||
end | ||
end | ||
|
||
context "when the iterator is on the last element" do | ||
|
||
let(:iterator) do | ||
Mongoid::Collections::CyclicIterator.new(array) | ||
end | ||
|
||
before do | ||
4.times { iterator.next } | ||
end | ||
|
||
it "returns the first element" do | ||
iterator.next.should == @first | ||
end | ||
|
||
it "resets the counter" do | ||
iterator.next | ||
iterator.counter.should == 0 | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.