RDF/Trine/Store.pm

filter: Statementstmt 73.3% 146 / 199 Branchbran 53.3% 32 / 60 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 87.1% 27 / 31 Podpod 100% 11 / 11 totaltotal 68.2% 219 / 321 CC 41 SCAR 28.6
CC
41
Cov
67%
_get_pattern
50.4
new_with_object
20.0
class_by_name
12.0
CRAP 17.5
« RDF/Trine/Serializer/TriG.pm ↑ Summary RDF/Trine/Parser/RDFJSON.pm »
1 # RDF::Trine::Store
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Store - RDF triplestore base class
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Store version 1.019
11  
12 =head1 DESCRIPTION
13  
14 RDF::Trine::Store provides a base class and common API for implementations of
15 triple/quadstores for use with the RDF::Trine framework. In general, it should
16 be used only be people implementing new stores. For interacting with stores
17 (e.g. to read, insert, and delete triples) the RDF::Trine::Model interface
18 should be used (using the model as an intermediary between the client/user and
19 the underlying store).
20  
21 To be used by the RDF::Trine framework, store implementations must implement a
22 set of required functionality:
23  
24 =over 4
25  
26 =item * C<< new >>
27  
28 =item * C<< get_statements >>
29  
30 =item * C<< get_contexts >>
31  
32 =item * C<< add_statement >>
33  
34 =item * C<< remove_statement >>
35  
36 =item * C<< count_statements >>
37  
38 =item * C<< supports >>
39  
40 =back
41  
42 Implementations may also provide the following methods if a native
43 implementation would be more efficient than the default provided by
44 RDF::Trine::Store:
45  
46 =over 4
47  
48 =item * C<< get_pattern >>
49  
50 =item * C<< get_sparql >>
51  
52 =item * C<< remove_statements >>
53  
54 =item * C<< size >>
55  
56 =item * C<< nuke >>
57  
58 =item * C<< _begin_bulk_ops >>
59  
60 =item * C<< _end_bulk_ops >>
61  
62 =back
63  
64 =cut
65  
66 package RDF::Trine::Store;
67  
68 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 68 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 68 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
75 68 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 68 ▶use Module::Load::Conditional qw[can_load];
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 68 ▶use RDF::Trine::Store::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 68 ▶use RDF::Trine::Store::Hexastore;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 68 ▶use RDF::Trine::Store::SPARQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 68 ▶use RDF::Trine::Store::LanguagePreference;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 ######################################################################
84  
85 68 our ($VERSION, $HAVE_REDLAND, %STORE_CLASSES);
86 68 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
87 68 $VERSION = '1.019';
88 68 ▶ if ($RDF::Redland::VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RDF::Redland::VERSION) 0 68 68
89 0 $HAVE_REDLAND = 1;
90 }
91 68 can_load( modules => {
92 'RDF::Trine::Store::DBI' => undef,
93 } );
94 }
95  
96 ######################################################################
97  
98 =head1 METHODS
99  
100 =over 4
101  
102 =cut
103  
104 =item C<< new ( $data ) >>
105  
106 Returns a new RDF::Trine::Store object based on the supplied data value.
107 This constructor delegates to one of the following methods depending on the
108 value of C<< $data >>:
109  
110 * C<< new_with_string >> if C<< $data >> is not a reference
111  
112 * C<< new_with_config >> if C<< $data >> is a HASH reference
113  
114 * C<< new_with_object >> if C<< $data >> is a blessed object
115  
116 =cut
117  
118 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.7
Pod documented
119 1 my $class = shift;
120 1 my $data = shift;
121 1 ▶ if (blessed($data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($data)) { } 0 1 1
elsif (ref $data) { } 0 1 1
122 0 return $class->new_with_object($data);
123 } elsif (ref($data)) {
124 0 return $class->new_with_config($data);
125 } else {
126 1 return $class->new_with_string($data);
127 }
128 }
129  
130 =item C<< new_with_string ( $config ) >>
131  
132 Returns a new RDF::Trine::Store object based on the supplied configuration
133 string. The format of the string specifies the Store subclass to be
134 instantiated as well as any required constructor arguments. These are separated
135 by a semicolon. An example configuration string for the DBI store would be:
136  
137 DBI;mymodel;DBI:mysql:database=rdf;user;password
138  
139 The format of the constructor arguments (everything after the first ';') is
140 specific to the Store subclass.
141  
142 =cut
143  
144 5 ▶sub new_with_string {
Subroutine new_with_string: called CC=4 SCAR=15.8
Pod documented
145 5 my $proto = shift;
146 5 my $string = shift;
147 5 ▶ if (defined($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $string) { } 5 0 5
148 5 my ($subclass, $config) = split(/;/, $string, 2);
149 5 my $class = join('::', 'RDF::Trine::Store', $subclass);
150 5 ▶ if (can_load(modules => { $class => 0 }) and $class->can('_new_with_string')) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_load('modules', {$class, 0}) and $class->can('_new_with_string')) { } 5 0 5
Condition33%1/3
can_load("modules", {$class, 0}) and $class->can("_new_with_string")
!ll&&!rl&&r
005
MC/DC0%0/2
can_load("modules", {$class, 0}) and $class->can("_new_with_string")
can_load("modules", {$class, 0}) $class->can("_new_with_string")
Truth table33%1/3
A and B
A= can_load("modules", {$class, 0})B= $class->can("_new_with_string")
ABresult
0X0
100
111
151 5 return $class->_new_with_string( $config );
152 } else {
153 0 throw RDF::Trine::Error::UnimplementedError -text => "The class $class doesn't support the use of new_with_string";
154 }
155 } else {
156 0 throw RDF::Trine::Error::MethodInvocationError;
157 }
158 }
159  
160  
161 =item C<< new_with_config ( \%config ) >>
162  
163 Returns a new RDF::Trine::Store object based on the supplied
164 configuration hashref. This requires the the Store subclass to be
165 supplied with a C<storetype> key, while other keys are required by the
166 Store subclasses, please refer to each subclass for specific
167 documentation.
168  
169 An example invocation for the DBI store may be:
170  
171 my $store = RDF::Trine::Store->new_with_config({
172 storetype => 'DBI',
173 name => 'mymodel',
174 dsn => 'DBI:mysql:database=rdf',
175 username => 'dahut',
176 password => 'Str0ngPa55w0RD'
177 });
178  
179 =cut
180  
181  
182 6 ▶sub new_with_config {
Subroutine new_with_config: called CC=4 SCAR=14.0
Pod documented
183 6 my $proto = shift;
184 6 my $config = shift;
185 6 ▶ if (defined($config)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) { } 5 1 6
186 5 ▶ my $class = $config->{storeclass} || join('::', 'RDF::Trine::Store', $config->{storetype});
Condition33%1/3
$config->{'storeclass'} || join('::', 'RDF::Trine::Store', $config->{'storetype'})
l!l&&r!l&&!r
050
MC/DC0%0/2
$config->{'storeclass'} || join('::', 'RDF::Trine::Store', $config->{'storetype'})
$config->{'storeclass'} join('::', 'RDF::Trine::Store', $config->{'storetype'})
Truth table33%1/3
A || B
A= $config->{'storeclass'}B= join('::', 'RDF::Trine::Store', $config->{'storetype'})
ABresult
1X1
011
000
187 5 ▶ if ($class->can('_new_with_config')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('_new_with_config')) { } 4 1 5
188 4 return $class->_new_with_config( $config );
189 } else {
190 1 throw RDF::Trine::Error::UnimplementedError -text => "The class $class doesn't support the use of new_with_config";
191 }
192 } else {
193 1 throw RDF::Trine::Error::MethodInvocationError;
194 }
195 }
196  
197  
198 =item C<< new_with_object ( $object ) >>
199  
200 Returns a new RDF::Trine::Store object based on the supplied opaque C<< $object >>.
201 If the C<< $object >> is recognized by an available backend as being sufficient
202 to construct a store object, the store object will be returned. Otherwise undef
203 will be returned.
204  
205 =cut
206  
207 0 ▶sub new_with_object {
Subroutine new_with_object: not called CC=4 SCAR=30.0
Pod documented
208 0 my $proto = shift;
209 0 my $obj = shift;
210 0 foreach my $class (keys %STORE_CLASSES) {
211 0 ▶ if ($class->can('_new_with_object')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("_new_with_object")) 0 0 0
212 0 my $s = $class->_new_with_object( $obj );
213 0 ▶ if ($s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s) 0 0 0
214 0 return $s;
215 }
216 }
217 }
218 0 return;
219 }
220  
221 =item C<< nuke >>
222  
223 Permanently removes the store and its data.
224  
225 =cut
226  
227 0 ▶sub nuke {}
Subroutine nuke: not called CC=1 SCAR=0.0
Pod documented
228  
229 =item C<< class_by_name ( $name ) >>
230  
231 Returns the class of the storage implementation with the given name.
232 For example, C<< 'Memory' >> would return C<< 'RDF::Trine::Store::Memory' >>.
233  
234 =cut
235  
236 0 ▶sub class_by_name {
Subroutine class_by_name: not called CC=3 SCAR=24.8
Pod documented
237 0 my $proto = shift;
238 0 my $name = shift;
239 0 foreach my $class (keys %STORE_CLASSES) {
240 0 ▶ if (lc($class) =~ m/::${name}$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($class) =~ /::$name$/i) 0 0 0
241 0 return $class;
242 }
243 }
244 0 return;
245 }
246  
247 =item C<< temporary_store >>
248  
249 Returns a new temporary triplestore (using appropriate default values).
250  
251 =cut
252  
253 331 ▶sub temporary_store {
Subroutine temporary_store: called CC=1 SCAR=0.0
Pod documented
254 331 return RDF::Trine::Store::Memory->new();
255 }
256  
257 # =item C<< get_pattern ( $bgp [, $context] ) >>
258 #
259 # Returns a stream object of all bindings matching the specified graph pattern.
260 #
261 # =cut
262  
263 7 ▶sub _get_pattern {
Subroutine _get_pattern: called CC=20 SCAR=39.2
264 7 my $self = shift;
265 7 my $bgp = shift;
266 7 my $context = shift;
267 7 my @args = @_;
268 7 my %args = @args;
269
270 7 ▶ if ($bgp->isa('RDF::Trine::Statement')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bgp->isa('RDF::Trine::Statement')) { } 0 7 7
271 0 $bgp = RDF::Trine::Pattern->new($bgp);
272 } else {
273 7 $bgp = $bgp->sort_for_join_variables();
274 }
275
276 7 my %iter_args;
277 7 my @triples = $bgp->triples;
278
279 7 my ($iter);
280 7 ▶ if (1 == scalar(@triples)) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @triples) { } 7 0 7
281 7 my $t = shift(@triples);
282 7 my @nodes = $t->nodes;
283 7 my $size = scalar(@nodes);
284 7 my %vars;
285 7 my @names = qw(subject predicate object context);
286 7 foreach my $n (0 .. $#nodes) {
287 25 ▶ if ($nodes[$n]->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[$n]->isa("RDF::Trine::Node::Variable")) 9 16 25
288 9 $vars{ $names[ $n ] } = $nodes[$n]->name;
289 }
290 }
291 7 my $_iter = $self->get_statements( @nodes );
292 7 ▶ if ($_iter->finished) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_iter->finished) 1 6 7
293 1 return RDF::Trine::Iterator::Bindings->new( [], [] );
294 }
295 6 my @vars = values %vars;
296 25 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
297 25 my $row = $_iter->next;
298 25 ▶ return unless ($row);
Branch: 1 branch
BranchTrueFalseTotal
unless $row 6 19 25
299 19 my %data = map { $vars{ $_ } => $row->$_() } (keys %vars);
300 19 return RDF::Trine::VariableBindings->new( \%data );
301 6 };
302 6 $iter = RDF::Trine::Iterator::Bindings->new( $sub, \@vars );
303 } else {
304 0 my $t = pop(@triples);
305 0 my $rhs = $self->get_pattern( RDF::Trine::Pattern->new( $t ), $context, @args );
306 0 my $lhs = $self->get_pattern( RDF::Trine::Pattern->new( @triples ), $context, @args );
307 0 my @inner;
308 0 ▶ while (my $row = $rhs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $rhs->next) 0 0 0
309 0 push(@inner, $row);
310 }
311 0 my @results;
312 0 ▶ while (my $row = $lhs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $lhs->next) 0 0 0
313 0 RESULT: foreach my $irow (@inner) {
314 0 my %keysa;
315 0 my @keysa = keys %$irow;
316 0 @keysa{ @keysa } = (1) x scalar(@keysa);
317 0 my @shared = grep { exists $keysa{ $_ } } (keys %$row);
318 0 foreach my $key (@shared) {
319 0 my $val_a = $irow->{ $key };
320 0 my $val_b = $row->{ $key };
321 0 ▶ next unless (defined($val_a) and defined($val_b));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val_a and defined $val_b 0 0 0
Condition0%0/3
defined $val_a and defined $val_b
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val_a and defined $val_b
defined $val_a defined $val_b
Truth table0%0/3
A and B
A= defined $val_aB= defined $val_b
ABresult
0X0
100
111
322 0 my $equal = $val_a->equal( $val_b );
323 0 ▶ unless ($equal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($equal) 0 0 0
324 0 next RESULT;
325 }
326 }
327
328 0 my $jrow = { (map { $_ => $irow->{$_} } grep { defined($irow->{$_}) } keys %$irow), (map { $_ => $row->{$_} } grep { defined($row->{$_}) } keys %$row) };
329 0 push(@results, RDF::Trine::VariableBindings->new($jrow));
330 }
331 }
332 0 $iter = RDF::Trine::Iterator::Bindings->new( \@results, [ $bgp->referenced_variables ] );
333 }
334
335 6 ▶ if (my $o = $args{ 'orderby' }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $args{'orderby'}) { } 2 4 6
336 2 ▶ unless (reftype($o) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($o) eq "ARRAY") 0 2 2
337 0 throw RDF::Trine::Error::MethodInvocationError -text => "The orderby argument to get_pattern must be an ARRAY reference";
338 }
339
340 2 my @order;
341 my %order;
342 2 my @o = @$o;
343 2 my @sorted_by;
344 2 my %vars = map { $_ => 1 } $bgp->referenced_variables;
345 2 ▶ if (scalar(@o) % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@o) % 2 != 0) 0 2 2
346 0 throw RDF::Trine::Error::MethodInvocationError -text => "The orderby argument ARRAY to get_pattern must contain an even number of elements";
347 }
348 2 ▶ while (@o) {
Branch: 1 branch
BranchTrueFalseTotal
if (@o) 2 2 4
349 2 my ($k,$dir) = splice(@o, 0, 2, ());
350 2 ▶ next unless ($vars{ $k });
Branch: 1 branch
BranchTrueFalseTotal
unless $vars{$k} 0 2 2
351 2 ▶ unless ($dir =~ m/^ASC|DESC$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir =~ /^ASC|DESC$/i) 0 2 2
352 0 throw RDF::Trine::Error::MethodInvocationError -text => "The sort direction for key $k must be either 'ASC' or 'DESC' in get_pattern call";
353 }
354 2 ▶ my $asc = ($dir eq 'ASC') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'ASC' ? : 2 0 2
355 2 push(@order, $k);
356 2 $order{ $k } = $asc;
357 2 push(@sorted_by, $k, $dir);
358 }
359
360 2 my @results = $iter->get_all;
361 2 @results = _sort_bindings( \@results, \@order, \%order );
362 2 $iter_args{ sorted_by } = \@sorted_by;
363 2 return RDF::Trine::Iterator::Bindings->new( \@results, [ $bgp->referenced_variables ], %iter_args );
364 } else {
365 4 return $iter;
366 }
367 }
368  
369 2 ▶sub _sort_bindings {
Subroutine _sort_bindings: called CC=1 SCAR=0.0
370 2 my $res = shift;
371 2 my $o = shift;
372 2 my $dir = shift;
373 2 my @sorted = map { $_->[0] } sort { _sort_mapped_data($a,$b,$o,$dir) } map { _map_sort_data( $_, $o ) } @$res;
374 2 return @sorted;
375 }
376  
377 8 ▶sub _sort_mapped_data {
Subroutine _sort_mapped_data: called CC=6 SCAR=18.4
378 8 my $a = shift;
379 8 my $b = shift;
380 8 my $o = shift;
381 8 my $dir = shift;
382 8 foreach my $i (1 .. $#{ $a }) {
383 8 my $av = $a->[ $i ];
384 8 my $bv = $b->[ $i ];
385 8 my $key = $o->[ $i-1 ];
386 8 ▶ next unless (defined($av) or defined($bv));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $av or defined $bv 0 8 8
Condition33%1/3
defined $av or defined $bv
l!l&&r!l&&!r
800
MC/DC0%0/2
defined $av or defined $bv
defined $av defined $bv
Truth table33%1/3
A or B
A= defined $avB= defined $bv
ABresult
1X1
011
000
387 8 my $cmp = RDF::Trine::Node::compare( $av, $bv );
388 8 ▶ unless ($dir->{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir->{$key}) 0 8 8
389 0 $cmp *= -1;
390 }
391 8 ▶ return $cmp if ($cmp);
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 2 6 8
392 }
393 6 return 0;
394 }
395  
396 6 ▶sub _map_sort_data {
Subroutine _map_sort_data: called CC=1 SCAR=0.0
397 6 my $res = shift;
398 6 my $o = shift;
399 6 my @data = ($res, map { $res->{ $_ } } @$o);
400 6 return \@data;
401 }
402  
403 =item C<< get_statements ($subject, $predicate, $object [, $context] ) >>
404  
405 Returns a stream object of all statements matching the specified subject,
406 predicate and objects. Any of the arguments may be undef to match any value.
407  
408 =cut
409  
410 sub get_statements;
411  
412 =item C<< get_contexts >>
413  
414 Returns an RDF::Trine::Iterator over the RDF::Trine::Node objects comprising
415 the set of contexts of the stored quads.
416  
417 =cut
418  
419 sub get_contexts;
420  
421 =item C<< add_statement ( $statement [, $context] ) >>
422  
423 Adds the specified C<$statement> to the underlying model.
424  
425 =cut
426  
427 sub add_statement;
428  
429 =item C<< remove_statement ( $statement [, $context]) >>
430  
431 Removes the specified C<$statement> from the underlying model.
432  
433 =cut
434  
435 sub remove_statement;
436  
437 =item C<< remove_statements ( $subject, $predicate, $object [, $context]) >>
438  
439 Removes the specified C<$statement> from the underlying model.
440  
441 =cut
442  
443 3 ▶sub remove_statements { # Fallback implementation
Subroutine remove_statements: called CC=2 SCAR=6.9
Pod documented
444 3 my $self = shift;
445 3 my $iterator = $self->get_statements(@_);
446 3 ▶ while (my $st = $iterator->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iterator->next) 4 3 7
447 4 $self->remove_statement($st);
448 }
449 }
450  
451 =item C<< count_statements ($subject, $predicate, $object) >>
452  
453 Returns a count of all the statements matching the specified subject,
454 predicate and objects. Any of the arguments may be undef to match any value.
455  
456 =cut
457  
458 sub count_statements;
459  
460 =item C<< size >>
461  
462 Returns the number of statements in the store.
463  
464 =cut
465  
466 95 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
467 95 my $self = shift;
468 95 return $self->count_statements( undef, undef, undef, undef );
469 }
470  
471 =item C<< etag >>
472  
473 If the store has the capability and knowledge to support caching, returns a
474 persistent token that will remain consistent as long as the store's data doesn't
475 change. This token is acceptable for use as an HTTP ETag.
476  
477 =cut
478  
479 4 ▶sub etag {
Subroutine etag: called CC=1 SCAR=0.0
Pod documented
480 4 return;
481 }
482  
483 =item C<< supports ( [ $feature ] ) >>
484  
485 If C<< $feature >> is specified, returns true if the feature is supported by the
486 store, false otherwise. If C<< $feature >> is not specified, returns a list of
487 supported features.
488  
489 =cut
490  
491 0 ▶sub supports {
Subroutine supports: not called CC=1 SCAR=6.9
Pod documented
492 0 return;
493 }
494  
495 776 ▶sub _begin_bulk_ops {}
Subroutine _begin_bulk_ops: called CC=1 SCAR=0.0
496 7079 ▶sub _end_bulk_ops {}
Subroutine _end_bulk_ops: called CC=1 SCAR=0.0
497  
498 68 1;
499  
500 __END__
« RDF/Trine/Serializer/TriG.pm ↑ Summary RDF/Trine/Parser/RDFJSON.pm »