Query/Plan/Update.pm

filter: Statementstmt 67.5% 98 / 145 Branchbran 47.7% 21 / 44 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 73.0% 19 / 26 Podpod 100% 16 / 16 totaltotal 65.2% 154 / 236 CC 29 SCAR 27.8
CC
29
Cov
62%
explain
42.0
execute
26.4
next
12.0
CRAP 16.2
« Query/Expression/Function.pm ↑ Summary Query/Plan/Iterator.pm »
1 # RDF::Query::Plan::Update
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Plan::Update - Executable query plan for DELETE/INSERT operations.
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Plan::Update version 2.919.
11  
12 =head1 METHODS
13  
14 Beyond the methods documented below, this class inherits methods from the
15 L<RDF::Query::Plan> class.
16  
17 =over 4
18  
19 =cut
20  
21 package RDF::Query::Plan::Update;
22  
23 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 35 ▶use base qw(RDF::Query::Plan);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 35 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 35 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 35 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 35 ▶use RDF::Query::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 35 ▶use RDF::Query::ExecutionContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 35 ▶use RDF::Query::VariableBindings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 ######################################################################
36  
37 35 our ($VERSION);
38 35 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
39 35 $VERSION = '2.919';
40 }
41  
42 ######################################################################
43  
44 =item C<< new ( $delete_template, $insert_template, $pattern, \%dataset ) >>
45  
46 =cut
47  
48 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
49 8 my $class = shift;
50 8 my $delete = shift;
51 8 my $insert = shift;
52 8 my $pattern = shift;
53 8 my $dataset = shift;
54 8 my $self = $class->SUPER::new( $delete, $insert, $pattern, $dataset );
55 8 return $self;
56 }
57  
58 =item C<< execute ( $execution_context ) >>
59  
60 =cut
61  
62 8 ▶sub execute ($) {
Subroutine execute: called CC=21 SCAR=32.7
Pod documented
63 8 my $self = shift;
64 8 my $context = shift;
65 8 $self->[0]{delegate} = $context->delegate;
66 8 ▶ if ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state == $self->OPEN) 0 8 8
67 0 throw RDF::Query::Error::ExecutionError -text => "UPDATE plan can't be executed while already open";
68 }
69
70 8 my $insert_template = $self->insert_template;
71 8 my $delete_template = $self->delete_template;
72 8 my $plan = $self->pattern;
73 8 ▶ if ($self->dataset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dataset) 0 8 8
74 0 my $ds = $context->model->dataset_model( %{ $self->dataset } );
75 0 $context = $context->copy( model => $ds );
76 }
77 8 $plan->execute( $context );
78 8 ▶ if ($plan->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->state == $self->OPEN) { } 8 0 8
79 8 my $l = Log::Log4perl->get_logger("rdf.query.plan.update");
80 8 $l->trace( "executing RDF::Query::Plan::Update" );
81
82 8 my @rows;
83 8 ▶ while (my $row = $plan->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $plan->next) 9 8 17
84 9 $l->trace("Update row: $row");
85 9 push(@rows, $row);
86 }
87
88 8 my @operations = (
89 [$delete_template, 'remove_statements'],
90 [$insert_template, 'add_statement'],
91 );
92
93 8 foreach my $data (@operations) {
94 16 my ($template, $method) = @$data;
95 16 $l->trace("UPDATE running $method");
96 16 foreach my $row (@rows) {
97 18 ▶ my @triples = blessed($template) ? $template->quads : ();
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($template) ? : 12 6 18
98
99 18 TRIPLE: foreach my $t (@triples) {
100 35 my @nodes = $t->nodes;
101 35 for my $i (0 .. $#nodes) {
102 140 ▶ if ($nodes[$i]->isa('RDF::Trine::Node::Variable')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nodes[$i]->isa('RDF::Trine::Node::Variable')) { } 6 134 140
elsif ($nodes[$i]->isa('RDF::Trine::Node::Blank')) { } 1 133 134
103 6 my $name = $nodes[$i]->name;
104 6 ▶ if ($method eq 'remove_statements') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'remove_statements') { } 3 3 6
105 3 ▶ if (exists($row->{ $name })) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $row->{$name}) { } 3 0 3
106 3 $nodes[$i] = $row->{ $name };
107 } else {
108 0 next TRIPLE;
109 }
110 } else {
111 3 $nodes[$i] = $row->{ $name };
112 }
113 } elsif ($nodes[$i]->isa('RDF::Trine::Node::Blank')) {
114 1 my $id = $nodes[$i]->blank_identifier;
115 1 ▶ unless (exists($self->[0]{blank_map}{ $id })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[0]{"blank_map"}{$id}) 1 0 1
116 1 ▶ if ($method eq 'remove_statements') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'remove_statements') { } 0 1 1
117 0 $self->[0]{blank_map}{ $id } = RDF::Query::Node::Variable->new();
118 } else {
119 1 $self->[0]{blank_map}{ $id } = RDF::Query::Node::Blank->new();
120 }
121 }
122 1 $nodes[$i] = $self->[0]{blank_map}{ $id };
123 }
124 }
125 # my $ok = 1;
126 35 foreach my $i (0 .. 3) {
127 140 my $n = $nodes[ $i ];
128 140 ▶ if (not blessed($n)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($n)) 0 140 140
129 0 ▶ if ($i == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 3) { } 0 0 0
130 0 $nodes[ $i ] = RDF::Trine::Node::Nil->new();
131 } else {
132 0 next TRIPLE;
133 # $nodes[ $i ] = RDF::Query::Node::Variable->new();
134 }
135 # $ok = 0;
136 # } elsif ($n->isa('RDF::Trine::Node::Variable')) {
137 # $ok = 0;
138 }
139 }
140 # next unless ($ok);
141 35 ▶ my $st = (scalar(@nodes) == 4)
Branch: 1 branch
BranchTrueFalseTotal
scalar @nodes == 4 ? : 35 0 35
142 ? RDF::Trine::Statement::Quad->new( @nodes )
143 : RDF::Trine::Statement->new( @nodes );
144 35 $l->trace( "$method: " . $st->as_string );
145 35 ▶ if ($method eq 'remove_statements') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'remove_statements') { } 3 32 35
146 3 $context->model->$method( $st->nodes );
147 } else {
148 32 $context->model->$method( $st );
149 }
150 }
151 }
152 }
153 8 $self->[0]{ok} = 1;
154 8 $self->state( $self->OPEN );
155 } else {
156 0 warn "could not execute Update pattern plan";
157 }
158 8 $self;
159 }
160  
161 =item C<< next >>
162  
163 =cut
164  
165 0 ▶sub next {
Subroutine next: not called CC=3 SCAR=24.8
Pod documented
166 0 my $self = shift;
167 0 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 0 0
168 0 throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open UPDATE";
169 }
170
171 0 my $l = Log::Log4perl->get_logger("rdf.query.plan.update");
172 0 $self->close();
173 0 ▶ if (my $d = $self->delegate) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $self->delegate) 0 0 0
174 0 $d->log_result( $self, $self->[0]{ok} );
175 }
176 0 return $self->[0]{ok};
177 }
178  
179 =item C<< close >>
180  
181 =cut
182  
183 8 ▶sub close {
Subroutine close: called CC=2 SCAR=7.4
Pod documented
184 8 my $self = shift;
185 8 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 8 8
186 0 throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open UPDATE";
187 }
188
189 8 delete $self->[0]{ok};
190 8 $self->SUPER::close();
191 }
192  
193 =item C<< delete_template >>
194  
195 Returns the algebra object representing the RDF template to delete.
196  
197 =cut
198  
199 8 ▶sub delete_template {
Subroutine delete_template: called CC=1 SCAR=0.0
Pod documented
200 8 my $self = shift;
201 8 return $self->[1];
202 }
203  
204 =item C<< insert_template >>
205  
206 Returns the algebra object representing the RDF template to insert.
207  
208 =cut
209  
210 8 ▶sub insert_template {
Subroutine insert_template: called CC=1 SCAR=0.0
Pod documented
211 8 my $self = shift;
212 8 return $self->[2];
213 }
214  
215 =item C<< pattern >>
216  
217 Returns the pattern plan object.
218  
219 =cut
220  
221 8 ▶sub pattern {
Subroutine pattern: called CC=1 SCAR=0.0
Pod documented
222 8 my $self = shift;
223 8 return $self->[3];
224 }
225  
226 =item C<< dataset >>
227  
228 Returns the dataset HASH reference.
229  
230 =cut
231  
232 8 ▶sub dataset {
Subroutine dataset: called CC=1 SCAR=0.0
Pod documented
233 8 my $self = shift;
234 8 return $self->[4];
235 }
236  
237 =item C<< distinct >>
238  
239 Returns true if the pattern is guaranteed to return distinct results.
240  
241 =cut
242  
243 8 ▶sub distinct {
Subroutine distinct: called CC=1 SCAR=0.0
Pod documented
244 8 return 1;
245 }
246  
247 =item C<< ordered >>
248  
249 Returns true if the pattern is guaranteed to return ordered results.
250  
251 =cut
252  
253 8 ▶sub ordered {
Subroutine ordered: called CC=1 SCAR=0.0
Pod documented
254 8 return [];
255 }
256  
257 =item C<< plan_node_name >>
258  
259 Returns the string name of this plan node, suitable for use in serialization.
260  
261 =cut
262  
263 0 ▶sub plan_node_name {
Subroutine plan_node_name: not called CC=1 SCAR=6.9
Pod documented
264 0 return 'update';
265 }
266  
267 =item C<< plan_prototype >>
268  
269 Returns a list of scalar identifiers for the type of the content (children)
270 nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable
271 identifiers.
272  
273 =cut
274  
275 0 ▶sub plan_prototype {
Subroutine plan_prototype: not called CC=1 SCAR=6.9
Pod documented
276 0 my $self = shift;
277 0 return qw(A A P);
278 }
279  
280 =item C<< plan_node_data >>
281  
282 Returns the data for this plan node that corresponds to the values described by
283 the signature returned by C<< plan_prototype >>.
284  
285 =cut
286  
287 0 ▶sub plan_node_data {
Subroutine plan_node_data: not called CC=1 SCAR=6.9
Pod documented
288 0 my $self = shift;
289 0 return ($self->delete_template, $self->insert_template, $self->pattern);
290 }
291  
292 =item C<< explain >>
293  
294 Returns a string serialization of the algebra appropriate for display on the
295 command line.
296  
297 =cut
298  
299 0 ▶sub explain {
Subroutine explain: not called CC=6 SCAR=37.4
Pod documented
300 0 my $self = shift;
301 0 my $s = shift;
302 0 my $count = shift;
303 0 my $indent = $s x $count;
304 0 my $type = $self->plan_node_name;
305 0 my $string = sprintf("%s%s (0x%x)\n", $indent, $type, refaddr($self));
306
307 0 ▶ if (my $d = $self->delete_template) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $self->delete_template) 0 0 0
308 0 $string .= "${indent}${s}delete:\n";
309 0 $string .= $d->explain( $s, $count+2 );
310 }
311  
312 0 ▶ if (my $i = $self->insert_template) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = $self->insert_template) 0 0 0
313 0 $string .= "${indent}${s}insert:\n";
314 0 $string .= $i->explain( $s, $count+2 );
315 }
316  
317 0 ▶ if (my $p = $self->pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $self->pattern) 0 0 0
318 0 ▶ if ($p->isa('RDF::Query::Plan::Constant') and $p->is_unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->isa('RDF::Query::Plan::Constant') and $p->is_unit) { } 0 0 0
Condition0%0/3
$p->isa("RDF::Query::Plan::Constant") and $p->is_unit
!ll&&!rl&&r
000
MC/DC0%0/2
$p->isa("RDF::Query::Plan::Constant") and $p->is_unit
$p->isa("RDF::Query::Plan::Constant") $p->is_unit
Truth table0%0/3
A and B
A= $p->isa("RDF::Query::Plan::Constant")B= $p->is_unit
ABresult
0X0
100
111
319
320 } else {
321 0 $string .= "${indent}${s}where:\n";
322 0 $string .= $p->explain( $s, $count+2 );
323 }
324 }
325
326 0 return $string;
327 }
328  
329 =item C<< graph ( $g ) >>
330  
331 =cut
332  
333 0 ▶sub graph {
Subroutine graph: not called CC=1 SCAR=6.9
Pod documented
334 0 my $self = shift;
335 0 my $g = shift;
336 0 my $label = $self->graph_labels;
337 0 my $url = $self->url->uri_value;
338 0 throw RDF::Query::Error::ExecutionError -text => "RDF::Query::Plan::Update->graph not implemented.";
339 # $g->add_node( "$self", label => "delete" . $self->graph_labels );
340 # $g->add_node( "${self}$url", label => $url );
341 # $g->add_edge( "$self" => "${self}$url", label => 'url' );
342 # return "$self";
343 }
344  
345 =item C<< is_update >>
346  
347 Returns true if the plan represents an update operation.
348  
349 =cut
350  
351 0 ▶sub is_update {
Subroutine is_update: not called CC=1 SCAR=6.9
Pod documented
352 0 return 1;
353 }
354  
355  
356 35 1;
357  
358 __END__
« Query/Expression/Function.pm ↑ Summary Query/Plan/Iterator.pm »