Class: XBar::Proxy

Inherits:
Object
  • Object
show all
Includes:
Colors, Mapper
Defined in:
lib/xbar/proxy.rb

Constant Summary

Constants included from Colors

Colors::BLACK_BACK, Colors::BLACK_TEXT, Colors::BLINK_OFF, Colors::BLINK_ON, Colors::BLUE_BACK, Colors::BLUE_TEXT, Colors::BOLD_OFF, Colors::BOLD_ON, Colors::BROWN_BACK, Colors::BROWN_TEXT, Colors::CYAN_BACK, Colors::CYAN_TEXT, Colors::GRAY_TEXT, Colors::GREEN_BACK, Colors::GREEN_TEXT, Colors::MAGENTA_BACK, Colors::MAGENTA_TEXT, Colors::RED_BACK, Colors::RED_TEXT, Colors::RESET_COLORS, Colors::REVERSE_OFF, Colors::REVERSE_ON, Colors::WHITE_BACK

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mapper

exports, #register, #reset_config

Methods included from Mapper::ClassMethods

#app_env, #config, #config_file_name, #config_from_file, #connection_file_name, #environments, #initialize_options, #register, #reset, #shards=, #xbar_env

Constructor Details

#initializeProxy

Returns a new instance of Proxy.



22
23
24
25
26
27
# File 'lib/xbar/proxy.rb', line 22

def initialize
  puts "Initializing new proxy."
  register
  reset_shards
  clean_proxy
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/xbar/proxy.rb', line 154

def method_missing(method, *args, &block)
  if XBar.debug
    puts("\nProxy##{BLUE_TEXT}method_missing#{RESET_COLORS}: " + 
      "method = #{RED_TEXT}#{method}#{RESET_COLORS}, " +
      "current_shard=#{current_shard}, " +
      "in_block_scope=#{in_block_scope?}")
  end

  if method.to_s =~ /insert|select|execute/ && !in_block_scope? # should clean connection
    shard = @last_current_shard = current_shard
    clean_proxy
    @shard_list[shard].run_queries(method, *args, &block)
  else
    select_shard.run_queries(method, *args, &block)
  end
end

Instance Attribute Details

#current_modelObject

Setters for these are written by hand below.



18
19
20
# File 'lib/xbar/proxy.rb', line 18

def current_model
  @current_model
end

#current_shardObject

Setters for these are written by hand below.



18
19
20
# File 'lib/xbar/proxy.rb', line 18

def current_shard
  @current_shard
end

#last_current_shardObject (readonly)

No setter method.



15
16
17
# File 'lib/xbar/proxy.rb', line 15

def last_current_shard
  @last_current_shard
end

#shard_listObject (readonly)

Returns the value of attribute shard_list.



20
21
22
# File 'lib/xbar/proxy.rb', line 20

def shard_list
  @shard_list
end

Instance Method Details

#check_schema_migrations(shard_name) ⇒ Object



138
139
140
# File 'lib/xbar/proxy.rb', line 138

def check_schema_migrations(shard_name)
  @shard_list[shard_name].check_schema_migrations
end

#clean_proxyObject



36
37
38
39
40
41
42
43
# File 'lib/xbar/proxy.rb', line 36

def clean_proxy
  if XBar.debug
    puts "Proxy##{BLUE_TEXT}clean_proxy=#{RESET_COLORS}: " +
      "current shard = #{@current_shard}"
  end
  @current_shard = :master
  clear_block_scope
end

#clear_block_scopeObject



99
100
101
102
# File 'lib/xbar/proxy.rb', line 99

def clear_block_scope
  @in_block = @old_in_block = false
  @depth = 0
end

#connection_poolObject



175
176
177
178
179
# File 'lib/xbar/proxy.rb', line 175

def connection_pool
  cp = shards[current_shard].first 
  cp.automatic_reconnect = true if XBar.rails31?
  cp
end

#enter_block_scopeObject



83
84
85
86
87
# File 'lib/xbar/proxy.rb', line 83

def enter_block_scope
  @old_in_block = @in_block
  @depth += 1
  @in_block = true
end

#enter_statistics(shard_name, config, method) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/xbar/proxy.rb', line 181

def enter_statistics(shard_name, config, method)
 
  return unless XBar.collect_stats?
 
  saved_current_model = current_model
  if UsageStatistics.connection.kind_of?(XBar::Proxy)
    connection_pool = UsageStatistics.connection.shards[:master][0]
    connection_pool.automatic_reconnect = true
    connection = connection_pool.connection
   
    return unless connection.table_exists? :usage_statistics

    # We have an AbstractAdapter.  We must insert statistics using this
    # adapter, not the Proxy, because otherwise the insertion triggers
    # 'enter_statistics' again and we would be in an infinite loop.

=begin
    params = {
      shard_name: shard_name,
      method: method.to_s,
      adapter: config[:adapter],
      username: config[:username],
      thread_id: Thread.current.object_id.to_s,
      port: (config[:port] || "default"),
      host: config[:host],
      database_name: config[:database]}
    insert(connection, params) -- alternatively
=end
   
    sql = "INSERT INTO usage_statistics " +
      "(shard_name, method, adapter, username, thread_id, port, host, database_name) " +
      "VALUES (\'#{shard_name}\', \'#{method.to_s}\', \'#{config[:adapter]}\', " +
      "\'#{config[:username]}\', \'#{Thread.current.object_id.to_s}\', " +
      "#{config[:port] || "default"}, \'#{config[:host]}\', \'#{config[:database]}\')"
    connection.execute(sql)

    self.current_model = saved_current_model
  end
end

#in_block_scope?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/xbar/proxy.rb', line 95

def in_block_scope?
  @in_block
end

#leave_block_scopeObject



89
90
91
92
93
# File 'lib/xbar/proxy.rb', line 89

def leave_block_scope
  @in_block = @old_in_block
  @old_in_block = false
  @depth -= 1
end

#quote_table_name(table_name) ⇒ Object



150
151
152
# File 'lib/xbar/proxy.rb', line 150

def quote_table_name(table_name)
  select_shard.quote_table_name(table_name)
end

#reset_proxyObject



29
30
31
32
33
34
# File 'lib/xbar/proxy.rb', line 29

def reset_proxy
  @current_shard = :master
  puts ">>> reset_proxy: #{current_shard}" if XBar.debug
  clear_block_scope
  reset_shards
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/xbar/proxy.rb', line 171

def respond_to?(method, include_private = false)
  super || current_shard.respond_to?(method, include_private)
end

#run_queries_on_shard(shard_name, use_scope = true) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/xbar/proxy.rb', line 112

def run_queries_on_shard(shard_name, use_scope = true)
  older_shard = current_shard 
  enter_block_scope if use_scope
  self.current_shard = shard_name
  if XBar.debug
    puts "Proxy##{BLUE_TEXT}run_queries_on_shard#{RESET_COLORS}: " +
      "current shard = #{current_shard}, use_scope = #{use_scope}, " +
      "previous shard = #{older_shard}"
  end
  result = yield
ensure
  if XBar.debug
    puts "Proxy##{BLUE_TEXT}run_queries_on_shard#{RESET_COLORS}: " +
      "restoring previous shard = #{older_shard}"
  end
  leave_block_scope if use_scope
  self.current_shard = older_shard
end

#schema_cacheObject



146
147
148
# File 'lib/xbar/proxy.rb', line 146

def schema_cache
  select_shard.schema_cache
end

#select_shardObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/xbar/proxy.rb', line 62

def select_shard
  if current_shard.kind_of? Array
    shard = current_shard.first
    if current_shard.size != 1
      puts "WARNING: selecting only first shard from array"
    end
  else
    shard = current_shard
  end
  unless @shard_list[shard] 
     puts "Shard not found: current_shard = #{shard}, @shard_list = #{@shard_list.keys}"     
  end
  @shard_list[shard]
end

#send_queries_to_multiple_shards(shard_names, &block) ⇒ Object



131
132
133
134
135
136
# File 'lib/xbar/proxy.rb', line 131

def send_queries_to_multiple_shards(shard_names, &block)
  shard_names = Array(shard_names)
  shard_names.each do |name|
    run_queries_on_shard(name, &block)
  end
end

#should_clean_table_name?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/xbar/proxy.rb', line 104

def should_clean_table_name?
  adapters.size > 1
end

#transaction(options = {}, &block) ⇒ Object



142
143
144
# File 'lib/xbar/proxy.rb', line 142

def transaction(options = {}, &block)
  select_shard.transaction(options, &block)
end

#verify_connectionObject



108
109
110
# File 'lib/xbar/proxy.rb', line 108

def verify_connection
  XBar::Mapper.options[:verify_connection]
end