RDF/Trine/Store/Redland.pm untested

filter: Statementstmt 0.0% 0 / 242 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 404 CC 36 SCAR 32.5
CC
36
Cov
0%
_cast_to_local
72.0
_cast_to_redland
56.0
get_statements
30.0
CRAP 25.8
1 =head1 NAME
2  
3 RDF::Trine::Store::Redland - Redland-backed RDF store for RDF::Trine
4  
5 =head1 VERSION
6  
7 This document describes RDF::Trine::Store::Redland version 1.019
8  
9 =head1 SYNOPSIS
10  
11 use RDF::Trine::Store::Redland;
12  
13 =head1 DESCRIPTION
14  
15 RDF::Trine::Store::Redland provides an RDF::Trine::Store interface to the
16 Redland RDF store.
17  
18  
19 =cut
20  
21 package RDF::Trine::Store::Redland;
22  
23 use strict;
24 use warnings;
25 no warnings 'redefine';
26 use base qw(RDF::Trine::Store);
27  
28 use Encode;
29 use Data::Dumper;
30 use RDF::Redland 1.00;
31 use Scalar::Util qw(refaddr reftype blessed);
32  
33 use RDF::Trine::Error;
34  
35 ######################################################################
36  
37 our $NIL_TAG;
38 our $VERSION;
39 BEGIN {
40 $VERSION = "1.019";
41 my $class = __PACKAGE__;
42 $RDF::Trine::Store::STORE_CLASSES{ $class } = $VERSION;
43 $NIL_TAG = 'tag:gwilliams@cpan.org,2010-01-01:RT:NIL';
44  
45 # XXX THE FOLLOWING IS TO KEEP DATA::DUMPER FROM CRAPPING OUT
46  
47 # insert these guys until we can get a fix into redland
48 my $fk = sub { 'DUMMY' };
49 my $nk = sub { undef };
50 my $f = sub { 'REDLAND PLEASE FIX YOUR API' };
51  
52 *_p_librdf_storage_s::FIRSTKEY = $fk;
53 *_p_librdf_storage_s::NEXTKEY = $nk;
54 *_p_librdf_storage_s::FETCH = $f;
55 *_p_librdf_model_s::FIRSTKEY = $fk;
56 *_p_librdf_model_s::NEXTKEY = $nk;
57 *_p_librdf_model_s::FETCH = $f;
58  
59 # too bad these aren't implemented, since they could be useful
60 }
61  
62 ######################################################################
63  
64 =head1 METHODS
65  
66 Beyond the methods documented below, this class inherits methods from the
67 L<RDF::Trine::Store> class.
68  
69 =over 4
70  
71 =item C<< new ( $store ) >>
72  
73 Returns a new storage object using the supplied RDF::Redland::Model object.
74  
75 =item C<new_with_config ( $hashref )>
76  
77 Returns a new storage object configured with a hashref with certain
78 keys as arguments.
79  
80 The C<storetype> key must be C<Redland> for this backend.
81  
82 The following keys may also be used:
83  
84 =over
85  
86 =item C<store_name>
87  
88 The name of the storage factory (currently C<hashes>, C<mysql>,
89 C<memory>, C<file>, C<postgresql>, C<sqlite>, C<tstore>, C<uri> or
90 C<virtuoso>).
91  
92 =item C<name>
93  
94 The name of the storage.
95  
96 =item C<options>
97  
98 Any other options to be passed to L<RDF::Redland::Storage> as a hashref.
99  
100 =back
101  
102 =item C<new_with_object ( $redland_model )>
103  
104 Initialize the store with a L<RDF::Redland::Model> object.
105  
106  
107 =cut
108  
109 sub new {
110 my $class = shift;
111 my $model = shift;
112 my $self = bless({
113 model => $model,
114 bulk => 0,
115 }, $class);
116 return $self;
117 }
118  
119 sub _new_with_string {
120 my $class = shift;
121 my $config = shift;
122 my ($store_name, $name, $opts) = split(/;/, $config, 3);
123 my $store = RDF::Redland::Storage->new( $store_name, $name, $opts );
124 my $model = RDF::Redland::Model->new( $store, '' );
125 return $class->new( $model );
126 }
127  
128 sub _new_with_config {
129 my $class = shift;
130 my $config = shift || { store_name => 'memory' };
131  
132 my $store = RDF::Redland::Storage->new
133 (@{$config}{qw(store_name name options)})
134 or throw RDF::Trine::Error::DatabaseError
135 -text => "Couldn't initialize Redland storage";
136 my $model = RDF::Redland::Model->new( $store, '' )
137 or throw RDF::Trine::Error::DatabaseError
138 -text => "Couldn't initialize Redland model";
139 return $class->new( $model );
140 }
141  
142 sub _new_with_object {
143 my $class = shift;
144 my $obj = shift;
145 return unless (blessed($obj) and $obj->isa('RDF::Redland::Model'));
146 return $class->new( $obj );
147 }
148  
149 sub _config_meta {
150 return {
151 required_keys => [qw(store_name name options)],
152 fields => {
153 store_name => { description => 'Redland Storage Type', type => 'string' },
154 name => { description => 'Storage Name', type => 'string' },
155 options => { description => 'Options String', type => 'string' },
156 },
157 }
158 }
159  
160 =item C<< temporary_store >>
161  
162 Returns a temporary (empty) triple store.
163  
164 =cut
165  
166 sub temporary_store {
167 my $class = shift;
168 return $class->_new_with_string( "hashes;test;new='yes',hash-type='memory',contexts='yes'" );
169 }
170  
171 =item C<< get_statements ( $subject, $predicate, $object [, $context] ) >>
172  
173 Returns a stream object of all statements matching the specified subject,
174 predicate and objects. Any of the arguments may be undef to match any value.
175  
176 =cut
177  
178 sub get_statements {
179 my $self = shift;
180 my @nodes = @_[0..3];
181
182 my $use_quad = 0;
183 if (scalar(@_) >= 4) {
184 $use_quad = 1;
185 }
186
187 my @rnodes;
188 foreach my $pos (0 .. ($use_quad ? 3 : 2)) {
189 my $n = $nodes[ $pos ];
190 if (blessed($n) and not($n->is_variable)) {
191 push(@rnodes, _cast_to_redland($n));
192 } else {
193 push(@rnodes, undef);
194 }
195 }
196
197 my $iter = ($use_quad)
198 ? $self->_get_statements_quad( @rnodes )
199 : $self->_get_statements_triple( @rnodes );
200 return $iter;
201 }
202  
203 sub _get_statements_triple {
204 my $self = shift;
205 my @rnodes = @_;
206 # warn '_get_statements_triple: ' . Dumper(\@rnodes);
207  
208 my $st = RDF::Redland::Statement->new( @rnodes[0..2] );
209 my $iter = $self->_model->find_statements( $st );
210 my %seen;
211 my $sub = sub {
212 while (1) {
213 return unless $iter;
214 #return if $iter->end;
215 my $st = $iter->current or return;
216 if ($seen{ $st->as_string }++) {
217 $iter->next;
218 next;
219 }
220 my @nodes = map { _cast_to_local($st->$_()) } qw(subject predicate object);
221 $iter->next;
222 return RDF::Trine::Statement->new( @nodes );
223 }
224 };
225 return RDF::Trine::Iterator::Graph->new( $sub );
226 }
227  
228 sub _get_statements_quad {
229 my $self = shift;
230 my @rnodes = @_;
231 # warn '_get_statements_quad: ' . Dumper(\@rnodes);
232
233 my $ctx = $rnodes[3];
234 my $ctx_local;
235 if ($ctx) {
236 # warn "-> context " . $ctx->as_string;
237 $ctx_local = _cast_to_local( $ctx );
238 }
239 my $st = RDF::Redland::Statement->new( @rnodes[0..2] );
240 my $iter = $self->_model->find_statements( $st, $ctx );
241 my $nil = RDF::Trine::Node::Nil->new();
242 my $sub = sub {
243 return unless $iter;
244 #return if $iter->end;
245 my $st = $iter->current or return;
246 my $c = $iter->context;
247 my @nodes = map { _cast_to_local($st->$_()) } qw(subject predicate object);
248 if ($ctx) {
249 push(@nodes, $ctx_local);
250 } elsif ($c) {
251 push(@nodes, _cast_to_local($c));
252 } else {
253 push(@nodes, $nil);
254 }
255 $iter->next;
256 # warn Dumper(\@nodes);
257 return RDF::Trine::Statement::Quad->new( @nodes );
258 };
259 return RDF::Trine::Iterator::Graph->new( $sub );
260 }
261  
262 =item C<< get_contexts >>
263  
264 Returns an RDF::Trine::Iterator over the RDF::Trine::Node objects comprising
265 the set of contexts of the stored quads.
266  
267 =cut
268  
269 sub get_contexts {
270 my $self = shift;
271 my @ctxs = $self->_model->contexts();
272 return RDF::Trine::Iterator->new( sub { my $n = shift(@ctxs); return _cast_to_local($n) } );
273 }
274  
275 =item C<< add_statement ( $statement [, $context] ) >>
276  
277 Adds the specified C<$statement> to the underlying model.
278  
279 =cut
280  
281 sub add_statement {
282 my $self = shift;
283 my $st = shift;
284 my $context = shift;
285  
286 my $nil = RDF::Trine::Node::Nil->new();
287 if ($st->isa( 'RDF::Trine::Statement::Quad' )) {
288 if (blessed($context)) {
289 throw RDF::Trine::Error::MethodInvocationError -text => "add_statement cannot be called with both a quad and a context";
290 }
291 } else {
292 my @nodes = $st->nodes;
293 if (blessed($context)) {
294 $st = RDF::Trine::Statement::Quad->new( @nodes[0..2], $context );
295 } else {
296 $st = RDF::Trine::Statement::Quad->new( @nodes[0..2], $nil );
297 }
298 }
299  
300 my $model = $self->_model;
301 my @nodes = $st->nodes;
302 my @rnodes = map { _cast_to_redland($_) } @nodes;
303 my $rst = RDF::Redland::Statement->new( @rnodes[0..2] );
304 my $ret = $model->add_statement( $rst, $rnodes[3] );
305  
306 # redland needs to be synced
307 $model->sync unless $self->{bulk};
308  
309 # for any code that was expecting this
310 $ret;
311 }
312  
313 =item C<< remove_statement ( $statement [, $context]) >>
314  
315 Removes the specified C<$statement> from the underlying model.
316  
317 =cut
318  
319 sub remove_statement {
320 my $self = shift;
321 my $st = shift;
322 my $context = shift;
323
324 if ($st->isa( 'RDF::Trine::Statement::Quad' )) {
325 if (blessed($context)) {
326 throw RDF::Trine::Error::MethodInvocationError -text => "remove_statement cannot be called with both a quad and a context";
327 }
328 } else {
329 my @nodes = $st->nodes;
330 if (blessed($context)) {
331 $st = RDF::Trine::Statement::Quad->new( @nodes[0..2], $context );
332 } else {
333 my $nil = RDF::Trine::Node::Nil->new();
334 $st = RDF::Trine::Statement::Quad->new( @nodes[0..2], $nil );
335 }
336 }
337  
338 my @nodes = $st->nodes;
339 my @rnodes = map { _cast_to_redland($_) } @nodes;
340 my $model = $self->_model;
341 my $ret = $model->remove_statement( @rnodes );
342  
343 # redland needs to be synced
344 $model->sync unless $self->{bulk};
345  
346 # for any code that was expecting this
347 $ret;
348 }
349  
350 =item C<< remove_statements ( $subject, $predicate, $object [, $context]) >>
351  
352 Removes the specified C<$statement> from the underlying model.
353  
354 =cut
355  
356 sub remove_statements {
357 my $self = shift;
358 my $iter = $self->get_statements(@_);
359  
360 # temporarily store the value for bulk so we don't sync over and over
361 my $bulk = $self->{bulk};
362 $self->{bulk} = 1;
363  
364 my $count = 0;
365 while (my $st = $iter->next) {
366 $self->remove_statement( $st );
367 $count++;
368 }
369  
370 # now put it back
371 $self->{bulk} = $bulk;
372 $self->_model->sync unless $bulk;
373  
374 # might as well return how many statements got deleted
375 $count;
376 }
377  
378 =item C<< count_statements ( $subject, $predicate, $object, $context ) >>
379  
380 Returns a count of all the statements matching the specified subject,
381 predicate, object, and context. Any of the arguments may be undef to match any
382 value.
383  
384 =cut
385  
386 sub count_statements {
387 my $self = shift;
388 my @nodes = @_;
389 if (scalar(@nodes) < 4) {
390 # if it isn't 4, then make damn sure it's 3
391 push @nodes, (undef) x (3 - @nodes);
392  
393 # warn "restricting count_statements to triple semantics";
394 my @rnodes = map { _cast_to_redland($_) } @nodes[0..2];
395 # force a 3-element list or you'll be sorry
396 my $st = RDF::Redland::Statement->new( @rnodes[0..2] );
397 my $iter = $self->_model->find_statements( $st );
398 my $count = 0;
399 my %seen;
400 while ($iter and my $st = $iter->current) {
401 unless ($seen{ $st->as_string }++) {
402 $count++;
403 }
404 $iter->next;
405 }
406 return $count;
407 } else {
408 my @rnodes = map { _cast_to_redland($_) } @nodes;
409 my $st = RDF::Redland::Statement->new( @rnodes[0..2] );
410 my $iter = $self->_model->find_statements( $st, $rnodes[3] );
411 my $count = 0;
412 while ($iter and my $st = $iter->current) {
413 $count++;
414 my $ctx = $iter->context;
415 $iter->next;
416 }
417 return $count;
418 }
419 }
420  
421 =item C<< size >>
422  
423 Returns the number of statements in the store.
424  
425 =cut
426  
427 sub size {
428 my $self = shift;
429 return $self->_model->size;
430 }
431  
432 =item C<< supports ( [ $feature ] ) >>
433  
434 If C<< $feature >> is specified, returns true if the feature is supported by the
435 store, false otherwise. If C<< $feature >> is not specified, returns a list of
436 supported features.
437  
438 =cut
439  
440 sub supports {
441 return;
442 }
443  
444 sub _begin_bulk_ops {
445 shift->{bulk} = 1;
446 }
447  
448 sub _end_bulk_ops {
449 my $self = shift;
450 $self->{bulk} = 0;
451 $self->_model->sync;
452 }
453  
454 sub _model {
455 my $self = shift;
456 return $self->{model};
457 }
458  
459 sub _cast_to_redland {
460 my $node = shift;
461 return unless (blessed($node));
462 if ($node->isa('RDF::Trine::Statement')) {
463 my @nodes = map { _cast_to_redland( $_ ) } $node->nodes;
464 return RDF::Redland::Statement->new( @nodes );
465 } elsif ($node->isa('RDF::Trine::Node::Resource')) {
466 return RDF::Redland::Node->new_from_uri( $node->uri_value );
467 } elsif ($node->isa('RDF::Trine::Node::Blank')) {
468 return RDF::Redland::Node->new_from_blank_identifier( $node->blank_identifier );
469 } elsif ($node->isa('RDF::Trine::Node::Literal')) {
470 my $lang = $node->literal_value_language;
471 my $dt = $node->literal_datatype;
472 my $value = $node->literal_value;
473 return RDF::Redland::Node->new_literal( "$value", $dt, $lang );
474 } elsif ($node->isa('RDF::Trine::Node::Nil')) {
475 return RDF::Redland::Node->new_from_uri( $NIL_TAG );
476 } else {
477 return;
478 }
479 }
480  
481 sub _cast_to_local {
482 my $node = shift;
483 return unless (blessed($node));
484 my $type = $node->type;
485 if ($type == $RDF::Redland::Node::Type_Resource) {
486 my $uri = $node->uri->as_string;
487 if ($uri eq $NIL_TAG) {
488 return RDF::Trine::Node::Nil->new();
489 } else {
490 return RDF::Trine::Node::Resource->new( $uri );
491 }
492 } elsif ($type == $RDF::Redland::Node::Type_Blank) {
493 return RDF::Trine::Node::Blank->new( $node->blank_identifier );
494 } elsif ($type == $RDF::Redland::Node::Type_Literal) {
495 my $lang = $node->literal_value_language;
496 my $dturi = $node->literal_datatype;
497 my $dt = ($dturi)
498 ? $dturi->as_string
499 : undef;
500 my $lv = $node->literal_value;
501 utf8::decode($lv) unless utf8::is_utf8($lv);
502 return RDF::Trine::Node::Literal->new($lv, $lang, $dt );
503 } else {
504 return;
505 }
506 }
507  
508  
509 1;
510  
511 __END__