| 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
| |||||||||||||||||||||||
| 32 | 2 | my $cost = 2; | |||||||||||||||||||||
| 33 | 2 | ▶ | $cost += int(log($plan->size_estimate+1)/log(10)) if ($plan->has_size_estimate); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 68 | 7962 | ▶ | $cost *= 10 unless ($plan->children_are_variable_connected); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 75 | 7882 | ▶ | $cost *= 100 unless ($plan->children_are_variable_connected); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||
| 77 | 15844 | ▶ | if ($cost) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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; | |||||||||||||||||||||