| 1 | package File::DataClass::Storage::Any; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use File::Basename qw( basename ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use File::DataClass::Constants qw( FALSE TRUE ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use File::DataClass::Functions qw( ensure_class_loaded first_char | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | qualify_storage_class map_extension2class | ||||||||||||||||||||||||||||
| 9 | 3 | is_stale merge_file_data throw ); | |||||||||||||||||||||||||||
| 10 | 3 | ▶ | use File::DataClass::Storage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::DataClass::Types qw( Object HashRef ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | has 'schema' => is => 'ro', isa => Object, | |||||||||||||||||||||||||||
| 15 | handles => [ 'cache', 'storage_attributes', ], | ||||||||||||||||||||||||||||
| 16 | required => TRUE, weak_ref => TRUE; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 6 | ▶ | has '_stores' => is => 'ro', isa => HashRef, default => sub { {} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Private methods | ||||||||||||||||||||||||||||
| 22 | 37 | ▶ | my $_get_store_from_extension = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 23 | 37 | my ($self, $extn) = @_; my $stores = $self->_stores; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 37 | ▶ | exists $stores->{ $extn } and return $stores->{ $extn }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 7 | ▶ | my $list; ($list = map_extension2class( $extn ) and my $class = $list->[ 0 ]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$list = &map_extension2class($extn) and my $class = $list->[0]
MC/DC50%1/2
$list = &map_extension2class($extn) and my $class = $list->[0]
$list = &map_extension2class($extn)
my $class = $list->[0]
Truth table66%2/3
A and B
A= $list = &map_extension2class($extn)B= my $class = $list->[0]
| |||||||||||||||||||||||||||||
| 28 | or throw 'Extension [_1] has no class', [ $extn ]; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if (first_char $class eq '+') { $class = substr $class, 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 6 | else { $class = qualify_storage_class $class } | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 6 | ensure_class_loaded $class; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | return $stores->{ $extn } = $class->new | ||||||||||||||||||||||||||||
| 36 | 6 | ( { %{ $self->storage_attributes }, schema => $self->schema } ); | |||||||||||||||||||||||||||
| 37 | 3 | }; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 37 | ▶ | my $_get_store_from_path = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 40 | 37 | my ($self, $path) = @_; my $file = basename( "${path}" ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 37 | ▶ | my $extn = (split m{ \. }mx, $file)[ -1 ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | or throw 'File [_1] has no extension', [ $file ]; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 37 | ▶ | my $store = $self->$_get_store_from_extension( ".${extn}" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | or throw 'Extension [_1] has no store', [ $extn ]; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 36 | return $store; | |||||||||||||||||||||||||||
| 49 | 3 | }; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Public methods | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | sub create_or_update { | ||||||||||||||||||||||||||
|
Subroutine
create_or_update: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 1 | return shift->$_get_store_from_path( $_[ 0 ] )->create_or_update( @_ ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 2 | return shift->$_get_store_from_path( $_[ 0 ] )->delete( @_ ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 5 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 5 | return shift->$_get_store_from_path( $_[ 0 ] )->dump( @_ ); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | sub extn { | ||||||||||||||||||||||||||
|
Subroutine
extn: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | sub insert { | ||||||||||||||||||||||||||
|
Subroutine
insert: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 1 | return shift->$_get_store_from_path( $_[ 0 ] )->insert( @_ ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 8 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 8 | ▶ | my ($self, @paths) = @_; $paths[ 0 ] or return {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 7 | ▶ | scalar @paths == 1 and return ($self->read_file( $paths[ 0 ], FALSE ))[ 0 ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | my ($loaded, $meta, $newest) = $self->cache->get_by_paths( \@paths ); | |||||||||||||||||||||||||||
| 77 | 1 | my $cache_mtime = $self->meta_unpack( $meta ); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | not is_stale $loaded, $cache_mtime, $newest and return $loaded; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | $loaded = {}; $newest = 0; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | for my $path (@paths) { # Different storage classes by filename extension | |||||||||||||||||||||||||||
| 84 | 2 | my ($red, $path_mtime) = $self->read_file( $path, FALSE ); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | $path_mtime > $newest and $newest = $path_mtime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 2 | merge_file_data $loaded, $red; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | $self->cache->set_by_paths( \@paths, $loaded, $self->meta_pack( $newest ) ); | |||||||||||||||||||||||||||
| 91 | 1 | return $loaded; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 3 | ▶ | sub meta_pack { | ||||||||||||||||||||||||||
|
Subroutine
meta_pack: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 3 | ▶ | my ($self, $mtime) = @_; my $attr = $self->{_meta_cache} || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'_meta_cache'} || {}
MC/DC100%1/1
$self->{'_meta_cache'} || {}
$self->{'_meta_cache'}
Truth table100%2/2
A
A= $self->{'_meta_cache'}
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | ▶ | defined $mtime and $attr->{mtime} = $mtime; return $attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | ▶ | sub meta_unpack { | ||||||||||||||||||||||||||
|
Subroutine
meta_unpack: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 2 | my ($self, $attr) = @_; $self->{_meta_cache} = $attr; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | ▶ | return $attr ? $attr->{mtime} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 8 | ▶ | sub read_file { | ||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 8 | return shift->$_get_store_from_path( $_[ 0 ] )->read_file( @_ ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 8 | ▶ | sub select { | ||||||||||||||||||||||||||
|
Subroutine
select: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 8 | return shift->$_get_store_from_path( $_[ 0 ] )->select( @_ ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 8 | ▶ | sub txn_do { | ||||||||||||||||||||||||||
|
Subroutine
txn_do: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 8 | return shift->$_get_store_from_path( $_[ 0 ] )->txn_do( @_ ); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 2 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 2 | return shift->$_get_store_from_path( $_[ 0 ] )->update( @_ ); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 2 | ▶ | sub validate_params { | ||||||||||||||||||||||||||
|
Subroutine
validate_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 2 | return shift->$_get_store_from_path( $_[ 0 ] )->validate_params( @_ ); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 3 | 1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||