-
Notifications
You must be signed in to change notification settings - Fork 27
/
redland.rb
331 lines (293 loc) · 12.5 KB
/
redland.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Author:: Eyal Oren
# Copyright:: (c) 2005-2006 Eyal Oren
# License:: LGPL
require 'active_rdf/federation/connection_pool'
require 'active_rdf/queryengine/query2sparql'
require 'rdf/redland'
# Adapter to Redland database
# uses SPARQL for querying
module ActiveRDF
class RedlandAdapter < ActiveRdfAdapter
ActiveRdfLogger::log_info "Loading Redland adapter", self
ConnectionPool.register_adapter(:redland,self)
# Instantiate connection to Redland database
# See ActiveRdfAdapter for available default options. The following additional options are accepted:
# :location => 'postgres' | 'mysql' | 'sqlite' | 'memory' | <filename>
# Sets the Redland store type. See the Redland docs for the additional options for individual stores.
# The 'trees' store is currently unsupported
# For Postgres, MySQL and Sqlite stores, the following additional options are available
# :database => database name
# :user => username
# :host => database server address
# :port => database server port
# :password => password
# For MySQL stores only
# :reconnect => true | false # Set automatic reconnect to database server. default false
def initialize(params = {})
super
location = params[:location]
name = params[:name] || ''
options = {}
options[:write],options[:new],options[:contexts] = [@writes,@new,@contexts].collect{|bool| bool ? 'yes' : 'no'}
# supported storage modules: mysql, postgresql, sqlite, hashes(as 'memory' or 'bdb')
# unsupported storage modules: uri, file, memory, tstore, trees
# see http://librdf.org/docs/api/redland-storage-modules.html
case location
when 'postgresql','mysql','sqlite'
store_type = location
if location == 'postgresql' or location == 'mysql'
[:host, :port, :database, :user, :password].each{|k| options[k] = params[k] if params[k]}
options[:host] ||= 'localhost'
end
options[:reconnect] = truefalse(params[:reconnect], false) if location == 'mysql'
when 'memory',nil
# use storage module hashes with hash-type 'memory' instead of non-indexing storage module memory
store_type = 'hashes'
options[:hash_type] = 'memory';
options.delete(:new) # not used with this hash-type
else
# use storage module hashes with hash-type 'bdb' instead of non-indexing storage module file
store_type = 'hashes'
options[:hash_type] = 'bdb'
if location.include?('/')
options[:dir], name = File.split(location)
else
options[:dir] = '.'
name = location || "default_redland_db"
end
end
hash_type = options.delete(:hash_type)
options = options.collect{|k,v| "#{k}='#{v}'"}.join(',')
options << "hash-type='#{hash_type}'" if hash_type # convert option key from hash_type to hash-type. :hash-type is an invalid symbol
@model = Redland::Model.new Redland::TripleStore.new(store_type, name, options)
@options = options
ActiveRDFLogger::log_inf(self) { "RedlandAdapter: initialized adapter with type=\'#{store_type}\', name=\'#{name}\' options: #{options} => #{@model.inspect}" }
rescue Redland::RedlandError => e
raise ActiveRdfError, "RedlandAdapter: could not initialise Redland database: #{e.message}\nstore_type=\'#{store_type}\', name=\'#{name}\' options: #{options}"
end
# load a file from the given location with the given syntax into the model.
# use Redland syntax strings, e.g. "ntriples" or "rdfxml", defaults to "ntriples"
# * location: location of file to load.
# * syntax: syntax of file
def load(location, syntax="ntriples")
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
parser = Redland::Parser.new(syntax, "", nil)
unless location =~ /^http/
location = "file:#{location}"
end
context = @contexts ? Redland::Uri.new(location) : nil
parser.parse_into_model(@model, location, nil, context)
save if ConnectionPool.auto_flush?
rescue Redland::RedlandError => e
$activerdflog.warn "RedlandAdapter: loading #{location} failed in Redland library: #{e}"
return false
end
# yields query results (as many as requested in select clauses) executed on data source
def execute(query, &block)
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
qs = Query2SPARQL.translate(query)
ActiveRdfLogger::log_debug(self) { "Executing SPARQL query #{qs}" }
redland_query = Redland::Query.new(qs, 'sparql')
query_results = @model.query_execute(redland_query)
# return Redland's answer without parsing if ASK query
return [[query_results.get_boolean?]] if query.ask?
ActiveRdfLogger::log_debug(self) { "Found #{query_results.size} query results" }
# verify if the query has failed
if query_results.nil?
ActiveRdfLogger::log_debug(self) { "Query has failed with nil result" }
return false
end
if not query_results.is_bindings?
ActiveRdfLogger::log_debug(self) { "Query has failed without bindings" }
return false
end
if query.count?
while not query_results.finished?
query_results.next
end
[[query_results.count]]
else
# convert the results to array
query_result_to_array(query_results, &block)
end
end
# executes query and returns results as SPARQL JSON or XML results
# requires svn version of redland-ruby bindings
# * query: ActiveRDF Query object
# * result_format: :json or :xml
def get_query_results(query, result_format=nil)
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
get_sparql_query_results(Query2SPARQL.translate(query), result_format, query.resource_class)
end
# executes sparql query and returns results as SPARQL JSON or XML results
# * query: sparql query string
# * result_format: :json or :xml
# * result_type: Is the type that is used for "resource" results
def get_sparql_query_results(qs, result_type, result_format=nil)
# author: Eric Hanson
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
# set uri for result formatting
result_uri =
case result_format
when :json
Redland::Uri.new('http://www.w3.org/2001/sw/DataAccess/json-sparql/')
when :xml
Redland::Uri.new('http://www.w3.org/TR/2004/WD-rdf-sparql-XMLres-20041221/')
end
# query redland
redland_query = Redland::Query.new(qs, 'sparql')
query_results = @model.query_execute(redland_query)
if (result_format != :array)
# get string representation in requested result_format (json or xml)
query_results.to_string(result_uri)
end
end
# add triple to datamodel
def add(s,p,o,c=nil)
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
ActiveRdfLogger::log_warn(self) { "Adapter does not support contexts" } if (!@contexts and !c.nil?)
ActiveRdfLogger::log_debug(self) { "Adding triple #{s} #{p} #{o} #{c}" }
# verify input
if s.nil? || p.nil? || o.nil?
ActiveRdfLogger::log_debug(self) { "Cannot add triple with empty subject, exiting" }
return false
end
unless s.respond_to?(:uri) && p.respond_to?(:uri)
ActiveRdfLogger::log_info(self) { "RedlandAdapter: cannot add triple where s/p are not resources, exiting" }
return false
end
quad = [s,p,o,c].collect{|e| to_redland(e)}
@model.add(*quad)
save if ConnectionPool.auto_flush?
rescue Redland::RedlandError => e
ActiveRdfLogger::log_warn(self) { "Adding triple (#{quad}) failed in Redland library: #{e}" }
return false
end
# deletes triple(s,p,o) from datastore
# nil parameters match anything: delete(nil,nil,nil) will delete all triples
def delete(s,p,o,c=nil)
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
quad = [s,p,o,c].collect{|e| to_redland(e)}
if quad.all?{|t| t.nil?}
clear
elsif quad[0..2].any?{|t| t.nil?}
@model.find(*quad).each{|stmt| @model.delete_statement(stmt,c)}
else
@model.delete(*quad)
end
save if ConnectionPool.auto_flush?
rescue Redland::RedlandError => e
ActiveRdfLogger::log_warn(self) { "RedlandAdapter: deleting triple failed in Redland library: #{e}" }
return false
end
# saves updates to the model into the redland file location
def save
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
Redland::librdf_model_sync(@model.model).nil?
end
alias flush save
# returns all triples in the datastore
def dump
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
arr = []
@model.triples{|s,p,o| arr << [s.to_s,p.to_s,o.to_s]}
arr
end
def contexts
@model.contexts
end
# returns size of datasources as number of triples
# warning: expensive method as it iterates through all statements
def size
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
# we cannot use @model.size, because redland does not allow counting of
# file-based models (@model.size raises an error if used on a file)
# instead, we just dump all triples, and count them
@model.triples.size
end
# clear all real triples of adapter
def clear
raise ActiveRdfError, "RedlandAdapter: adapter is closed" unless @enabled
@model.triples.each{|stmt| @model.delete_statement(stmt)}
end
# close adapter and remove it from the ConnectionPool
def close
if @enabled
ConnectionPool.remove_data_source(self)
flush # sync model with datastore
@model = nil # remove reference to model for removal by GC
@enabled = false
end
end
private
################ helper methods ####################
def query_result_to_array(query_results, &block)
results = []
number_bindings = query_results.binding_names.size
# redland results are set that needs to be iterated
while not query_results.finished?
# we collect the bindings in each row and add them to results
row = (0..number_bindings-1).collect do |i|
# node is the query result for one binding
node = query_results.binding_value(i)
# we determine the node type
if node.nil?
nil
elsif node.literal?
value = Redland.librdf_node_get_literal_value(node.node)
lang_uri_ref = Redland.librdf_node_get_literal_value_language(node.node)
dt_uri_ref = Redland.librdf_node_get_literal_value_datatype_uri(node.node)
if lang_uri_ref
LocalizedString.new(value,Redland::Uri.new(lang_uri_ref).to_s)
elsif dt_uri_ref
type = RDFS::Resource.new(Redland::Uri.new(dt_uri_ref).to_s)
RDFS::Literal.typed(value,type)
elsif lang_uri_ref and dt_uri_ref
raise ActiveRdfError, "cannot have both datatype and lang set"
else
RDFS::Literal.typed(value,XSD::string) # string is default type if none specified
end
elsif node.blank?
# blank nodes are not currently supported
nil
else
# other nodes are rdfs:resources
RDFS::Resource.new(node.uri.to_s)
end
end
if block_given?
yield row
else
results << row
end
# iterate through result set
query_results.next
end
results unless block_given?
end
def to_redland(obj)
case obj
when RDFS::Resource
Redland::Uri.new(obj.uri)
when RDFS::Literal
str = obj.kind_of?(Time) ? obj.xmlschema : obj.to_s
if not $activerdf_without_datatype
if obj.kind_of?(LocalizedString)
Redland::Literal.new(str, obj.lang)
else
Redland::Literal.new(str,nil,Redland::Uri.new(obj.datatype.uri))
end
else
Redland::Literal.new(str)
end
when Class
raise ActiveRdfError, "RedlandAdapter: class must inherit from RDFS::Resource" unless obj.ancestors.include?(RDFS::Resource)
Redland::Uri.new(obj.class_uri.to_s)
when Symbol, nil
nil
else
Redland::Literal.new(obj.to_s)
end
end
end
end