| 1 | 11 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 11 | ▶ | use feature "state"; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | package AtteanX::Query::AccessPlan::Cache; | ||||||||||||||
| 7 | 11 | ▶ | use Class::Method::Modifiers; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 11 | our $AUTHORITY = 'cpan:KJETILK'; | |||||||||||||
| 10 | 11 | our $VERSION = '0.002'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | 11 | ▶ | use Moo::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 11 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 11 | with 'MooX::Log::Any'; | |||||||||||||
| 16 | |||||||||||||||
| 17 | 7 | ▶ | around 'access_plans' => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.7
| |||||||||||||||
| 18 | 7 | my $orig = shift; | |||||||||||||
| 19 | 7 | my @params = @_; | |||||||||||||
| 20 | 7 | my $self = shift; | |||||||||||||
| 21 | 7 | my $model = shift; | |||||||||||||
| 22 | 7 | my $active_graphs = shift; | |||||||||||||
| 23 | 7 | my $pattern = shift; | |||||||||||||
| 24 | |||||||||||||||
| 25 | # First, add any plans coming from the original planner (which will | ||||||||||||||
| 26 | # include queries to the remote SPARQL endpoint | ||||||||||||||
| 27 | 7 | my @plans = $orig->(@params); | |||||||||||||
| 28 | 7 | my @vars = $pattern->values_consuming_role('Attean::API::Variable'); | |||||||||||||
| 29 | 7 | my @varstrings = map { $_->value } @vars; | |||||||||||||
| 30 | |||||||||||||||
| 31 | # Start checking the cache | ||||||||||||||
| 32 | 7 | my $keypattern = $pattern->canonicalize->tuples_string; | |||||||||||||
| 33 | 7 | my $cached = $model->cache->get($keypattern); | |||||||||||||
| 34 | 7 | ▶ | if (defined($cached)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 3 | $self->log->info("Found data in the cache for " . $keypattern); | |||||||||||||
| 36 | 3 | my $parser = Attean->get_parser('NTriples')->new(lazy_iris => 1); | |||||||||||||
| 37 | 3 | my $iter; | |||||||||||||
| 38 | 3 | my $size = 0; | |||||||||||||
| 39 | 3 | ▶ | if (ref($cached) eq 'ARRAY') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 40 | 2 | $size = scalar @{$cached}; | |||||||||||||
| 41 | # Then, the cache resulted from a TP with just one variable | ||||||||||||||
| 42 | $iter = Attean::CodeIterator->new( | ||||||||||||||
| 43 | 1 | ▶ | generator => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||
| 44 | 1 | state $i = 0; | |||||||||||||
| 45 | 1 | ▶ | return undef if ($i > $#{$cached}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 1 | my $term = $parser->parse_term_from_string(${$cached}[$i]); | |||||||||||||
| 47 | 0 | $i++; | |||||||||||||
| 48 | 0 | return Attean::Result->new(bindings => { $vars[0]->value => $term }); | |||||||||||||
| 49 | }, | ||||||||||||||
| 50 | 2 | item_type => 'Attean::API::Result', | |||||||||||||
| 51 | variables => \@varstrings | ||||||||||||||
| 52 | ); | ||||||||||||||
| 53 | } elsif (ref($cached) eq 'HASH') { | ||||||||||||||
| 54 | # Cache resulted from TP with two variables | ||||||||||||||
| 55 | 1 | my @firsts = keys(%{$cached}); | |||||||||||||
| 56 | 1 | $size = scalar @firsts; | |||||||||||||
| 57 | $iter = Attean::CodeIterator->new( | ||||||||||||||
| 58 | 1 | ▶ | generator => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=18.2
| |||||||||||||||
| 59 | 1 | state $i = 0; | |||||||||||||
| 60 | 1 | ▶ | return undef if ($i > $#firsts); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 1 | state $j = 0; | |||||||||||||
| 62 | 1 | my $term1 = $parser->parse_term_from_string($firsts[$i]); | |||||||||||||
| 63 | 0 | my @seconds = @{${$cached}{$firsts[$i]}}; | |||||||||||||
| 64 | 0 | my $term2 = $parser->parse_term_from_string($seconds[$j]); | |||||||||||||
| 65 | 0 | $j++; | |||||||||||||
| 66 | 0 | ▶ | if ($j > $#seconds) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 0 | $j = 0; | |||||||||||||
| 68 | 0 | $i++; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | 0 | return Attean::Result->new(bindings => {$vars[0]->value => $term1, | |||||||||||||
| 71 | $vars[1]->value => $term2}); | ||||||||||||||
| 72 | }, | ||||||||||||||
| 73 | 1 | item_type => 'Attean::API::Result', | |||||||||||||
| 74 | variables => \@varstrings | ||||||||||||||
| 75 | ); | ||||||||||||||
| 76 | } else { | ||||||||||||||
| 77 | 0 | croak 'Unknown data structure found in cache for key ' . $keypattern; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | 3 | push(@plans, Attean::Plan::Iterator->new( variables => \@vars, | |||||||||||||
| 80 | iterator => $iter, | ||||||||||||||
| 81 | size_estimate => $size, | ||||||||||||||
| 82 | distinct => 0, | ||||||||||||||
| 83 | ordered => [] )); | ||||||||||||||
| 84 | } else { | ||||||||||||||
| 85 | 4 | $self->log->debug("Found no data in the cache for " . $keypattern); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 7 | return @plans; | |||||||||||||
| 89 | 11 | }; | |||||||||||||
| 90 | |||||||||||||||
| 91 | |||||||||||||||
| 92 | 3528 | ▶ | sub _join_vars { | ||||||||||||
|
Subroutine
_join_vars: called CC=3 SCAR=11.0
| |||||||||||||||
| 93 | 3528 | my ($self, $lhs, $rhs) = @_; | |||||||||||||
| 94 | 3528 | my @vars = (@{ $lhs->in_scope_variables }, @{ $rhs->in_scope_variables }); | |||||||||||||
| 95 | 3528 | my %vars; | |||||||||||||
| 96 | my %join_vars; | ||||||||||||||
| 97 | 3528 | foreach my $v (@vars) { | |||||||||||||
| 98 | 9480 | ▶ | if ($vars{$v}++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 3448 | $join_vars{$v}++; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | } | ||||||||||||||
| 102 | 3528 | return keys %join_vars; | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 11 | 1; | |||||||||||||
| 106 | |||||||||||||||
| 107 | __END__ | ||||||||||||||