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, c = nil) ⇒ Object

add triple s,p,o (context is optional) to the currently selected write-adapter



10
11
12
13
14
15
16
17
# File 'lib/active_rdf/federation/federation_manager.rb', line 10

def FederationManager.add(s,p,o,c=nil)
  benchmark("SPARQL/RDF", Logger::DEBUG) do |bench_message|
    bench_message << "ADD #{s} - #{p} - #{o} : #{c}" if(bench_message)
    # 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,c)
  end
end

.clear(context = nil) ⇒ Object

Clear the store (or the given context)

Raises:

  • (RuntimeError)


38
39
40
41
42
# File 'lib/active_rdf/federation/federation_manager.rb', line 38

def FederationManager.clear(context=nil)
  # FIXME: Make sure that all adapters support clearing
  raise(RuntimeError, "Adapter #{ConnectionPool.write_adapter.class} doesn't support clear") unless(ConnectionPool.write_adapter.respond_to?(:clear))
  ConnectionPool.write_adapter.clear(context)
end

.delete(s, p, o, c = nil) ⇒ Object

delete triple s,p,o (context is optional) to the currently selected write-adapter



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_rdf/federation/federation_manager.rb', line 20

def FederationManager.delete(s,p,o,c=nil)
  benchmark("SPARQL/RDF", Logger::DEBUG) do |bench_message|
    bench_message << "DELETE #{s} - #{p} - #{o} : #{c}" if(bench_message)
    raise ActiveRdfError, "cannot write without a write-adapter" unless ConnectionPool.write_adapter
    # transform wildcard symbols to nil (for the adaptors)
    s = nil if s.is_a? Symbol
    p = nil if p.is_a? Symbol
    o = nil if o.is_a? Symbol
    ConnectionPool.write_adapter.delete(s,p,o,c)
  end
end

.delete_all(resource) ⇒ Object

delete every triples about a specified resource



33
34
35
# File 'lib/active_rdf/federation/federation_manager.rb', line 33

def FederationManager.delete_all(resource)
  delete(resource, nil, nil)
end

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

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



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/active_rdf/federation/federation_manager.rb', line 47

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

  benchmark("SPARQL/RDF", Logger::DEBUG) do |bench_message|
    # Only build the benchmark message if we need it
    bench_message << ((q.class == String) ? q : q.to_sp) if(bench_message)
    # 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)
      # FIXME: Calling get_sparql_query_results? This *only* exists on the redland adapter
      results = []
      ConnectionPool.read_adapters.each do |source|
        if (q.class != String)
          source_results = source.query(q)
        else
          source_results = source.get_sparql_query_results(q, RDFS::Resource, options[:result_format])
        end
        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.class != String) && (q.distinct?))

      # flatten results array if only one select clause
      # to prevent unnecessarily nested array [[eyal],[renaud],...]
      if (q.class != String)
        results.flatten! if (q.select_clauses.size == 1 or q.ask?)
      else
        results.flatten! if q.scan(/[?]/).length == 2
      end

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

    results
  end # End benchmark
end