Skip to content

Commit

Permalink
Cleanup whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sobrinho committed Aug 11, 2011
1 parent 7e585ec commit 8558c52
Show file tree
Hide file tree
Showing 46 changed files with 279 additions and 279 deletions.
30 changes: 15 additions & 15 deletions README.mkdn
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ Octopus supports:
When using replication, all writes queries will be sent to master, and read queries to slaves. More info could be found at: <a href="http://wiki.github.com/tchandy/octopus/replication"> Wiki</a>

### Sharding
When using sharding, you need to specify which shard to send the query. Octopus supports selecting the shard inside a controller, or manually in each object. More could be found at <a href="http://wiki.github.com/tchandy/octopus/sharding"> Wiki</a>
When using sharding, you need to specify which shard to send the query. Octopus supports selecting the shard inside a controller, or manually in each object. More could be found at <a href="http://wiki.github.com/tchandy/octopus/sharding"> Wiki</a>

### Replication + Sharding
Replication + Sharding isn't supported yet. This is on our TODO list and will be done ASAP. If you need, feel free to fork and implement it.

## Install
## Install

### Rails 2.x
### Rails 2.x

Install the octopus gem:

Expand All @@ -33,7 +33,7 @@ Add this line to enviroment.rb:

config.gem 'ar-octopus', :lib => "octopus"

### Rails 3.x
### Rails 3.x

Add this line to Gemfile:

Expand All @@ -43,19 +43,19 @@ Runs a bundle install:

bundle install

## How to use Octopus?
## How to use Octopus?

First, you need to create a config file, shards.yml, inside your config/ directory. to see the syntax and how this file should look, please checkout <a href="http://wiki.github.com/tchandy/octopus/config-file">this page on wiki</a>.

### Syntax
### Syntax

Octopus adds a method to each AR Class and object: the using method is used to select the shard like this:
Octopus adds a method to each AR Class and object: the using method is used to select the shard like this:

User.where(:name => "Thiago").limit(3).using(:slave_one)

Octopus also supports queries within a block. When you pass a block to the using method, all queries inside the block will be sent to the specified shard.
Octopus also supports queries within a block. When you pass a block to the using method, all queries inside the block will be sent to the specified shard.

Octopus.using(:slave_two) do
Octopus.using(:slave_two) do
User.create(:name => "Mike")
end

Expand All @@ -70,7 +70,7 @@ Each model instance knows which shard it came from so this will work automatical
#Sets the name
@user.name = "Mike"

# Save the user in the correct shard, shard1.
# Save the user in the correct shard, shard1.
@user.save

### Migrations
Expand All @@ -89,7 +89,7 @@ In migrations, you also have access to the using method. The syntax is basically
end
end

You also could send a migration to a group of shards. This migration will be sent to all shards that belongs to history_shards group, specified in shards.yml:
You also could send a migration to a group of shards. This migration will be sent to all shards that belongs to history_shards group, specified in shards.yml:

class CreateUsersOnMultiplesGroups < ActiveRecord::Migration
using_group(:history_shards)
Expand All @@ -106,13 +106,13 @@ You also could send a migration to a group of shards. This migration will be se
### Rails Controllers

If you want to send a specified action, or all actions from a controller, to a specific shard, use this syntax:

class ApplicationController < ActionController::Base
around_filter :select_shard
around_filter :select_shard

def select_shard(&block)
Octopus.using(:brazil, &block)
end
end
end

To see the complete list of features and syntax, please check out our <a href="http://wiki.github.com/tchandy/octopus/"> Wiki</a>
Expand Down
12 changes: 6 additions & 6 deletions lib/octopus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.config()
# Returns the Rails.root_to_s when you are using rails
# Running the current directory in a generic Ruby process
def self.directory()
@directory ||= defined?(Rails) ? Rails.root.to_s : Dir.pwd
@directory ||= defined?(Rails) ? Rails.root.to_s : Dir.pwd
end

# This is the default way to do Octopus Setup
Expand All @@ -52,25 +52,25 @@ def self.rails3?
end

def self.rails?
defined?(Rails)
defined?(Rails)
end

def self.shards=(shards)
@config ||= HashWithIndifferentAccess.new
@config[rails_env()] = HashWithIndifferentAccess.new(shards)
ActiveRecord::Base.connection.initialize_shards(@config)
end

def self.using(shard, &block)
ActiveRecord::Base.hijack_initializer()
conn = ActiveRecord::Base.connection

if conn.is_a?(Octopus::Proxy)
conn.run_queries_on_shard(shard, &block)
else
yield
end
end
end
end


Expand Down
8 changes: 4 additions & 4 deletions lib/octopus/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module InstanceMethods
def set_connection_on_association(record)
return if !self.connection.respond_to?(:current_shard) || !self.respond_to?(:current_shard)
if !record.current_shard.nil? && !self.current_shard.nil? && record.current_shard != self.current_shard
raise "Association Error: Records are from different shards"
raise "Association Error: Records are from different shards"
end

record.current_shard = self.connection.current_shard = self.current_shard if should_set_current_shard?
end
end
Expand All @@ -28,15 +28,15 @@ def default_octopus_opts(options)
if options[:before_add].is_a?(Array)
options[:before_add] << :set_connection_on_association
elsif options[:before_add].is_a?(Symbol)
options[:before_add] = [:set_connection_on_association, options[:before_add]]
options[:before_add] = [:set_connection_on_association, options[:before_add]]
else
options[:before_add] = :set_connection_on_association
end

if options[:before_remove].is_a?(Array)
options[:before_remove] << :set_connection_on_association
elsif options[:before_remove].is_a?(Symbol)
options[:before_remove] = [:set_connection_on_association, options[:before_remove]]
options[:before_remove] = [:set_connection_on_association, options[:before_remove]]
else
options[:before_remove] = :set_connection_on_association
end
Expand Down
6 changes: 3 additions & 3 deletions lib/octopus/association_collection.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Octopus::AssociationCollection
module Octopus::AssociationCollection
def should_wrap_the_connection?
@owner.respond_to?(:current_shard) && @owner.current_shard != nil
end

def count(*args)
if should_wrap_the_connection?
Octopus.using(@owner.current_shard) { super }
else
Octopus.using(@owner.current_shard) { super }
else
super
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/octopus/has_and_belongs_to_many_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ def self.included(base)
alias_method_chain :insert_record, :octopus
end
end

def insert_record_with_octopus(record, force = true, validate = true)
if should_wrap_the_connection?
Octopus.using(@owner.current_shard) { insert_record_without_octopus(record, force, validate) }
else
insert_record_without_octopus(record, force, validate)
Octopus.using(@owner.current_shard) { insert_record_without_octopus(record, force, validate) }
else
insert_record_without_octopus(record, force, validate)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/octopus/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

class Octopus::Logger < Logger
def format_message(severity, timestamp, progname, msg)
str = super
str = super

if ActiveRecord::Base.connection.respond_to?(:current_shard)
str += "Shard: #{ActiveRecord::Base.connection.current_shard} -"
str += "Shard: #{ActiveRecord::Base.connection.current_shard} -"
end

str
end
end
Expand Down
22 changes: 11 additions & 11 deletions lib/octopus/migration.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Octopus::Migration
module Octopus::Migration
def self.extended(base)
class << base
def announce_with_octopus(message)
announce_without_octopus("#{message} - #{get_current_shard}")
end

alias_method_chain :migrate, :octopus
alias_method_chain :announce, :octopus
attr_accessor :current_shard
Expand All @@ -19,7 +19,7 @@ def using(*args)

self.connection().block = true
self.current_shard = args
self.connection().current_shard = args
self.connection().current_shard = args
end

return self
Expand All @@ -38,10 +38,10 @@ def using_group(*args)
self.connection().block = true
self.connection().current_group = args
end

return self
end

def get_current_shard
"Shard: #{ActiveRecord::Base.connection.current_shard()}" if ActiveRecord::Base.connection.respond_to?(:current_shard)
end
Expand All @@ -51,16 +51,16 @@ def migrate_with_octopus(direction)
conn = ActiveRecord::Base.connection
return migrate_without_octopus(direction) unless conn.is_a?(Octopus::Proxy)
self.connection().current_shard = self.current_shard if self.current_shard != nil

groups = conn.instance_variable_get(:@groups)

begin
if conn.current_group.is_a?(Array)
conn.current_group.each { |group| conn.send_queries_to_multiple_shards(groups[group]) { migrate_without_octopus(direction) } }
elsif conn.current_group.is_a?(Symbol)
conn.send_queries_to_multiple_shards(groups[conn.current_group]) { migrate_without_octopus(direction) }
conn.current_group.each { |group| conn.send_queries_to_multiple_shards(groups[group]) { migrate_without_octopus(direction) } }
elsif conn.current_group.is_a?(Symbol)
conn.send_queries_to_multiple_shards(groups[conn.current_group]) { migrate_without_octopus(direction) }
elsif conn.current_shard.is_a?(Array)
conn.send_queries_to_multiple_shards(conn.current_shard) { migrate_without_octopus(direction) }
conn.send_queries_to_multiple_shards(conn.current_shard) { migrate_without_octopus(direction) }
else
migrate_without_octopus(direction)
end
Expand Down
26 changes: 13 additions & 13 deletions lib/octopus/model.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Octopus::Model
def self.extended(base)
module Octopus::Model
def self.extended(base)
base.send(:include, InstanceMethods)
base.extend(ClassMethods)
base.hijack_connection()
Expand All @@ -9,7 +9,7 @@ module SharedMethods
def clean_table_name
return unless self.connection_proxy.should_clean_table_name?
if self != ActiveRecord::Base && self.respond_to?(:reset_table_name) && !self.read_inheritable_attribute(:set_table_name)
self.reset_table_name()
self.reset_table_name()
end

if Octopus.rails3?
Expand All @@ -20,7 +20,7 @@ def clean_table_name

def using(shard)
return self if defined?(::Rails) && !Octopus.environments.include?(Rails.env.to_s)

hijack_initializer() if !respond_to?(:set_current_shard)
clean_table_name()

Expand All @@ -35,9 +35,9 @@ def hijack_initializer()

def set_current_shard
if new_record? || self.class.connection_proxy.block
self.current_shard = self.class.connection_proxy.current_shard
self.current_shard = self.class.connection_proxy.current_shard
else
self.current_shard = self.class.connection_proxy.last_current_shard
self.current_shard = self.class.connection_proxy.last_current_shard
end
end

Expand All @@ -54,7 +54,7 @@ def hijack_connection()
def self.should_use_normal_connection?
(defined?(Rails) && Octopus.config() && !Octopus.environments.include?(Rails.env.to_s)) || self.read_inheritable_attribute(:establish_connection)
end

def self.connection_proxy
Thread.current[:connection_proxy] ||= Octopus::Proxy.new(Octopus.config())
end
Expand Down Expand Up @@ -90,18 +90,18 @@ module ClassMethods
def replicated_model()
write_inheritable_attribute(:replicated, true)
end

def sharded_model()
write_inheritable_attribute(:sharded, true)
end
write_inheritable_attribute(:sharded, true)
end

def octopus_establish_connection(spec = nil)
write_inheritable_attribute(:establish_connection, true)
write_inheritable_attribute(:establish_connection, true)
establish_connection(spec)
end

def octopus_set_table_name(value = nil, &block)
write_inheritable_attribute(:set_table_name, true)
write_inheritable_attribute(:set_table_name, true)
set_table_name(value, &block)
end
end
Expand Down
Loading

0 comments on commit 8558c52

Please sign in to comment.