RDF/Trine/Store/Memory.pm

filter: Statementstmt 98.0% 350 / 357 Branchbran 95.1% 99 / 104 Conditioncond 88.2% 15 / 17 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 94.1% 32 / 34 Podpod 100% 11 / 11 totaltotal 96.6% 516 / 534 CC 74 SCAR 19.8
CC
74
Cov
97%
count_statements
16.0
remove_statement
10.0
add_statement
9.0
CRAP 7.3
« RDF/Trine/Parser/RDFPatch.pm ↑ Summary RDF/Trine/Store/DBI/SQLite.pm »
1 =head1 NAME
2  
3 RDF::Trine::Store::Memory - Simple in-memory RDF store
4  
5 =head1 VERSION
6  
7 This document describes RDF::Trine::Store::Memory version 1.019
8  
9 =head1 SYNOPSIS
10  
11 use RDF::Trine::Store::Memory;
12  
13 =head1 DESCRIPTION
14  
15 RDF::Trine::Store::Memory provides an in-memory triple-store.
16  
17 =cut
18  
19 package RDF::Trine::Store::Memory;
20  
21 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 68 ▶use base qw(RDF::Trine::Store);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 68 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 68 ▶use Set::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 68 ▶use Digest::SHA qw(sha1);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 68 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 68 ▶use Scalar::Util qw(refaddr reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 68 ▶use RDF::Trine::Statement::Quad;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 68 ▶use RDF::Trine qw(iri);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 68 ▶use RDF::Trine::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 ######################################################################
38  
39 68 my @pos_names;
40 68 our $VERSION;
41 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
42 68 $VERSION = "1.019";
43 68 my $class = __PACKAGE__;
44 68 $RDF::Trine::Store::STORE_CLASSES{ $class } = $VERSION;
45 68 @pos_names = qw(subject predicate object context);
46 }
47  
48 ######################################################################
49  
50 =head1 METHODS
51  
52 Beyond the methods documented below, this class inherits methods from the
53 L<RDF::Trine::Store> class.
54  
55 =over 4
56  
57 =item C<< new () >>
58  
59 Returns a new memory-backed storage object.
60  
61 =item C<new_with_config ( $hashref )>
62  
63 Returns a new storage object configured with a hashref with certain
64 keys as arguments.
65  
66 The C<storetype> key must be C<Memory> for this backend.
67  
68 This module also supports initializing the store from a file or URL,
69 in which case, a C<sources> key may be used. This holds an arrayref of
70 hashrefs. To load a file, you may give the file name with a C<file>
71 key in the hashref, and to load a URL, use C<url>. See example
72 below. Furthermore, the following keys may be used:
73  
74 =over
75  
76 =item C<syntax>
77  
78 The syntax of the parsed file or URL.
79  
80 =item C<base_uri>
81  
82 The base URI to be used for a parsed file.
83  
84 =item C<graph> NOT IMPLEMENTED
85  
86 Use this URI as a graph name for the contents of the file or URL.
87  
88 =back
89  
90 The following example initializes a Memory store based on a local file and a remote URL:
91  
92 my $store = RDF::Trine::Store->new_with_config(
93 {
94 storetype => 'Memory',
95 sources => [
96 {
97 file => 'test-23.ttl',
98 syntax => 'turtle',
99 },
100 {
101 url => 'http://www.kjetil.kjernsmo.net/foaf',
102 syntax => 'rdfxml',
103 graph => 'http://example.org/graph/remote-users'
104 }
105 ]
106 });
107  
108 =cut
109  
110 931 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
111 931 my $class = shift;
112 931 my $self = bless({
113 size => 0,
114 statements => [],
115 subject => {},
116 predicate => {},
117 object => {},
118 context => {},
119 ctx_nodes => {},
120 hash => Digest::SHA->new,
121 statement_hashes => {},
122 }, $class);
123  
124 931 return $self;
125 }
126  
127 3 ▶sub _new_with_string {
Subroutine _new_with_string: called CC=3 SCAR=11.2
128 3 my $class = shift;
129 3 ▶ my $config = shift || '';
Condition50%1/2
shift() || ''
l!l
03
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
130 3 my @uris = split(';', $config);
131 3 my $self = $class->new();
132
133 3 my $model = RDF::Trine::Model->new( $self );
134 3 foreach my $u (@uris) {
135 0 RDF::Trine::Parser->parse_url_into_model( $u, $model );
136 }
137
138 3 return $self;
139 }
140  
141 0 ▶sub _config_meta {
Subroutine _config_meta: not called CC=1 SCAR=6.9
142 return {
143 0 required_keys => []
144 }
145 }
146  
147 3 ▶sub _new_with_config {
Subroutine _new_with_config: called CC=7 SCAR=20.8
148 3 my $class = shift;
149 3 my $config = shift;
150 3 my @sources = @{$config->{sources}};
151 3 my $self = $class->new();
152 3 foreach my $source (@sources) {
153 4 my %args;
154 4 ▶ if (my $g = $source->{graph}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $source->{"graph"}) 1 3 4
155 1 ▶ $args{context} = (blessed($g) ? $g : iri($g));
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($g) ? : 0 1 1
156 }
157 4 ▶ if ($source->{url}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source->{'url'}) { } 0 4 4
elsif ($source->{'file'}) { } 4 0 4
158 0 my $parser = RDF::Trine::Parser->new($source->{syntax});
159 0 my $model = RDF::Trine::Model->new( $self );
160 0 $parser->parse_url_into_model( $source->{url}, $model, %args );
161
162 } elsif ($source->{file}) {
163 open(my $fh, "<:encoding(UTF-8)", $source->{file})
164 4 ▶ || throw RDF::Trine::Error -text => "Couldn't open file $source->{file}";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $source->{"file"} or "RDF::Trine::Error"->throw("-text", qq[Couldn't open file $source->{"file"}]) 4 0 4
165 4 my $parser = RDF::Trine::Parser->new($source->{syntax});
166 4 my $model = RDF::Trine::Model->new( $self );
167 4 $parser->parse_file_into_model( $source->{base_uri}, $source->{file}, $model, %args );
168 } else {
169 0 throw RDF::Trine::Error::MethodInvocationError -text => "$class needs a url or file argument";
170 }
171 }
172 2 return $self;
173 }
174  
175  
176  
177 =item C<< temporary_store >>
178  
179 Returns a temporary (empty) triple store.
180  
181 =cut
182  
183 3 ▶sub temporary_store {
Subroutine temporary_store: called CC=1 SCAR=0.0
Pod documented
184 3 my $class = shift;
185 3 return $class->new();
186 }
187  
188 =item C<< get_statements ( $subject, $predicate, $object [, $context] ) >>
189  
190 Returns a stream object of all statements matching the specified subject,
191 predicate and objects. Any of the arguments may be undef to match any value.
192  
193 =cut
194  
195 3197 ▶sub get_statements {
Subroutine get_statements: called CC=8 SCAR=20.8
Pod documented
196 3197 my $self = shift;
197 3197 my @nodes = @_[0..3];
198 3197 my $bound = 0;
199 3197 my %bound;
200
201 3197 my $use_quad = 0;
202 3197 ▶ if (scalar(@_) >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 4) 2195 1002 3197
203 2195 $use_quad = 1;
204 2195 my $g = $nodes[3];
205 2195 ▶ if (blessed($g) and not($g->is_variable)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($g) and not $g->is_variable) 65 2130 2195
Condition100%3/3
builtin::blessed($g) and not $g->is_variable
!ll&&!rl&&r
134178965
MC/DC100%2/2
builtin::blessed($g) and not $g->is_variable
builtin::blessed($g) not $g->is_variable
Truth table100%3/3
A and not(B)
A= builtin::blessed($g)B= not $g->is_variable
ABresult
0X0
100
111
206 65 $bound++;
207 65 $bound{ 3 } = $g;
208 }
209 }
210
211 3197 foreach my $pos (0 .. 2) {
212 9591 my $n = $nodes[ $pos ];
213 9591 ▶ if (blessed($n) and not($n->is_variable)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($n) and not $n->is_variable) 4248 5343 9591
Condition100%3/3
builtin::blessed($n) and not $n->is_variable
!ll&&!rl&&r
265626874248
MC/DC100%2/2
builtin::blessed($n) and not $n->is_variable
builtin::blessed($n) not $n->is_variable
Truth table100%3/3
A and not(B)
A= builtin::blessed($n)B= not $n->is_variable
ABresult
0X0
100
111
214 4248 $bound++;
215 4248 $bound{ $pos } = $n;
216 }
217 }
218
219 3197 ▶ my $iter = ($use_quad)
Branch: 1 branch
BranchTrueFalseTotal
$use_quad ? : 2195 1002 3197
220 ? $self->_get_statements_quad( $bound, %bound )
221 : $self->_get_statements_triple( $bound, %bound );
222 3197 return $iter;
223 }
224  
225 1002 ▶sub _get_statements_triple {
Subroutine _get_statements_triple: called CC=6 SCAR=17.9
226 1002 my $self = shift;
227 1002 my $bound = shift;
228 1002 my %bound = @_;
229
230 1002 my $match_set = Set::Scalar->new( 0 .. $#{ $self->{statements} } );
231 1002 ▶ if ($bound) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bound) 889 113 1002
232 # warn "getting $bound-bound statements";
233 889 my @pos = sort { $a <=> $b } keys %bound;
234 889 my @names = @pos_names[ @pos ];
235 # warn "\tbound nodes are: " . join(', ', @names) . "\n";
236
237 889 my @sets;
238 889 foreach my $i (0 .. $#pos) {
239 1508 my $pos = $pos[ $i ];
240 1508 my $node = $bound{ $pos };
241 1508 my $string = $node->as_string;
242 # warn $node . " has string: '" . $string . "'\n";
243 1508 my $hash = $self->{$names[$i]};
244 1508 my $set = $hash->{ $string };
245 1508 push(@sets, $set);
246 }
247
248 889 foreach my $s (@sets) {
249 1470 ▶ unless (blessed($s)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($s)) 275 1195 1470
250 275 return RDF::Trine::Iterator::Graph->new();
251 }
252 }
253
254 # warn "initial set: $i\n";
255 614 ▶ while (@sets) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sets) 1021 614 1635
256 1021 my $s = shift(@sets);
257 # warn "new set: $s\n";
258 1021 $match_set = $match_set->intersection($s);
259 # warn "intersection: $i";
260 }
261 }
262
263 727 my $open = 1;
264 727 my %seen;
265
266 727 my @members = sort { $a <=> $b } $match_set->members;
267 1905 ▶ my $sub = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
268 1905 while (1) {
269 2662 my $e = shift(@members);
270 2662 ▶ unless (defined($e)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $e) 714 1948 2662
271 714 $open = 0;
272 714 return;
273 }
274
275 1948 my $st = $self->{statements}[ $e++ ];
276 1948 ▶ unless (blessed($st)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($st)) 333 1615 1948
277 333 next;
278 }
279 1615 my @nodes = $st->nodes;
280 1615 my $triple = RDF::Trine::Statement->new( @nodes[0..2] );
281 1615 ▶ if ($seen{ $triple->as_string }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$triple->as_string}++) 424 1191 1615
282 # warn "already seen " . $triple->as_string . "\n" if ($::debug);
283 424 next;
284 }
285 # warn "returning statement from $bound-bound iterator: " . $triple->as_string . "\n";
286 1191 return $triple;
287 }
288 727 };
289 727 return RDF::Trine::Iterator::Graph->new( $sub );
290 }
291  
292 2195 ▶sub _get_statements_quad {
Subroutine _get_statements_quad: called CC=8 SCAR=20.8
293 2195 my $self = shift;
294 2195 my $bound = shift;
295 2195 my %bound = @_;
296 2195 ▶ if ($bound == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bound == 0) 792 1403 2195
297 # warn "getting all statements";
298 # warn Dumper($self);
299 792 my $i = 0;
300 2373 ▶ my $sub = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
301 # warn "quad iter called with i=$i, last=" . $#{ $self->{statements} };
302 2373 ▶ return unless ($i <= $#{ $self->{statements} });
Branch: 1 branch
BranchTrueFalseTotal
unless $i <= $#{$$self{"statements"};} 792 1581 2373
303 1581 my $st = $self->{statements}[ $i ];
304 # warn $st;
305 1581 ▶ while (not(blessed($st)) and ($i <= $#{ $self->{statements} })) {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($st) and $i <= $#{$$self{"statements"};}) 16 1581 1597
Condition66%2/3
not builtin::blessed($st) and $i <= $#{$$self{"statements"};}
!ll&&!rl&&r
1581016
MC/DC50%1/2
not builtin::blessed($st) and $i <= $#{$$self{"statements"};}
not builtin::blessed($st) $i <= $#{$$self{"statements"};}
Truth table66%2/3
not(A) and B
A= not builtin::blessed($st)B= $i <= $#{$$self{"statements"};}
ABresult
0X0
100
111
306 16 $st = $self->{statements}[ ++$i ];
307 # warn "null st. next: $st";
308 }
309 1581 $i++;
310 1581 return $st;
311 792 };
312 # warn "returning all quads sub $sub";
313 792 return RDF::Trine::Iterator::Graph->new( $sub );
314 }
315
316 1403 my $match_set;
317 1403 ▶ if ($bound == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bound == 1) { } 50 1353 1403
318 # warn "getting 1-bound statements";
319 50 my ($pos) = keys %bound;
320 50 my $name = $pos_names[ $pos ];
321 # warn "\tbound node is $name\n";
322 50 my $node = $bound{ $pos };
323 50 my $string = $node->as_string;
324 50 $match_set = $self->{$name}{ $string };
325 # warn "\tmatching statements: $match_set\n";
326 50 ▶ unless (blessed($match_set)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($match_set)) 1 49 50
327 1 return RDF::Trine::Iterator::Graph->new();
328 }
329 } else {
330 # warn "getting $bound-bound statements";
331 1353 my @pos = keys %bound;
332 1353 my @names = @pos_names[ @pos ];
333 # warn "\tbound nodes are: " . join(', ', @names) . "\n";
334
335 1353 my @sets;
336 1353 foreach my $i (0 .. $#pos) {
337 2755 my $pos = $pos[ $i ];
338 2755 my $node = $bound{ $pos };
339 2755 my $string = $node->as_string;
340 # warn $node . " has string: '" . $string . "'\n";
341 2755 my $hash = $self->{$names[$i]};
342 2755 my $set = $hash->{ $string };
343 2755 push(@sets, $set);
344 }
345
346 1353 foreach my $s (@sets) {
347 2751 ▶ unless (blessed($s)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($s)) 2 2749 2751
348 2 return RDF::Trine::Iterator::Graph->new();
349 }
350 }
351 1351 my $i = shift(@sets);
352 # warn "initial set: $i\n";
353 1351 ▶ while (@sets) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sets) 1398 1351 2749
354 1398 my $s = shift(@sets);
355 # warn "new set: $s\n";
356 1398 $i = $i->intersection($s);
357 # warn "intersection: $i";
358 }
359 1351 $match_set = $i;
360 # warn "\tmatching statements: $match_set\n";
361 }
362
363 1400 my $open = 1;
364 1400 my @e = $match_set->elements;
365 3417 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
366 3417 ▶ unless (scalar(@e)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @e) 1395 2022 3417
367 1395 $open = 0;
368 1395 return;
369 }
370 2022 my $e = shift(@e);
371 # warn "quad iterator returning statement $e";
372
373 2022 my $st = $self->{statements}[ $e ];
374 # warn "returning statement from $bound-bound iterator: " . $st->as_string . "\n";
375 2022 return $st;
376 1400 };
377 1400 return RDF::Trine::Iterator::Graph->new( $sub );
378 }
379  
380 =item C<< get_contexts >>
381  
382 Returns an RDF::Trine::Iterator over the RDF::Trine::Node objects comprising
383 the set of contexts of the stored quads.
384  
385 =cut
386  
387 7 ▶sub get_contexts {
Subroutine get_contexts: called CC=1 SCAR=0.0
Pod documented
388 7 my $self = shift;
389 7 my @ctx = grep { not($_->isa('RDF::Trine::Node::Nil')) } values %{ $self->{ ctx_nodes } };
390 7 return RDF::Trine::Iterator->new( \@ctx );
391 }
392  
393 =item C<< add_statement ( $statement [, $context] ) >>
394  
395 Adds the specified C<$statement> to the underlying model.
396  
397 =cut
398  
399 4188 ▶sub add_statement {
Subroutine add_statement: called CC=9 SCAR=22.0
Pod documented
400 4188 my $self = shift;
401 4188 my $st = shift;
402 4188 my $context = shift;
403
404 4188 ▶ if ($st->isa( 'RDF::Trine::Statement::Quad' )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->isa('RDF::Trine::Statement::Quad')) { } 142 4046 4188
405 142 ▶ if (blessed($context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($context)) 1 141 142
406 1 throw RDF::Trine::Error::MethodInvocationError -text => "add_statement cannot be called with both a quad and a context";
407 }
408 } else {
409 4046 my @nodes = $st->nodes;
410 4046 ▶ if (blessed($context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($context)) { } 13 4033 4046
411 13 $st = RDF::Trine::Statement::Quad->new( @nodes[0..2], $context );
412 } else {
413 4033 my $nil = RDF::Trine::Node::Nil->new();
414 4033 $st = RDF::Trine::Statement::Quad->new( @nodes[0..2], $nil );
415 }
416 }
417
418 4187 my $string = encode_utf8($st->as_string);
419 4187 my $st_hash = sha1($string);
420 4187 ▶ if ($self->{statement_hashes}{$st_hash}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"statement_hashes"}{$st_hash}++) 11 4176 4187
421 11 my $count = $self->count_statements( $st->nodes );
422 # warn "store already has statement " . $st->as_string;
423 11 ▶ return if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 7 4 11
424 }
425
426 4180 $self->{size}++;
427 4180 my $id = scalar(@{ $self->{ statements } });
428 4180 $self->{hash}->add('+' . $string);
429 4180 push( @{ $self->{ statements } }, $st );
430 4180 foreach my $pos (0 .. $#pos_names) {
431 16720 my $name = $pos_names[ $pos ];
432 16720 my $node = $st->$name();
433 16720 my $string = $node->as_string;
434 16720 my $set = $self->{$name}{ $string };
435 16720 ▶ unless (blessed($set)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($set)) 7684 9036 16720
436 7684 $set = Set::Scalar->new();
437 7684 $self->{$name}{ $string } = $set;
438 }
439 16720 $set->insert( $id );
440 }
441
442 4180 my $ctx = $st->context;
443 4180 my $str = $ctx->as_string;
444 4180 ▶ unless (exists $self->{ ctx_nodes }{ $str }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"ctx_nodes"}{$str}) 954 3226 4180
445 954 $self->{ ctx_nodes }{ $str } = $ctx;
446 }
447 4180 return;
448 }
449  
450 =item C<< remove_statement ( $statement [, $context]) >>
451  
452 Removes the specified C<$statement> from the underlying model.
453  
454 =cut
455  
456 164 ▶sub remove_statement {
Subroutine remove_statement: called CC=10 SCAR=23.0
Pod documented
457 164 my $self = shift;
458 164 my $st = shift;
459 164 my $context = shift;
460
461 164 ▶ if ($st->isa( 'RDF::Trine::Statement::Quad' )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->isa('RDF::Trine::Statement::Quad')) { } 109 55 164
462 109 ▶ if (blessed($context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($context)) 1 108 109
463 1 throw RDF::Trine::Error::MethodInvocationError -text => "remove_statement cannot be called with both a quad and a context";
464 }
465 } else {
466 55 my @nodes = $st->nodes;
467 55 ▶ if (blessed($context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($context)) { } 4 51 55
468 4 $st = RDF::Trine::Statement::Quad->new( @nodes[0..2], $context );
469 } else {
470 51 my $nil = RDF::Trine::Node::Nil->new();
471 51 $st = RDF::Trine::Statement::Quad->new( @nodes[0..2], $nil );
472 }
473 }
474  
475 163 my $string = encode_utf8($st->as_string);
476 163 my $st_hash = sha1($string);
477 163 ▶ unless (exists $self->{statement_hashes}{$st_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"statement_hashes"}{$st_hash}) 11 152 163
478 11 return;
479 }
480
481 152 ▶ if (0 == --$self->{statement_hashes}{$st_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == --$self->{"statement_hashes"}{$st_hash}) 144 8 152
482 144 delete $self->{statement_hashes}{$st_hash};
483 }
484
485 152 my @nodes = $st->nodes;
486 152 my $count = $self->count_statements( @nodes[ 0..3 ] );
487 # warn "remove_statement: count of statement is $count";
488 152 ▶ if ($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 152 0 152
489 152 $self->{size}--;
490 152 my $id = $self->_statement_id( $st->nodes );
491 # warn "removing statement $id: " . $st->as_string . "\n";
492 152 $self->{hash}->add('-' . $string);
493 152 $self->{statements}[ $id ] = undef;
494 152 foreach my $pos (0 .. 3) {
495 608 my $name = $pos_names[ $pos ];
496 608 my $node = $st->$name();
497 608 my $str = $node->as_string;
498 608 my $set = $self->{$name}{ $str };
499 608 $set->delete( $id );
500 608 ▶ if ($set->size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set->size == 0) 92 516 608
501 92 ▶ if ($pos == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos == 3) 18 74 92
502 18 delete $self->{ ctx_nodes }{ $str };
503 }
504 92 delete $self->{$name}{ $str };
505 }
506 }
507 }
508 152 return;
509 }
510  
511 =item C<< remove_statements ( $subject, $predicate, $object [, $context]) >>
512  
513 Removes the specified C<$statement> from the underlying model.
514  
515 =cut
516  
517 8 ▶sub remove_statements {
Subroutine remove_statements: called CC=2 SCAR=6.9
Pod documented
518 8 my $self = shift;
519 8 my $subj = shift;
520 8 my $pred = shift;
521 8 my $obj = shift;
522 8 my $context = shift;
523 8 my $iter = $self->get_statements( $subj, $pred, $obj, $context );
524 8 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 19 8 27
525 19 $self->remove_statement( $st );
526 }
527 }
528  
529 =item C<< count_statements ( $subject, $predicate, $object, $context ) >>
530  
531 Returns a count of all the statements matching the specified subject,
532 predicate, object, and context. Any of the arguments may be undef to match any
533 value.
534  
535 =cut
536  
537 906 ▶sub count_statements {
Subroutine count_statements: called CC=16 SCAR=27.7
Pod documented
538 906 my $self = shift;
539 906 my @nodes = @_[0..3];
540 906 my $bound = 0;
541 906 my %bound;
542
543 906 my $use_quad = 0;
544 906 ▶ if (scalar(@_) >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 4) 261 645 906
545 261 $use_quad = 1;
546 # warn "count statements with quad" if ($::debug);
547 261 my $g = $nodes[3];
548 261 ▶ if (blessed($g) and not($g->is_variable)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($g) and not $g->is_variable) 197 64 261
Condition100%3/3
builtin::blessed($g) and not $g->is_variable
!ll&&!rl&&r
631197
MC/DC100%2/2
builtin::blessed($g) and not $g->is_variable
builtin::blessed($g) not $g->is_variable
Truth table100%3/3
A and not(B)
A= builtin::blessed($g)B= not $g->is_variable
ABresult
0X0
100
111
549 197 $bound++;
550 197 $bound{ 3 } = $g;
551 }
552 }
553
554 906 foreach my $pos (0 .. 2) {
555 2718 my $n = $nodes[ $pos ];
556 # unless (blessed($n)) {
557 # $n = RDF::Trine::Node::Nil->new();
558 # $nodes[ $pos ] = $n;
559 # }
560
561 2718 ▶ if (blessed($n) and not($n->is_variable)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($n) and not $n->is_variable) 1628 1090 2718
Condition100%3/3
builtin::blessed($n) and not $n->is_variable
!ll&&!rl&&r
108461628
MC/DC100%2/2
builtin::blessed($n) and not $n->is_variable
builtin::blessed($n) not $n->is_variable
Truth table100%3/3
A and not(B)
A= builtin::blessed($n)B= not $n->is_variable
ABresult
0X0
100
111
562 1628 $bound++;
563 1628 $bound{ $pos } = $n;
564 }
565 }
566
567 # warn "use quad: $use_quad\n" if ($::debug);
568 # warn "bound: $bound\n" if ($::debug);
569 906 ▶ if ($use_quad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_quad) { } 261 645 906
570 261 ▶ if ($bound == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bound == 0) { } 35 226 261
elsif ($bound == 1) { } 34 192 226
571 # warn "counting all statements";
572 35 return $self->size;
573 } elsif ($bound == 1) {
574 34 my ($pos) = keys %bound;
575 34 my $name = $pos_names[ $pos ];
576 34 my $set = $self->{$name}{ $bound{ $pos }->as_string };
577 # warn Dumper($set) if ($::debug);
578 34 ▶ unless (blessed($set)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($set)) 4 30 34
579 4 return 0;
580 }
581 30 return $set->size;
582 } else {
583 192 my @pos = keys %bound;
584 192 my @names = @pos_names[ @pos ];
585 192 my @sets;
586 192 foreach my $i (0 .. $#names) {
587 711 my $pos = $pos[ $i ];
588 711 my $setname = $names[ $i ];
589 711 my $data = $self->{ $setname };
590
591 711 my $node = $bound{ $pos };
592 711 my $str = $node->as_string;
593 711 my $set = $data->{ $str };
594 711 push( @sets, $set );
595 }
596 192 foreach my $s (@sets) {
597 # warn "set: " . Dumper($s) if ($::debug);
598 700 ▶ unless (blessed($s)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($s)) 5 695 700
599 # warn "*** returning zero" if ($::debug);
600 5 return 0;
601 }
602 }
603 187 my $i = shift(@sets);
604 187 ▶ while (@sets) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sets) 506 187 693
605 506 my $s = shift(@sets);
606 506 $i = $i->intersection($s);
607 }
608 187 return $i->size;
609 }
610 } else {
611 # use_quad is false here
612 # we're counting distinct (s,p,o) triples from the quadstore
613 645 my $count = 0;
614 645 my $iter = $self->get_statements( @nodes[ 0..2 ] );
615 645 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 525 645 1170
616 # warn $st->as_string if ($::debug);
617 525 $count++;
618 }
619 645 return $count;
620 }
621 }
622  
623 =item C<< etag >>
624  
625 If the store has the capability and knowledge to support caching, returns a
626 persistent token that will remain consistent as long as the store's data doesn't
627 change. This token is acceptable for use as an HTTP ETag.
628  
629 =cut
630  
631 7 ▶sub etag {
Subroutine etag: called CC=1 SCAR=0.0
Pod documented
632 7 my $self = shift;
633 7 return $self->{hash}->b64digest;
634 }
635  
636 =item C<< size >>
637  
638 Returns the number of statements in the store.
639  
640 =cut
641  
642 72 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
643 72 my $self = shift;
644 72 my $size = $self->{size};
645 72 return $size;
646 }
647  
648 =item C<< supports ( [ $feature ] ) >>
649  
650 If C<< $feature >> is specified, returns true if the feature is supported by the
651 store, false otherwise. If C<< $feature >> is not specified, returns a list of
652 supported features.
653  
654 =cut
655  
656 0 ▶sub supports {
Subroutine supports: not called CC=1 SCAR=6.9
Pod documented
657 0 return;
658 }
659  
660 154 ▶sub _statement_id {
Subroutine _statement_id: called CC=7 SCAR=19.5
661 154 my $self = shift;
662 154 my @nodes = @_;
663 154 foreach my $pos (0 .. 3) {
664 616 my $n = $nodes[ $pos ];
665 # unless (blessed($n)) {
666 # $n = RDF::Trine::Node::Nil->new();
667 # $nodes[ $pos ] = $n;
668 # }
669 }
670
671 154 my ($subj, $pred, $obj, $context) = @nodes;
672
673 154 my @pos = (0 .. 3);
674 154 my @names = @pos_names[ @pos ];
675 154 my @sets;
676 154 foreach my $i (0 .. $#names) {
677 616 my $pos = $pos[ $i ];
678 616 my $setname = $names[ $i ];
679 616 my $data = $self->{ $setname };
680 616 my $node = $nodes[ $pos ];
681 616 my $str = $node->as_string;
682 616 my $set = $data->{ $str };
683 616 push( @sets, $set );
684 }
685
686 154 foreach my $s (@sets) {
687 613 ▶ unless (blessed($s)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($s)) 1 612 613
688 1 return -1;
689 }
690 }
691 153 my $i = shift(@sets);
692 153 ▶ while (@sets) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sets) 459 153 612
693 459 my $s = shift(@sets);
694 459 $i = $i->intersection($s);
695 }
696 153 ▶ if ($i->size == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i->size == 1) { } 152 1 153
697 152 my ($id) = $i->members;
698 152 return $id;
699 } else {
700 1 return -1;
701 }
702 }
703  
704 # sub _debug {
705 # my $self = shift;
706 # my $size = scalar(@{ $self->{statements} });
707 # warn "Memory quad-store contains " . $size . " statements:\n";
708 # foreach my $st (@{ $self->{statements} }) {
709 # if (blessed($st)) {
710 # warn $st->as_string . "\n";
711 # }
712 # }
713 # }
714  
715 68 1;
716  
717 __END__
« RDF/Trine/Parser/RDFPatch.pm ↑ Summary RDF/Trine/Store/DBI/SQLite.pm »