ar-octopus 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -29,7 +29,7 @@ begin
29
29
  gem.authors = ["Thiago Pradi", "Mike Perham", "Amit Agarwal"]
30
30
  gem.add_development_dependency "rspec", ">= 1.2.9"
31
31
  gem.add_dependency('activerecord')
32
- gem.version = "0.0.9"
32
+ gem.version = "0.0.10"
33
33
  end
34
34
  Jeweler::GemcutterTasks.new
35
35
  rescue LoadError
data/ar-octopus.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ar-octopus}
8
- s.version = "0.0.9"
8
+ s.version = "0.0.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Thiago Pradi", "Mike Perham", "Amit Agarwal"]
data/lib/octopus/proxy.rb CHANGED
@@ -3,16 +3,17 @@ class Octopus::Proxy
3
3
 
4
4
  def initialize(config)
5
5
  initialize_shards(config)
6
- initialize_replication() if config[Octopus.env()]["replicated"]
6
+ initialize_replication() if config[Octopus.env()] && config[Octopus.env()]["replicated"]
7
7
  end
8
-
8
+
9
9
  def initialize_shards(config)
10
10
  @shards = {}
11
11
  @groups = {}
12
12
  @shards[:master] = ActiveRecord::Base.connection_pool()
13
13
  @current_shard = :master
14
+ shards_config = config[Octopus.env()]["shards"] || []
14
15
 
15
- config[Octopus.env()]["shards"].each do |key, value|
16
+ shards_config.each do |key, value|
16
17
  if value.has_key?("adapter")
17
18
  initialize_adapter(value['adapter'])
18
19
  @shards[key.to_sym] = connection_pool_for(value, "#{value['adapter']}_connection")
@@ -28,13 +29,13 @@ class Octopus::Proxy
28
29
  end
29
30
  end
30
31
  end
31
-
32
+
32
33
  def initialize_replication()
33
34
  @replicated = true
34
35
  @slaves_list = @shards.keys.map {|sym| sym.to_s}.sort
35
36
  @slaves_list.delete('master')
36
37
  end
37
-
38
+
38
39
  def current_shard=(shard_symbol)
39
40
  if shard_symbol.is_a?(Array)
40
41
  shard_symbol.each {|symbol| raise "Nonexistent Shard Name: #{symbol}" if @shards[symbol].nil? }
@@ -54,11 +55,11 @@ class Octopus::Proxy
54
55
 
55
56
  @current_group = group_symbol
56
57
  end
57
-
58
+
58
59
  def current_model=(model)
59
60
  @current_model = model.is_a?(ActiveRecord::Base) ? model.class : model
60
61
  end
61
-
62
+
62
63
  def select_connection()
63
64
  @shards[shard_name].connection()
64
65
  end
@@ -66,7 +67,7 @@ class Octopus::Proxy
66
67
  def shard_name
67
68
  current_shard.is_a?(Array) ? current_shard.first : current_shard
68
69
  end
69
-
70
+
70
71
  def add_transaction_record(record)
71
72
  if !select_connection().instance_variable_get(:@_current_transaction_records).nil?
72
73
  select_connection().add_transaction_record(record)
@@ -106,12 +107,12 @@ class Octopus::Proxy
106
107
  select_connection().send(method, *args, &block)
107
108
  end
108
109
  end
109
-
110
+
110
111
  def run_queries_on_shard(shard, &block)
111
112
  older_shard = self.current_shard
112
113
  self.block = true
113
114
  self.current_shard = shard
114
-
115
+
115
116
  begin
116
117
  yield
117
118
  ensure
@@ -119,7 +120,7 @@ class Octopus::Proxy
119
120
  self.current_shard = older_shard
120
121
  end
121
122
  end
122
-
123
+
123
124
  protected
124
125
  def connection_pool_for(adapter, config)
125
126
  ActiveRecord::ConnectionAdapters::ConnectionPool.new(ActiveRecord::Base::ConnectionSpecification.new(adapter, config))
@@ -180,7 +181,7 @@ class Octopus::Proxy
180
181
 
181
182
  def send_queries_to_selected_slave(method, *args, &block)
182
183
  old_shard = self.current_shard
183
-
184
+
184
185
  if current_model.read_inheritable_attribute(:replicated)
185
186
  if !using_enabled
186
187
  self.current_shard = @slaves_list.shift.to_sym
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar-octopus
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 9
10
- version: 0.0.9
9
+ - 10
10
+ version: 0.0.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Thiago Pradi