AtteanX/Model/SPARQLCache/LDF.pm

filter: Statementstmt 28.5% 26 / 91 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 70.0% 7 / 10 Podpod n/a totaltotal 21.1% 33 / 156 CC 26 SCAR 57.8
CC
26
Cov
18%
__ANON__
506.0
__ANON__
30.0
__ANON__
2.0
CRAP 322.6
↑ Summary AtteanX/QueryPlanner/Cache/LDF.pm »
1 package AtteanX::Model::SPARQLCache::LDF;
2  
3  
4 10 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Types::Standard qw(InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Class::Method::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 extends 'AtteanX::Model::SPARQLCache';
14 10 with 'MooX::Log::Any';
15  
16 10 has 'ldf_store' => (is => 'ro',
17 isa => InstanceOf['AtteanX::Store::LDF'],
18 required => 1);
19  
20 10 has 'publisher' => (is => 'ro',
21 isa => InstanceOf['Redis'],
22 predicate => 'has_publisher'
23 );
24  
25 0 ▶around 'cost_for_plan' => sub {
Subroutine __ANON__: not called CC=22 SCAR=62.3
26 0 my $orig = shift;
27 0 my @params = @_;
28 0 my $self = shift;
29 0 my $plan = shift;
30 0 my $planner = shift;
31 0 my @passthroughs = qw/Attean::Plan::Iterator Attean::Plan::Quad/;
32 0 my $cost = $orig->(@params);
33 0 ▶ if ($self->log->is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->log->is_debug) 0 0 0
34 0 ▶ my $logcost = $cost || 'not defined';
Condition0%0/2
$cost || 'not defined'
l!l
00
MC/DC0%0/1
$cost || 'not defined'
$cost
Truth table0%0/2
A
A= $cost
Aresult
11
00
35 0 $self->log->debug('Cost for original plan \'' . ref($plan) . "' was $logcost.");
36 }
37 0 ▶ if ($plan->isa('AtteanX::Plan::LDF::Triple')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->isa("AtteanX::Plan::LDF::Triple")) 0 0 0
38 0 $cost = $self->ldf_store->cost_for_plan($plan, $planner);
39 0 return $cost;
40 }
41 0 ▶ if ($cost && any { $plan->isa($_) } @passthroughs) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($cost and &any(sub { $plan->isa($_); } , @passthroughs)) 0 0 0
Condition0%0/3
$cost and &any(sub { $plan->isa($_); } , @passthroughs)
!ll&&!rl&&r
000
MC/DC0%0/2
$cost and &any(sub { $plan->isa($_); } , @passthroughs)
$cost &any(sub { $plan->isa($_); } , @passthroughs)
Truth table0%0/3
A and B
A= $costB= &any(sub { $plan->isa($_); } , @passthroughs)
ABresult
0X0
100
111
42 # In here, we just pass the plans that probably do not need
43 # balancing against others
44 0 $self->log->debug("Use original's cost for '" . ref($plan) . "'");
45 0 return $cost;
46 }
47 # This is where the plans that needs to be balanced against LDFs go
48 0 ▶ if ($plan->isa('AtteanX::Plan::SPARQLBGP')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->isa("AtteanX::Plan::SPARQLBGP")) 0 0 0
49 0 ▶ if ($cost <= 1000 && (scalar(@{ $plan->children }) == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cost <= 1000 and scalar @{$plan->children;} == 1) { } 0 0 0
Condition0%0/3
$cost <= 1000 and scalar @{$plan->children;} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$cost <= 1000 and scalar @{$plan->children;} == 1
$cost <= 1000 scalar @{$plan->children;} == 1
Truth table0%0/3
A and B
A= $cost <= 1000B= scalar @{$plan->children;} == 1
ABresult
0X0
100
111
50 0 $self->log->trace("Set cost for single BGP SPARQL plan");
51 0 $cost = 1001;
52 } else {
53 0 $cost = ($cost + 1) * 5;
54 }
55 0 return $cost;
56 }
57 0 ▶ if ($plan->does('Attean::API::Plan::Join')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->does("Attean::API::Plan::Join")) 0 0 0
58 # Then, penalize the plan by the number of LDFs
59 0 my $countldfs = scalar $plan->subpatterns_of_type('AtteanX::Plan::LDF::Triple');
60 0 ▶ return unless ($countldfs);
Branch: 1 branch
BranchTrueFalseTotal
unless $countldfs 0 0 0
61 0 ▶ unless ($cost) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cost) 0 0 0
62 0 my @children = @{ $plan->children };
63 0 ▶ if ($plan->isa('Attean::Plan::NestedLoopJoin')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($plan->isa('Attean::Plan::NestedLoopJoin')) { } 0 0 0
elsif ($plan->isa('Attean::Plan::HashJoin')) { } 0 0 0
64 0 my $lcost = $planner->cost_for_plan($children[0], $self);
65 0 my $rcost = $planner->cost_for_plan($children[1], $self);
66 0 ▶ if ($lcost == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lcost == 0) { } 0 0 0
elsif ($rcost == 0) { } 0 0 0
67 0 $cost = $rcost;
68 } elsif ($rcost == 0) {
69 0 $cost = $lcost;
70 } else {
71 0 $cost = $lcost * $rcost;
72 }
73 0 ▶ $cost++ if ($rcost > $lcost);
Branch: 1 branch
BranchTrueFalseTotal
if $rcost > $lcost 0 0 0
74 } elsif ($plan->isa('Attean::Plan::HashJoin')) {
75 0 my $lcost = $planner->cost_for_plan($children[0], $self);
76 0 my $rcost = $planner->cost_for_plan($children[1], $self);
77 0 $cost = ($lcost + $rcost);
78 0 ▶ $cost++ if ($rcost > $lcost);
Branch: 1 branch
BranchTrueFalseTotal
if $rcost > $lcost 0 0 0
79 }
80 }
81 0 ▶ unless ($plan->children_are_variable_connected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plan->children_are_variable_connected) 0 0 0
82 0 ▶ $cost *= 10 if $plan->isa('Attean::Plan::NestedLoopJoin');
Branch: 1 branch
BranchTrueFalseTotal
if $plan->isa("Attean::Plan::NestedLoopJoin") 0 0 0
83 0 ▶ $cost *= 100 if $plan->isa('Attean::Plan::HashJoin');
Branch: 1 branch
BranchTrueFalseTotal
if $plan->isa("Attean::Plan::HashJoin") 0 0 0
84 }
85  
86 0 $cost *= $countldfs; # TODO: This is assuming that it is better to join remotely
87 }
88  
89 # Now, penalize plan if any SPARQLBGP has a common variable with a LDFTriple
90 0 my %bgpvars;
91 my %ldfvars;
92 0 my $shared = 0;
93 0 ▶ $plan->walk(prefix => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
94 0 my $node = shift;
95 0 ▶ if ($node->isa('AtteanX::Plan::SPARQLBGP')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->isa('AtteanX::Plan::SPARQLBGP')) { } 0 0 0
elsif ($node->isa('AtteanX::Plan::LDF::Triple')) { } 0 0 0
96 0 map { $bgpvars{$_} = 1 } @{$node->in_scope_variables};
97 }
98 elsif ($node->isa('AtteanX::Plan::LDF::Triple')) {
99 0 map { $ldfvars{$_} = 1 } @{$node->in_scope_variables};
100 # TODO: A single loop should be sufficient
101 }
102 0 foreach my $lid (keys(%ldfvars)) {
103 0 ▶ if ($bgpvars{$lid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bgpvars{$lid}) 0 0 0
104 0 $shared = $lid;
105 0 last;
106 # TODO: Jump out of the walk here
107 }
108 }
109 0 });
110 0 ▶ if ($shared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shared) 0 0 0
111 0 $self->log->debug("Penalizing for SPARQL and LDF common variable '?$shared'.");
112 0 $cost += 1000;
113 }
114 # $cost *= 10; # TODO: Just multiply by a factor for now...
115
116 0 return $cost;
117 10 };
118  
119  
120  
121 10 1;
↑ Summary AtteanX/QueryPlanner/Cache/LDF.pm »