Module: DWH::Factory

Includes:
Logger
Included in:
DWH
Defined in:
lib/dwh/factory.rb

Overview

Manages adapters. This should be the means by which adapters are created, loaded, and pooled.

Instance Method Summary collapse

Methods included from Logger

#logger, logger

Instance Method Details

#adapter?(adapter_name) ⇒ Boolean

Check if the given adapter is registered

Parameters:

  • adapter_name (String, Symbol)

Returns:

  • (Boolean)


35
36
37
# File 'lib/dwh/factory.rb', line 35

def adapter?(adapter_name)
  adapters.key?(adapter_name.to_sym)
end

#adaptersObject

Get the list of registed adapters



40
41
42
# File 'lib/dwh/factory.rb', line 40

def adapters
  @adapters ||= {}
end

#create(adapter_name, config) ⇒ Object

The canonical way of creating an adapter instance in DWH.

Examples:

connect to MySQL

DWH.create(:mysql, { host: '127.0.0.1', databse: 'mydb', username: 'me', password: 'mypwd', client_name: 'Strata CLI'})

connect Trino

DWH.create(:trino, {host: 'localhost', catalog: 'native', username: 'Ajo'})

connect to Druid

DWH.create(:druid, {host: 'localhost',port: 8080, protocol: 'http'})

Parameters:

  • adapter_name (String, Symbol)
  • config (Hash)

    options hash for the target database



60
61
62
# File 'lib/dwh/factory.rb', line 60

def create(adapter_name, config)
  get_adapter(adapter_name).new(config)
end

#get_adapter(adapter_name) ⇒ Object

Get the adapter.

Parameters:

  • adapter_name (String, Symbol)


27
28
29
30
31
# File 'lib/dwh/factory.rb', line 27

def get_adapter(adapter_name)
  raise "Adapter '#{adapter_name}' not found. Did you forget to register it: DWH.register(MyAdapterClass)" unless adapter?(adapter_name)

  adapters[adapter_name.to_sym]
end

#pool(name, adapter_name, config, timeout: 5, size: 10) ⇒ Object

Create a pool of connections for a given name and adapter. Returns existing pool if it was already created.

Parameters:

  • name (String)

    custom name for your pool

  • adapter_name (String, Symbol)
  • config (Hash)

    connection options

  • timeout (Integer) (defaults to: 5)

    pool checkout time out

  • size (Integer) (defaults to: 10)

    size of the pool



72
73
74
75
76
77
78
79
80
# File 'lib/dwh/factory.rb', line 72

def pool(name, adapter_name, config, timeout: 5, size: 10)
  if pools.key?(name)
    pools[name]
  else
    pools[name] = ConnectionPool.new(size: size, timeout: timeout) do
      create(adapter_name, config)
    end
  end
end

#poolsObject

Current active pools



45
46
47
# File 'lib/dwh/factory.rb', line 45

def pools
  @pools ||= {}
end

#register(adapter_name, adapter_class) ⇒ Object

Register your new adapter.

Parameters:

  • adapter_name (String, Symbol)

    your adapter name. Could be different from the class name.

  • adapter_class (Class)

    actual class of the adapter.

Raises:



13
14
15
16
17
18
# File 'lib/dwh/factory.rb', line 13

def register(adapter_name, adapter_class)
  raise ConfigError, 'adapter_class should be a class' unless adapter_class.is_a?(Class)

  adapter_class.load_settings
  adapters[adapter_name.to_sym] = adapter_class
end

#shutdown(pool = nil) ⇒ Object

Shutdown a specific pool or all pools

Parameters:

  • pool (String, ConnectionPool, nil) (defaults to: nil)

    pool or name of pool or nil to shut everything down



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dwh/factory.rb', line 85

def shutdown(pool = nil)
  case pool.class
  when String
    pools[pool].shutdown { it.close }
    pools.delete(pool)
  when Symbol
    pools[pool.to_s].shutdown { it.close }
    pools[pool.to_s].delete
  when ConnectionPool
    pool.shutdown { it.close }
    pools.delete(pools.key(pool))
  else
    pools.each_value do |val|
      val.shutdown { c.close }
    end
    @pools = {}
  end
end

#start_reaper(frequency = 300) ⇒ Object

Start reaper that will periodically clean up unused or idle connections.

Parameters:

  • frequency (Integer) (defaults to: 300)

    defaults to 300 seconds



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dwh/factory.rb', line 107

def start_reaper(frequency = 300)
  logger.info 'Starting DB Adapter reaper process'
  Thread.new do
    loop do
      pools.each do |name, pool|
        logger.info "DB POOL FOR #{name} STATS:"
        pool.with do
          logger.info "\tSize:      #{pool.size}"
          logger.info "\tIdle:      #{pool.available}"
          logger.info "\tAvailable: #{pool.available}"
        end
        pool.reap(frequency) { it.close }
      end
      sleep frequency
    end
  end
end

#unregister(adapter_name) ⇒ Object

Remove the given adapter from the registry.



21
22
23
# File 'lib/dwh/factory.rb', line 21

def unregister(adapter_name)
  adapters.delete adapter_name.to_sym
end