Class: Rails::Sharding::ShardThreadRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/sharding/shard_thread_registry.rb

Class Method Summary collapse

Class Method Details

.connect_back_to_master!Object

Clears the connection stack and goes back to connecting to master



29
30
31
32
33
# File 'lib/rails/sharding/shard_thread_registry.rb', line 29

def self.connect_back_to_master!
  shard_group_stack.clear
  shard_name_stack.clear
  shard_connection_used_stack.clear
end

.connecting_to_master?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/rails/sharding/shard_thread_registry.rb', line 20

def self.connecting_to_master?
  current_shard_group.nil? || current_shard_name.nil?
end

.connecting_to_shard?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rails/sharding/shard_thread_registry.rb', line 24

def self.connecting_to_shard?
  !connecting_to_master?
end

.current_connection_used?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/rails/sharding/shard_thread_registry.rb', line 45

def self.current_connection_used?
  shard_connection_used_stack.last
end

.current_shard_groupObject

Returns the current shard group (for the current Thread)



36
37
38
# File 'lib/rails/sharding/shard_thread_registry.rb', line 36

def self.current_shard_group
  shard_group_stack.last
end

.current_shard_group_and_nameObject



72
73
74
# File 'lib/rails/sharding/shard_thread_registry.rb', line 72

def self.current_shard_group_and_name
  [current_shard_group, current_shard_name]
end

.current_shard_nameObject

Returns the current shard name (for the current Thread)



41
42
43
# File 'lib/rails/sharding/shard_thread_registry.rb', line 41

def self.current_shard_name
  shard_name_stack.last
end

.notify_connection_retrievedObject

notifies the current connection was used (wee keep track of this to warn the user in case the connection is not used)



63
64
65
# File 'lib/rails/sharding/shard_thread_registry.rb', line 63

def self.notify_connection_retrieved
  shard_connection_used_stack[-1] = true if shard_connection_used_stack.present?
end

.pop_current_shardObject

removes shard connection to the stack



68
69
70
# File 'lib/rails/sharding/shard_thread_registry.rb', line 68

def self.pop_current_shard
  [shard_group_stack.pop, shard_name_stack.pop, shard_connection_used_stack.pop]
end

.push_current_shard(group, name) ⇒ Object

adds shard connection to the stack



50
51
52
53
54
55
56
57
58
59
# File 'lib/rails/sharding/shard_thread_registry.rb', line 50

def self.push_current_shard(group, name)
  # this line supresses the unused connection warning when there are nested
  # using_shard blocks. We suppress the warning because we view nested using_shard
  # blocks as a override
  notify_connection_retrieved

  shard_group_stack.push(group.blank? ? nil : group.to_sym)
  shard_name_stack.push(name.blank? ? nil : name.to_sym)
  shard_connection_used_stack.push(false)
end

.shard_connection_used_stackObject



18
# File 'lib/rails/sharding/shard_thread_registry.rb', line 18

def self.shard_connection_used_stack; self._shard_connection_used_stack ||= [] end

.shard_group_stackObject

accessors that initialize stacks if necessary



16
# File 'lib/rails/sharding/shard_thread_registry.rb', line 16

def self.shard_group_stack; self._shard_group_stack ||= [] end

.shard_name_stackObject



17
# File 'lib/rails/sharding/shard_thread_registry.rb', line 17

def self.shard_name_stack; self._shard_name_stack ||= [] end