Class: ActiveShard::ActiveRecord::ConnectionHandler

Inherits:
ActiveRecord::ConnectionAdapters::ConnectionHandler
  • Object
show all
Defined in:
lib/active_shard/active_record/connection_handler.rb

Defined Under Namespace

Classes: ConnectionPoolHash, PoolKey

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ConnectionHandler

Initializes a new ConnectionHandler

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):



18
19
20
21
22
23
24
# File 'lib/active_shard/active_record/connection_handler.rb', line 18

def initialize( options={} )
  @shard_lookup = options[ :shard_lookup ]

  @shard_definitions = []
  @connection_pools = ConnectionPoolHash.new( self )
  @schema_pools     = {}
end

Instance Attribute Details

#shard_lookupObject

Used to look up shard names



11
12
13
# File 'lib/active_shard/active_record/connection_handler.rb', line 11

def shard_lookup
  @shard_lookup
end

Instance Method Details

#add_shard(shard_definition) ⇒ Object



32
33
34
35
36
37
# File 'lib/active_shard/active_record/connection_handler.rb', line 32

def add_shard( shard_definition )
  connection_pools[ connection_pool_id( shard_definition ) ] = new_connection_pool( shard_definition )
  shard_definitions << shard_definition

  self
end

#add_shards(shard_definitions) ⇒ Object



27
28
29
# File 'lib/active_shard/active_record/connection_handler.rb', line 27

def add_shards( shard_definitions )
  shard_definitions.each( &method(:add_shard) )
end

#connection_pool(schema_name, active_shard_name = nil) ⇒ Object



87
88
89
90
91
# File 'lib/active_shard/active_record/connection_handler.rb', line 87

def connection_pool( schema_name, active_shard_name = nil )
  ( active_shard_name.nil? ?
      get_schema_pool_for( schema_name ) :
      connection_pools[ connection_pool_id( schema_name, active_shard_name ) ] )
end

#remove_all_shards!Object



62
63
64
65
66
# File 'lib/active_shard/active_record/connection_handler.rb', line 62

def remove_all_shards!
  defs = shard_definitions.dup

  defs.each( &method(:remove_shard) )
end

#remove_shard(shard_definition) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/active_shard/active_record/connection_handler.rb', line 40

def remove_shard( shard_definition )
  schema_name = shard_definition.schema

  # Remove SchemaPool if it's based on this shard
  #
  if ( schema_pools[ schema_name ] && schema_pools[ schema_name ].shard_definition == shard_definition )
    schema_pools[ schema_name ].disconnect!
    schema_pools.delete( schema_name )
  end

  pool_id = connection_pool_id( shard_definition )

  # Remove connection pool
  connection_pools[ pool_id ].disconnect!
  connection_pools.delete( pool_id )

  shard_definitions.delete( shard_definition )

  self
end

#retrieve_connection_pool(klass) ⇒ ConnectionPool, SchemaConnectionPool

Retrieve connection pool for class

Parameters:

  • klass (#schema_name)

    An object which responds to #schema_name

Returns:

  • (ConnectionPool, SchemaConnectionPool)

    connection pool



79
80
81
82
83
84
85
# File 'lib/active_shard/active_record/connection_handler.rb', line 79

def retrieve_connection_pool( klass )
  schema_name = ( sn = klass.schema_name ).nil? ? nil : sn.to_sym

  active_shard_name = shard_lookup.lookup_active_shard( schema_name )
  
  connection_pool schema_name, active_shard_name
end