Index/SQLite/Sharded.pm

filter: Statementstmt 93.8% 121 / 129 Branchbran 60.8% 28 / 46 Conditioncond 53.3% 8 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 62.8% 22 / 35 Podpod 0.0% 0 / 18 totaltotal 71.9% 182 / 253 CC 32 SCAR 16.7
CC
32
Cov
83%
_shard_key
11.2
__ANON__
10.9
initialize
9.2
CRAP 5.3
↑ Summary Index/SQL.pm »
1 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Metabase::Index::SQLite::Sharded;
6 # ABSTRACT: Metabase index using multiple SQLite databases
7  
8 1 our $VERSION = '1.001';
9  
10 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Stream::Bulk::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::Util qw/sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Storable qw/dclone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Metabase::Index::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 with 'Metabase::Backend::SQLite';
18 1 with 'Metabase::Index' => { -excludes => 'exists' };
19  
20 subtype 'IndexShardSize', # XXX should refactor with Archive one
21 as 'Int',
22 4 ▶ where { $_ > 0 && $_ < 8 }, # can't trust last byte of timestamp
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_ > 0 and $_ < 8 4 0 4
23 0 ▶ message { "The number you provided, $_, was not between 1 and 8" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
24  
25 1 has shard_digits => (
26 is => 'ro',
27 isa => 'IndexShardSize',
28 default => 2,
29 );
30  
31 has _shards => (
32 is => 'ro',
33 traits => ['Hash'],
34 isa => 'HashRef[Object]',
35 4 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1 handles => {
37 '_get_shard' => 'get',
38 '_set_shard' => 'set',
39 '_all_shards' => 'values',
40 },
41 );
42  
43 4 ▶sub initialize {
Subroutine initialize: called CC=8 SCAR=22.2
Pod undocumented
44 4 my ($self, $classes, $resources) = @_;
45 4 my $filename = $self->filename;
46 4 my ($basename, $ext) = $self->filename =~ m{^(.*)\.([^.]+)$};
47 4 ▶ $ext = '' unless defined $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext 0 4 4
48 4 ▶ $basename = $filename unless defined $basename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $basename 0 4 4
49 4 my $digits = $self->shard_digits;
50 4 ▶ my $template = $digits == 1 ? "H2" : "H$digits";
Branch: 1 branch
BranchTrueFalseTotal
$digits == 1 ? : 4 0 4
51 # warn "*** TEMPLATE: $template";
52 4 for my $n ( 0 .. 16**$digits - 1) {
53 64 my $key = unpack($template,pack("I",$n));
54 64 my $index_file;
55 64 ▶ if ( $basename && $ext ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basename and $ext) { } 64 0 64
Condition33%1/3
$basename and $ext
!ll&&!rl&&r
0064
MC/DC0%0/2
$basename and $ext
$basename $ext
Truth table33%1/3
A and B
A= $basenameB= $ext
ABresult
0X0
100
111
56 64 $index_file = "$basename\_$key.$ext";
57 }
58 else {
59 0 $index_file = "$basename\_$key";
60 }
61 64 ▶ my $index = Metabase::Index::SQLite->new(
Branch: 1 branch
BranchTrueFalseTotal
my $index = "Metabase::Index::SQLite"->new("filename", $index_file) or die "Couldn't not build shard '${index_file}' \n" 64 0 64
62 filename => $index_file,
63 ) or die "Couldn't not build shard '$index_file' \n";
64 # warn "*** Initializing $key\n";
65 64 $index->initialize($classes, $resources);
66 64 $self->_set_shard($key, $index);
67 }
68 4 return;
69 }
70  
71 13 ▶sub _shard_key {
Subroutine _shard_key: called CC=6 SCAR=24.2
72 13 my ($self, $guid) = @_;
73 13 my $digits = $self->shard_digits;
74 13 my $key = substr $guid, (7-$digits), $digits;
75 13 ▶ if ( defined $key && length $key > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $key and length $key > 1) { } 0 13 13
elsif (defined $key and length $key == 1) { } 13 0 13
Condition33%1/3
defined $key and length $key > 1
!ll&&!rl&&r
0130
MC/DC0%0/2
defined $key and length $key > 1
defined $key length $key > 1
Truth table33%1/3
A and B
A= defined $keyB= length $key > 1
ABresult
0X0
100
111
76 0 return $key;
77 }
78 ▶ elsif ( defined $key && length $key == 1 ) {
Condition33%1/3
defined $key and length $key == 1
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $key and length $key == 1
defined $key length $key == 1
Truth table33%1/3
A and B
A= defined $keyB= length $key == 1
ABresult
0X0
100
111
79 13 return "0$key";
80 }
81 else {
82 0 ▶ return scalar "0"x($digits==1 ? 2 : $digits);
Branch: 1 branch
BranchTrueFalseTotal
$digits == 1 ? : 0 0 0
83 }
84 }
85  
86 # override from role to target query at right shard
87 3 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod undocumented
88 3 my ($self, $guid) = @_;
89 3 my $key = $self->_shard_key($guid);
90 3 my $shard = $self->_get_shard($key);
91 # if desired guid in upper case, fix it
92 3 return scalar @{ $shard->search(-where => [-eq =>'core.guid'=>lc $guid])};
93 }
94  
95 8 ▶sub add {
Subroutine add: called CC=2 SCAR=7.0
Pod undocumented
96 8 my ( $self, $fact ) = @_;
97 8 my $key = $self->_shard_key($fact->guid);
98 # warn "***Adding to shard '$key'\n";
99 8 ▶ my $shard = $self->_get_shard($key)
Branch: 1 branch
BranchTrueFalseTotal
my $shard = $self->_get_shard($key) or die "Couldn't find shard for '${key}' from " . $fact->guid . "\n" 8 0 8
100 or die "Couldn't find shard for '$key' from " . $fact->guid. "\n";
101 8 return $shard->add($fact);
102 }
103  
104 2 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
105 2 my ( $self, $guid ) = @_;
106 2 my $key = $self->_shard_key($guid);
107 2 return $self->_get_shard($key)->delete($guid);
108 }
109  
110 15 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod undocumented
111 15 my ( $self, %spec ) = @_;
112 15 return sum map { $_->count(%{ dclone(\%spec) }) } $self->_all_shards;
113 }
114  
115 13 ▶sub query {
Subroutine query: called CC=1 SCAR=0.0
Pod undocumented
116 13 my ( $self, %spec) = @_;
117 13 my @shards = $self->_all_shards;
118 13 my @iters = map { $_->_shard_query(%{ dclone(\%spec) }) } $self->_all_shards;
119  
120 # XXX this does not preserve order or limit
121 13 my $limit = $spec{-limit};
122 13 my $count = 0;
123 return Data::Stream::Bulk::Callback->new(
124 24 ▶ callback => sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
125 24 ▶ return if $limit && $count == $limit; # shortcut
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $count == $limit 1 23 24
Condition100%3/3
$limit and $count == $limit
!ll&&!rl&&r
2211
MC/DC100%2/2
$limit and $count == $limit
$limit $count == $limit
Truth table100%3/3
A and B
A= $limitB= $count == $limit
ABresult
0X0
100
111
126 # Need to merge results
127 23 my @results;
128 my @not_done;
129 23 for my $s ( @iters ) {
130 223 ▶ if ( my @items = $s->items ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@items) = $s->items) 17 206 223
131 17 push @not_done, $s; # round-robin
132 17 push @results, @items;
133 }
134 }
135 23 ▶ return unless @results;
Branch: 1 branch
BranchTrueFalseTotal
unless @results 12 11 23
136 11 @iters = @not_done; # for next invocation
137 # Need to order results
138 11 ▶ if ( my @clauses = $self->_order_clauses(\%spec) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@clauses) = $self->_order_clauses(\%spec)) 3 8 11
139 3 @results = $self->_sort_results(\@results, \@clauses);
140 }
141 # Need to limit results
142 11 ▶ if ( $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) 1 10 11
143 1 ▶ if ( $count + @results <= $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count + @results <= $limit) { } 0 1 1
144 0 $count += @results;
145 }
146 else {
147 1 my $need = $limit - $count;
148 1 $count += $need;
149 1 splice @results, $need;
150 }
151 }
152 # Need to extract just guid
153 # warn "*** RESULTS: @results\n";
154 11 return [ map { $_->[0] } @results ]; # just the GUID
155 },
156 13 );
157 }
158  
159 11 ▶sub _order_clauses {
Subroutine _order_clauses: called CC=4 SCAR=13.9
160 11 my ($self, $spec) = @_;
161 11 ▶ if ( defined $spec->{-order} and ref $spec->{-order} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec->{'-order'} and ref $spec->{'-order'} eq 'ARRAY') { } 3 8 11
Condition66%2/3
defined $spec->{"-order"} and ref $spec->{"-order"} eq "ARRAY"
!ll&&!rl&&r
803
MC/DC50%1/2
defined $spec->{"-order"} and ref $spec->{"-order"} eq "ARRAY"
defined $spec->{"-order"} ref $spec->{"-order"} eq "ARRAY"
Truth table66%2/3
A and B
A= defined $spec->{"-order"}B= ref $spec->{"-order"} eq "ARRAY"
ABresult
0X0
100
111
162 3 my @clauses;
163 3 my @order = @{$spec->{-order}};
164 3 ▶ while ( @order ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@order) 3 3 6
165 3 my ($dir, $field) = splice( @order, 0, 2);
166 3 $dir =~ s/^-//;
167 3 $dir = uc $dir;
168 3 push @clauses, [$field, $dir];
169 }
170 3 return @clauses;
171 }
172 else {
173 8 return ();
174 }
175 }
176  
177 3 ▶sub _sort_results {
Subroutine _sort_results: called CC=1 SCAR=0.5
178 3 my ($self, $results, $clauses) = @_;
179 3 ▶ my $sorter = sub {
Subroutine __ANON__: called CC=7 SCAR=23.8
180 3 my ($left_data, $right_data) = @_;
181 3 for my $i ( 0 .. $#$clauses ) {
182 3 my $dir = $clauses->[$i][1];
183 3 my $left = $left_data->[$i+1];
184 3 my $right = $right_data->[$i+1];
185 3 ▶ if ( $dir eq 'ASC' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq 'ASC') { } 1 2 3
186 1 ▶ return 1 if $left gt $right;
Branch: 1 branch
BranchTrueFalseTotal
if $left gt $right 1 0 1
187 0 ▶ return -1 if $left lt $right;
Branch: 1 branch
BranchTrueFalseTotal
if $left lt $right 0 0 0
188 }
189 else {
190 2 ▶ return -1 if $left gt $right;
Branch: 1 branch
BranchTrueFalseTotal
if $left gt $right 2 0 2
191 0 ▶ return 1 if $left lt $right;
Branch: 1 branch
BranchTrueFalseTotal
if $left lt $right 0 0 0
192 }
193 }
194 0 return 0; # everything was equal
195 3 };
196 3 return sort { $sorter->($a,$b) } @$results; ## no critic
197 }
198  
199 # Fake these to satisfy the role -- we actually delegate everything out
200 # to shards
201 0 ▶sub op_and { }
Subroutine op_and: not called CC=1 SCAR=0.0
Pod undocumented
202 0 ▶sub op_between { }
Subroutine op_between: not called CC=1 SCAR=0.0
Pod undocumented
203 0 ▶sub op_eq { }
Subroutine op_eq: not called CC=1 SCAR=0.0
Pod undocumented
204 0 ▶sub op_ge { }
Subroutine op_ge: not called CC=1 SCAR=0.0
Pod undocumented
205 0 ▶sub op_gt { }
Subroutine op_gt: not called CC=1 SCAR=0.0
Pod undocumented
206 0 ▶sub op_le { }
Subroutine op_le: not called CC=1 SCAR=0.0
Pod undocumented
207 0 ▶sub op_like { }
Subroutine op_like: not called CC=1 SCAR=0.0
Pod undocumented
208 0 ▶sub op_lt { }
Subroutine op_lt: not called CC=1 SCAR=0.0
Pod undocumented
209 0 ▶sub op_ne { }
Subroutine op_ne: not called CC=1 SCAR=0.0
Pod undocumented
210 0 ▶sub op_not { }
Subroutine op_not: not called CC=1 SCAR=0.0
Pod undocumented
211 0 ▶sub op_or { }
Subroutine op_or: not called CC=1 SCAR=0.0
Pod undocumented
212 0 ▶sub translate_query { }
Subroutine translate_query: not called CC=1 SCAR=0.0
Pod undocumented
213  
214 1 1;
215  
216 __END__
↑ Summary Index/SQL.pm »