AtteanX/Model/SPARQLCache.pm

filter: Statementstmt 88.2% 60 / 68 Branchbran 67.6% 23 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 80.7% 92 / 114 CC 18 SCAR 27.8
CC
18
Cov
81%
cost_for_plan
22.9
BEGIN
1.0
BEGIN
1.0
CRAP 16.1
« AtteanX/Query/Cache/Analyzer.pm ↑ Summary AtteanX/QueryPlanner/Cache.pm »
1 package AtteanX::Model::SPARQLCache;
2  
3  
4 11 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Types::Standard qw(InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 extends 'AtteanX::Model::SPARQL';
13 11 with 'MooX::Log::Any';
14  
15 11 has 'cache' => (
16 is => 'ro',
17 isa => InstanceOf['CHI::Driver'],
18 required => 1
19 );
20  
21 # Override the store's planner, to take back control
22 4 ▶sub plans_for_algebra {
Subroutine plans_for_algebra: called CC=1 SCAR=0.0
Pod undocumented
23 4 return;
24 }
25  
26 17420 ▶sub cost_for_plan {
Subroutine cost_for_plan: called CC=18 SCAR=31.3
Pod undocumented
27 17420 my $self = shift;
28 17420 my $plan = shift;
29 17420 my $planner = shift;
30 # warn $plan->as_string;
31 17420 ▶ if ($plan->isa('Attean::Plan::Iterator')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($plan->isa('Attean::Plan::Iterator')) { } 2 17418 17420
elsif ($plan->isa('Attean::Plan::Quad')) { } 0 17418 17418
elsif ($plan->isa('AtteanX::Plan::SPARQLBGP')) { } 1574 15844 17418
elsif ($plan->does('Attean::API::Plan::Join')) { } 15844 0 15844
32 2 my $cost = 2;
33 2 ▶ $cost += int(log($plan->size_estimate+1)/log(10)) if ($plan->has_size_estimate);
Branch: 1 branch
BranchTrueFalseTotal
if $plan->has_size_estimate 2 0 2
34 2 return $cost;
35 } elsif ($plan->isa('Attean::Plan::Quad')) {
36 0 return 100000;
37 } elsif ($plan->isa('AtteanX::Plan::SPARQLBGP')) {
38 # BGPs should have a cost proportional to the number of triple patterns,
39 # but be much more costly if they contain a cartesian product.
40 1574 $self->log->trace('Estimating cost for single BGP');
41 1574 ▶ if ($plan->children_are_variable_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->children_are_variable_connected) { } 1574 0 1574
42 1574 return 20 * scalar(@{ $plan->children });
43 } else {
44 0 return 200 * scalar(@{ $plan->children });
45 }
46 } elsif ($plan->does('Attean::API::Plan::Join')) {
47 15844 my @bgps = $plan->subpatterns_of_type('AtteanX::Plan::SPARQLBGP');
48 15844 my $countbgps = scalar(@bgps);
49 15844 ▶ return unless $countbgps;
Branch: 1 branch
BranchTrueFalseTotal
unless $countbgps 0 15844 15844
50 # Now, we have SPARQLBGPs as subplans, which is usually not wanted
51 15844 my @children = @{ $plan->children };
52 15844 ▶ if ($self->log->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->log->is_trace) 0 15844 15844
53 0 $self->log->trace("Found $countbgps SPARQL BGP subplans, immediate children are of type " . join(', ', map {ref} @children))
54 }
55 15844 my $cost = 0;
56 # The below code is from Attean::API::SimpleCostPlanner
57 15844 ▶ if ($plan->isa('Attean::Plan::NestedLoopJoin')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($plan->isa('Attean::Plan::NestedLoopJoin')) { } 7962 7882 15844
elsif ($plan->isa('Attean::Plan::HashJoin')) { } 7882 0 7882
58 7962 my $lcost = $planner->cost_for_plan($children[0], $self);
59 7962 my $rcost = $planner->cost_for_plan($children[1], $self);
60 7962 ▶ if ($lcost == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lcost == 0) { } 0 7962 7962
elsif ($rcost == 0) { } 0 7962 7962
61 0 $cost = $rcost;
62 } elsif ($rcost == 0) {
63 0 $cost = $lcost;
64 } else {
65 7962 $cost = $lcost * $rcost;
66 }
67 7962 ▶ $cost++ if ($rcost > $lcost);
Branch: 1 branch
BranchTrueFalseTotal
if $rcost > $lcost 3949 4013 7962
68 7962 ▶ $cost *= 10 unless ($plan->children_are_variable_connected);
Branch: 1 branch
BranchTrueFalseTotal
unless $plan->children_are_variable_connected 80 7882 7962
69 } elsif ($plan->isa('Attean::Plan::HashJoin')) {
70 7882 my $joined = $plan->children_are_variable_connected;
71 7882 my $lcost = $planner->cost_for_plan($children[0], $self);
72 7882 my $rcost = $planner->cost_for_plan($children[1], $self);
73 7882 $cost = ($lcost + $rcost);
74 7882 ▶ $cost++ if ($rcost > $lcost);
Branch: 1 branch
BranchTrueFalseTotal
if $rcost > $lcost 3941 3941 7882
75 7882 ▶ $cost *= 100 unless ($plan->children_are_variable_connected);
Branch: 1 branch
BranchTrueFalseTotal
unless $plan->children_are_variable_connected 0 7882 7882
76 }
77 15844 ▶ if ($cost) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cost) 15844 0 15844
78 15844 $cost *= $countbgps * 1.2;
79 15844 $cost = min($cost, 1_000_000_000);
80 15844 return int($cost);
81 }
82 }
83 0 return;
84 };
85  
86 3 ▶sub is_cached {
Subroutine is_cached: called CC=1 SCAR=0.0
Pod undocumented
87 3 my $self = shift;
88 3 my $keypattern = shift;
89 3 return $self->cache->is_valid($keypattern);
90 }
91  
92  
93 11 1;
« AtteanX/Query/Cache/Analyzer.pm ↑ Summary AtteanX/QueryPlanner/Cache.pm »