| 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::Archive::SQLite::Sharded; | ||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Metabase storage 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 Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Metabase::Archive::SQLite; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | with 'Metabase::Backend::SQLite'; | |||||||||||||||||||||||||||
| 16 | 1 | with 'Metabase::Archive'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | subtype 'ShardSize', | ||||||||||||||||||||||||||||
| 19 | as 'Int', | ||||||||||||||||||||||||||||
| 20 | 3 | ▶ | where { $_ > 0 && $_ < 8 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | message { "The number you provided, $_, was not between 1 and 8" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | has shard_digits => ( | |||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||
| 25 | isa => 'ShardSize', | ||||||||||||||||||||||||||||
| 26 | default => 2, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | has _shards => ( | ||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | traits => ['Hash'], | ||||||||||||||||||||||||||||
| 32 | isa => 'HashRef[Object]', | ||||||||||||||||||||||||||||
| 33 | 3 | ▶ | default => sub { return {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | handles => { | |||||||||||||||||||||||||||
| 35 | '_get_shard' => 'get', | ||||||||||||||||||||||||||||
| 36 | '_set_shard' => 'set', | ||||||||||||||||||||||||||||
| 37 | '_all_shards' => 'values', | ||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | sub initialize { } | ||||||||||||||||||||||||||
|
Subroutine
initialize: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 5 | ▶ | sub _create_shard { | ||||||||||||||||||||||||||
|
Subroutine
_create_shard: called CC=5 SCAR=17.4
| |||||||||||||||||||||||||||||
| 44 | 5 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 5 | my $filename = $self->filename; | |||||||||||||||||||||||||||
| 47 | 5 | my ($basename, $ext) = $self->filename =~ m{^(.*)\.([^.]+)$}; | |||||||||||||||||||||||||||
| 48 | 5 | ▶ | if ( $basename && $ext ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$basename and $ext
MC/DC0%0/2
$basename and $ext
$basename
$ext
Truth table33%1/3
A and B
A= $basenameB= $ext
| |||||||||||||||||||||||||||||
| 49 | 0 | $filename = "$basename\_$key.$ext"; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||||||||||
| 52 | 5 | $filename .= "_$key"; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 5 | ▶ | my %args = ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 55 | filename => $filename, | ||||||||||||||||||||||||||||
| 56 | ($self->has_page_size ? ( page_size => $self->page_size ) : ()), | ||||||||||||||||||||||||||||
| 57 | ($self->has_cache_size ? ( cache_size => $self->cache_size ) : ()), | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | 5 | my $archive = Metabase::Archive::SQLite->new( %args ); | |||||||||||||||||||||||||||
| 60 | 5 | $archive->initialize; | |||||||||||||||||||||||||||
| 61 | 5 | $self->_set_shard($key, $archive); | |||||||||||||||||||||||||||
| 62 | 5 | return $archive; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 7 | ▶ | sub _shard_key { | ||||||||||||||||||||||||||
|
Subroutine
_shard_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 7 | my ($self, $guid) = @_; | |||||||||||||||||||||||||||
| 67 | 7 | return substr $guid, (7-$self->shard_digits), $self->shard_digits; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | ▶ | sub store { | ||||||||||||||||||||||||||
|
Subroutine
store: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 5 | my ( $self, $fact_struct ) = @_; | |||||||||||||||||||||||||||
| 72 | 5 | my $key = $self->_shard_key( $fact_struct->{metadata}{core}{guid} ); | |||||||||||||||||||||||||||
| 73 | 5 | ▶ | my $archive = $self->_get_shard($key) || $self->_create_shard($key); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->_get_shard($key) || $self->_create_shard($key)
MC/DC0%0/2
$self->_get_shard($key) || $self->_create_shard($key)
$self->_get_shard($key)
$self->_create_shard($key)
Truth table33%1/3
A || B
A= $self->_get_shard($key)B= $self->_create_shard($key)
| |||||||||||||||||||||||||||||
| 74 | 5 | return $archive->store($fact_struct); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | sub extract { | ||||||||||||||||||||||||||
|
Subroutine
extract: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 1 | my ( $self, $guid ) = @_; | |||||||||||||||||||||||||||
| 80 | 1 | my $key = $self->_shard_key( $guid ); | |||||||||||||||||||||||||||
| 81 | 1 | ▶ | my $archive = $self->_get_shard($key) || $self->_create_shard($key); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->_get_shard($key) || $self->_create_shard($key)
MC/DC0%0/2
$self->_get_shard($key) || $self->_create_shard($key)
$self->_get_shard($key)
$self->_create_shard($key)
Truth table33%1/3
A || B
A= $self->_get_shard($key)B= $self->_create_shard($key)
| |||||||||||||||||||||||||||||
| 82 | 1 | return $archive->extract($guid); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 1 | my ( $self, $guid ) = @_; | |||||||||||||||||||||||||||
| 87 | 1 | my $key = $self->_shard_key( $guid ); | |||||||||||||||||||||||||||
| 88 | 1 | ▶ | my $archive = $self->_get_shard($key) || $self->_create_shard($key); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->_get_shard($key) || $self->_create_shard($key)
MC/DC0%0/2
$self->_get_shard($key) || $self->_create_shard($key)
$self->_get_shard($key)
$self->_create_shard($key)
Truth table33%1/3
A || B
A= $self->_get_shard($key)B= $self->_create_shard($key)
| |||||||||||||||||||||||||||||
| 89 | 1 | return $archive->delete($guid); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | ▶ | sub iterator { | ||||||||||||||||||||||||||
|
Subroutine
iterator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 2 | my @iters = map { $_->iterator } $self->_all_shards; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | return Data::Stream::Bulk::Callback->new( | ||||||||||||||||||||||||||||
| 98 | 6 | ▶ | callback => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 99 | 6 | ▶ | if ( @iters ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 6 | my $s = shift @iters; | |||||||||||||||||||||||||||
| 101 | 6 | ▶ | if ( my $next = $s->next ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 4 | push @iters, $s; # round-robin | |||||||||||||||||||||||||||
| 103 | 4 | return $next; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||||||
| 106 | 0 | return; # done | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||
| 109 | 2 | ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | 1; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||