Class: FederationManager

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

Overview

Manages the federation of datasources: distributes queries to right datasources and merges their results

Class Method Summary collapse

Class Method Details

.add(s, p, o) ⇒ Object

add triple s,p,o to the currently selected write-adapter

Raises:



8
9
10
11
12
# File 'lib/active_rdf/federation/federation_manager.rb', line 8

def FederationManager.add(s,p,o)
  # TODO: allow addition of full graphs
  raise ActiveRdfError, "cannot write without a write-adapter" unless ConnectionPool.write_adapter
  ConnectionPool.write_adapter.add(s,p,o)
end

.delete(s, p, o = :all) ⇒ Object

delete triple s,p,o from the currently selected write adapter (s and p are mandatory, o is optional, symbols are interpreted as wildcards)

Raises:



16
17
18
19
# File 'lib/active_rdf/federation/federation_manager.rb', line 16

def FederationManager.delete(s,p,o=:all)
  raise ActiveRdfError, "cannot write without a write-adapter" unless ConnectionPool.write_adapter
  ConnectionPool.write_adapter.delete(s,p,o)
end

.query(q, options = {:flatten => true}) ⇒ Object

executes read-only queries by distributing query over complete read-pool and aggregating the results



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_rdf/federation/federation_manager.rb', line 24

def FederationManager.query(q, options={:flatten => true})
if ConnectionPool.read_adapters.empty?
	raise ActiveRdfError, "cannot execute query without data sources" 
end

  # ask each adapter for query results
  # and yield them consequtively
  if block_given?
    ConnectionPool.read_adapters.each do |source|
      source.query(q) do |*clauses|
        yield(*clauses)
      end
    end
  else
    # build Array of results from all sources
    # TODO: write test for sebastian's select problem
    # (without distinct, should get duplicates, they
    # were filtered out when doing results.union)
    results = []
    ConnectionPool.read_adapters.each do |source|
		source_results = source.query(q)
		source_results.each do |clauses|
			results << clauses
		end
	end

    # filter the empty results
    results.reject {|ary| ary.empty? }

    # remove duplicate results from multiple
    # adapters if asked for distinct query
    # (adapters return only distinct results,
    # but they cannot check duplicates against each other)
    results.uniq! if q.distinct?

    # flatten results array if only one select clause
    # to prevent unnecessarily nested array [[eyal],[renaud],...]
    results.flatten! if q.select_clauses.size == 1 or q.ask?

    # remove array (return single value or nil) if asked to
    if options[:flatten] or q.count?
      case results.size
      when 0
        results = nil
      when 1
        results = results.first
		end
	end
  end
  
  results
end