Class: ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/active_rdf/federation/connection_pool.rb

Overview

Maintains pool of adapter instances that are connected to datasources. Returns right adapter for a given datasource, by either reusing an existing adapter-instance or creating new a adapter-instance.

Constant Summary collapse

@@adapter_pool =

pool of all adapters

Array.new
@@adapter_parameters =

pool of connection parameters to all adapter

Array.new
@@registered_adapter_types =

adapters-classes known to the pool, registered by the adapter-class itself using register_adapter method, used to select new adapter-instance for requested connection type

Hash.new

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.write_adapterObject

Returns the value of attribute write_adapter.



9
10
11
# File 'lib/active_rdf/federation/connection_pool.rb', line 9

def write_adapter
  @write_adapter
end

Class Method Details

.adapter_typesObject



51
52
53
# File 'lib/active_rdf/federation/connection_pool.rb', line 51

def ConnectionPool.adapter_types
	@@registered_adapter_types.keys
end

.adaptersObject



42
43
44
# File 'lib/active_rdf/federation/connection_pool.rb', line 42

def ConnectionPool.adapters
  @@adapter_pool.dup
end

.add_data_source(connection_params) ⇒ Object Also known as: add

returns adapter-instance for given parameters (either existing or new)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_rdf/federation/connection_pool.rb', line 65

def ConnectionPool.add_data_source(connection_params)
  $activerdflog.info "ConnectionPool: add_data_source with params: #{connection_params.inspect}"

  # either get the adapter-instance from the pool
  # or create new one (and add it to the pool)
  index = @@adapter_parameters.index(connection_params)
  if index.nil?
    # adapter not in the pool yet: create it,
    # register its connection parameters in parameters-array
    # and add it to the pool (at same index-position as parameters)
    $activerdflog.debug("Create a new adapter for parameters #{connection_params.inspect}")
    adapter = create_adapter(connection_params)
    @@adapter_parameters << connection_params
    @@adapter_pool << adapter
  else
    # if adapter parametrs registered already,
    # then adapter must be in the pool, at the same index-position as its parameters
    $activerdflog.debug("Reusing existing adapter")
    adapter = @@adapter_pool[index]
  end

  # sets the adapter as current write-source if it can write
  self.write_adapter = adapter if adapter.writes?

  return adapter
end

.clearObject

clears the pool: removes all registered data sources



35
36
37
38
39
40
# File 'lib/active_rdf/federation/connection_pool.rb', line 35

def ConnectionPool.clear
  $activerdflog.info "ConnectionPool: clear called"
  @@adapter_pool = []
  @@adapter_parameters = []
  self.write_adapter = nil
end

.flushObject

flushes all openstanding changes into the original datasource.



47
48
49
# File 'lib/active_rdf/federation/connection_pool.rb', line 47

def ConnectionPool.flush
	write_adapter.flush
end

.read_adaptersObject

returns the set of currently registered read-access datasources



56
57
58
# File 'lib/active_rdf/federation/connection_pool.rb', line 56

def ConnectionPool.read_adapters
  @@adapter_pool.select {|adapter| adapter.reads? }
end

.register_adapter(type, klass) ⇒ Object

adapter-types can register themselves with connection pool by indicating which adapter-type they are



132
133
134
135
# File 'lib/active_rdf/federation/connection_pool.rb', line 132

def ConnectionPool.register_adapter(type, klass)
  $activerdflog.info "ConnectionPool: registering adapter of type #{type} for class #{klass}"
  @@registered_adapter_types[type] = klass
end

.remove_data_source(adapter) ⇒ Object

remove one adapter from activerdf



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/active_rdf/federation/connection_pool.rb', line 93

def ConnectionPool.remove_data_source(adapter)
  $activerdflog.info "ConnectionPool: remove_data_source with params: #{adapter.to_s}"
  
  index = @@adapter_pool.index(adapter)

  # remove_data_source mit be called repeatedly, e.g because the adapter object is stale
  unless index.nil?
    @@adapter_parameters.delete_at(index)
    @@adapter_pool.delete_at(index)
    if self.write_adapters.empty?
      self.write_adapter = nil
    else
      self.write_adapter = self.write_adapters.first
    end
  end
  
end

.set_data_source(adapter, connection_params = {}) ⇒ Object

sets adapter-instance for connection parameters (if you want to re-enable an existing adapter)



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/active_rdf/federation/connection_pool.rb', line 112

def ConnectionPool.set_data_source(adapter, connection_params = {})
	index = @@adapter_parameters.index(connection_params)
	if index.nil?
     @@adapter_parameters << connection_params
     @@adapter_pool << adapter
	else
		@@adapter_pool[index] = adapter
	end
	self.write_adapter = adapter if adapter.writes?
	adapter
end

.write_adaptersObject



60
61
62
# File 'lib/active_rdf/federation/connection_pool.rb', line 60

def ConnectionPool.write_adapters
  @@adapter_pool.select {|adapter| adapter.writes? }
end