AtteanX/Query/Cache/Analyzer.pm

filter: Statementstmt 42.3% 55 / 130 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 70.0% 14 / 20 Podpod 33.3% 1 / 3 totaltotal 38.8% 70 / 180 CC 22 SCAR 36.4
CC
22
Cov
35%
analyze_and_cache
90.0
best_cost_improvement
59.0
count_patterns
42.0
CRAP 38.0
« AtteanX/Query/Cache/Retriever.pm ↑ Summary AtteanX/Model/SPARQLCache.pm »
1 package AtteanX::Query::Cache::Analyzer;
2  
3 5 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $AUTHORITY = 'cpan:KJETILK';
8 5 our $VERSION = '0.002';
9  
10 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Attean::RDF qw(triplepattern variable iri);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Types::Standard qw(Str Int InstanceOf ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Types::URI -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use AtteanX::Parser::SPARQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use AtteanX::Query::Cache::Analyzer::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use AtteanX::QueryPlanner::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use AtteanX::Query::Cache::Analyzer::QueryPlanner;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use AtteanX::Query::Cache::Retriever;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 5 has 'query' => (is => 'ro', required => 1, isa => Str);
23 5 has 'algebra' => (is => 'ro', isa => ConsumerOf['Attean::API::Algebra'], builder => '_parse_query', lazy => 1);
24 5 has 'base_uri' => (is => 'ro', default => 'http://default.invalid/');
25  
26 5 has 'model' => (is => 'ro', isa => InstanceOf['AtteanX::Query::Cache::Analyzer::Model'], required => 1);
27  
28 0 ▶has 'graph' => (is => 'ro', isa => InstanceOf['Attean::IRI'], default => sub { return iri('http://example.invalid')});
Subroutine __ANON__: not called CC=1 SCAR=1.2
29  
30 5 has 'improvement_threshold' => (is => 'ro', isa => Int, default => '10');
31 5 has 'improvement_top' => (is => 'ro', isa => Int, default => '3');
32  
33 5 has 'count_threshold' => (is => 'ro', isa => Int, default => '3');
34  
35 0 ▶has 'max_triples' => (is => 'ro', isa => Int, default => sub { return $ENV{'LDF_MAX_TRIPLES'} || 100000 });
Subroutine __ANON__: not called CC=2 SCAR=13.0
Condition0%0/2
$ENV{'LDF_MAX_TRIPLES'} || 100000
l!l
00
MC/DC0%0/1
$ENV{'LDF_MAX_TRIPLES'} || 100000
$ENV{'LDF_MAX_TRIPLES'}
Truth table0%0/2
A
A= $ENV{'LDF_MAX_TRIPLES'}
Aresult
11
00
36  
37 5 with 'MooX::Log::Any';
38  
39 =pod
40  
41 =over
42  
43 =item C<< store >>
44  
45 A L<Redis> object. This has two purposes: First, to store any
46 data the analyzer needs to persist to decide when to prefetch. Second,
47 it uses Redis' publish-subscribe system to publish the URLs containing
48 queries that the prefetcher should fetch.
49  
50 =cut
51  
52 5 has store => (is => 'ro',
53 isa => InstanceOf['Redis'],
54 required => 1
55 );
56  
57 0 ▶sub _parse_query {
Subroutine _parse_query: not called CC=1 SCAR=6.9
58 0 my $self = shift;
59 0 my $parser = AtteanX::Parser::SPARQL->new();
60 0 my ($algebra) = $parser->parse_list_from_bytes($self->query, $self->base_uri); # TODO: this is a bit of cargocult
61 0 return $algebra;
62 }
63  
64 0 ▶sub best_cost_improvement {
Subroutine best_cost_improvement: not called CC=8 SCAR=40.8
Pod undocumented
65 0 my $self = shift;
66 # First, we find the cost of the plan with the current cache:
67 0 my $algebra = $self->algebra;
68 0 my $curplanner = AtteanX::QueryPlanner::Cache::LDF->new;
69 0 my $curplan = $curplanner->plan_for_algebra($algebra, $self->model, [$self->graph]);
70 0 my $curcost = $curplanner->cost_for_plan($curplan, $self->model);
71 0 $self->log->trace("Cost of incumbent plan: $curcost");
72 0 my %costs;
73 my %triples;
74 0 my $percentage = 1-($self->improvement_threshold/100);
75 0 my $planner = AtteanX::Query::Cache::Analyzer::QueryPlanner->new;
76 0 foreach my $bgp ($algebra->subpatterns_of_type('Attean::Algebra::BGP')) { # TODO: Parallelize
77 0 foreach my $triple (@{ $bgp->triples }) { # TODO: May need quads
78 0 my $key = $triple->canonicalize->tuples_string;
79 0 ▶ next if ($self->model->is_cached($key));
Branch: 1 branch
BranchTrueFalseTotal
if $self->model->is_cached($key) 0 0 0
80 0 ▶ next if ($self->model->ldf_store->count_triples_estimate($triple->values) > $self->max_triples);
Branch: 1 branch
BranchTrueFalseTotal
if $self->model->ldf_store->count_triples_estimate($triple->values) > $self->max_triples 0 0 0
81 0 $self->model->try($key);
82 0 ▶ if ($self->log->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->log->is_trace) 0 0 0
83 0 foreach my $plan ($planner->plans_for_algebra($algebra, $self->model, [$self->graph])) {
84 0 my $cost = $planner->cost_for_plan($plan, $self->model);
85 0 $self->log->trace("Cost $cost for:\n" . $plan->as_string);
86 }
87 }
88 0 my $plan = $planner->plan_for_algebra($algebra, $self->model, [$self->graph]);
89 0 $self->log->debug("Alternative plan after fetching $key:\n" . $plan->as_string);
90 0 $costs{$key} = $planner->cost_for_plan($plan, $self->model);
91 0 $self->log->info("Triple $key has cost $costs{$key}, current $curcost");
92 0 ▶ if ($costs{$key} < $curcost * $percentage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($costs{$key} < $curcost * $percentage) 0 0 0
93 0 $triples{$key} = $triple;
94 }
95 }
96 }
97 5 ▶ no sort 'stable';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 0 my @worthy = map { $triples{$_} } sort {$costs{$a} <=> $costs{$b}} keys(%triples);
99 0 return splice(@worthy,0, $self->improvement_top-1);
100 }
101  
102  
103 =item C<< count_patterns >>
104  
105 Loops the triple patterns, checks if any of them have a cached result
106 (TODO) and increments the number of times a certain predicate has been
107 seen in the store. When that number exceeds the C<count_threshold>, a
108 single-element array of L<Attean::TriplePattern>s will be returned.
109  
110  
111 =back
112  
113 =cut
114  
115 0 ▶sub count_patterns {
Subroutine count_patterns: not called CC=6 SCAR=37.4
Pod documented
116 0 my $self = shift;
117 0 my $algebra = $self->algebra;
118 0 my @worthy = ();
119 # TODO: Return undef if we can't process the query
120 0 foreach my $bgp ($algebra->subpatterns_of_type('Attean::Algebra::BGP')) {
121 0 foreach my $triple (@{ $bgp->triples }) { # TODO: May need quads
122 0 my $patternkey = $triple->canonicalize->tuples_string; # This is the key for the triple we process
123 0 ▶ next if ($self->model->is_cached($patternkey));
Branch: 1 branch
BranchTrueFalseTotal
if $self->model->is_cached($patternkey) 0 0 0
124 0 ▶ next if ($self->model->ldf_store->count_triples_estimate($triple->values) > $self->max_triples);
Branch: 1 branch
BranchTrueFalseTotal
if $self->model->ldf_store->count_triples_estimate($triple->values) > $self->max_triples 0 0 0
125 0 my $key = $triple->predicate->as_string; # This is the key for the predicate we count
126 # Update the storage and return the triple pattern
127 0 $self->store->incr($key);
128 0 my $count = $self->store->get($key);
129 0 $self->log->debug("Count for key '$key' in database is $count");
130 0 ▶ if ($count >= $self->count_threshold) { # TODO: A way to expire counts
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= $self->count_threshold) 0 0 0
131 0 $self->log->info("Triple '$patternkey' has predicate with $count counts");
132 0 push(@worthy, $triple);
133 }
134 }
135 }
136 0 return @worthy;
137 }
138  
139 0 ▶sub analyze_and_cache {
Subroutine analyze_and_cache: not called CC=9 SCAR=45.0
Pod undocumented
140 0 my ($self, @analyzers) = @_;
141 0 ▶ croak 'No analyzers given to analyze and cache' unless @analyzers;
Branch: 1 branch
BranchTrueFalseTotal
unless @analyzers 0 0 0
142 0 ▶ if ($analyzers[0] eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($analyzers[0] eq "all") 0 0 0
143 0 @analyzers = ('count_patterns', 'best_cost_improvement');
144 }
145 0 foreach my $analyzer (@analyzers) {
146 0 ▶ croak "Could not find analyzer method $analyzer" unless $self->can($analyzer);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($analyzer) 0 0 0
147 }
148 0 $self->log->info('Running analyzers named ' . join(', ', @analyzers));
149 0 my $retriever = AtteanX::Query::Cache::Retriever->new(model => $self->model); # TODO: Only OK if we don't do query planning
150 0 my $i = 0;
151 0 my %done;
152 0 foreach my $analyzer (@analyzers) {
153 0 foreach my $triple ($self->$analyzer) {
154 0 my $key = $triple->canonicalize->tuples_string;
155 0 ▶ next if $done{$key}; # Skip if some other analyzer already did fetch
Branch: 1 branch
BranchTrueFalseTotal
if $done{$key} 0 0 0
156 0 $self->log->debug('Fetching triple pattern ' . $triple->as_string);
157 0 my $data = $retriever->fetch($triple);
158 0 ▶ if (defined($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 0 0 0
159 0 $done{$key} = 1;
160 0 $i++;
161 }
162 0 $self->model->cache->set($key, $data);
163 }
164 }
165 0 $self->log->info("Got results from prefetching $i triple patterns");
166 0 return $i;
167 }
168  
169 5 1;
« AtteanX/Query/Cache/Retriever.pm ↑ Summary AtteanX/Model/SPARQLCache.pm »