Class: ActiveRDF::ConnectionPool

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

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.



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

def write_adapter
  @write_adapter
end

Class Method Details

.adapter_typesObject



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

def ConnectionPool.adapter_types
  @@registered_adapter_types.keys
end

.adaptersObject



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

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)



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
91
# File 'lib/active_rdf/federation/connection_pool.rb', line 66

def ConnectionPool.add_data_source(connection_params)
ActiveRdfLogger::log_info(self) { "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)
  ActiveRdfLogger::log_debug(self) { "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
  ActiveRdfLogger::log_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



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

def ConnectionPool.clear
ActiveRdfLogger::log_info "Clear called", self
  @@adapter_pool = []
  @@adapter_parameters = []
  self.write_adapter = nil
end

.close_data_source(adapter) ⇒ Object Also known as: close

closes and removes adapter from pool



113
114
115
116
# File 'lib/active_rdf/federation/connection_pool.rb', line 113

def ConnectionPool.close_data_source(adapter)
  remove_data_source(adapter)
  adapter.close
end

.flushObject

flushes all openstanding changes into the original datasource.



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

def ConnectionPool.flush
  write_adapter.flush
end

.read_adaptersObject

returns the set of currently registered read-access datasources



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

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

.register_adapter(type, klass) ⇒ Object

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



140
141
142
143
# File 'lib/active_rdf/federation/connection_pool.rb', line 140

def ConnectionPool.register_adapter(type, klass)
ActiveRdfLogger::log_info(self) { "Registering adapter of type #{type} for class #{klass}" }
  @@registered_adapter_types[type] = klass
end

.remove_data_source(adapter) ⇒ Object

remove one adapter from activerdf



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

def ConnectionPool.remove_data_source(adapter)
ActiveRdfLogger.log_info(self) { "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)



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/active_rdf/federation/connection_pool.rb', line 119

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

.unregister_adapter(type) ⇒ Object

unregister adapter-type



146
147
148
149
# File 'lib/active_rdf/federation/connection_pool.rb', line 146

def ConnectionPool.unregister_adapter(type)
  ActiveRdfLogger::log_info(self) { "ConnectionPool: deregistering adapter of type #{type}" }
  @@registered_adapter_types.delete type
end

.write_adaptersObject



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

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