Class: ActiveRDF::FederationManager

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

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



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

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)


43
44
45
46
47
# File 'lib/active_rdf/federation/federation_manager.rb', line 43

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

.contextsObject



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

def FederationManager.contexts
  ConnectionPool.adapters.collect{|adapter| adapter.contexts if adapter.respond_to?(:contexts)}.flatten.compact
end

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

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



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_rdf/federation/federation_manager.rb', line 25

def FederationManager.delete(s,p=nil,o=nil,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



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

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

.execute(q, options = {:flatten => false}) ⇒ Object

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



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
# File 'lib/active_rdf/federation/federation_manager.rb', line 52

def FederationManager.execute(q, options={:flatten => false})
  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.execute(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.execute(q)
      source_results.each do |clauses|
        results << clauses
      end
    end

    # count
    return results.flatten.inject{|mem,c| mem + c} if q.count?

    # 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]
      case results.size
      when 0
        results = nil
      when 1
        results = results.first
      end
    end
  end

  results
  end # End benchmark
end