lib/File/DataClass/Storage.pm

filter: Statementstmt 98.4% 195 / 199 (1 uncoverable) Branchbran 79.6% 43 / 54 Conditioncond 73.5% 25 / 34 MC/DCmcdc 42.1% 8 / 19 Subroutinesub 96.1% 50 / 52 Podpod 100% 16 / 16 totaltotal 90.3% 337 / 374 (1 uncoverable) CC 42 SCAR 11.7
CC
42
Cov
92%
__ANON__
9.3
load
6.0
__ANON__
5.0
CRAP 3.2
« lib/File/DataClass/ResultSet.pm ↑ Summary lib/File/DataClass/Storage/Any.pm »
1 package File::DataClass::Storage;
2  
3 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Class::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::DataClass::Constants qw( EXCEPTION_CLASS FALSE NUL TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::DataClass::Functions qw( is_stale merge_file_data
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 merge_for_update throw );
11 4 ▶use File::DataClass::Types qw( Bool HashRef Object Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Unexpected::Functions qw( RecordAlreadyExists PathNotFound
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 NothingUpdated Unspecified );
16 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 has 'atomic_write' => is => 'ro', isa => Bool, default => TRUE;
19  
20 4 has 'backup' => is => 'ro', isa => Str, default => NUL;
21  
22 4 has 'encoding' => is => 'ro', isa => Str, default => NUL;
23  
24 4 has 'extn' => is => 'ro', isa => Str, default => NUL;
25  
26 0 ▶has 'read_options' => is => 'ro', isa => HashRef, builder => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=0.4
Subroutine _build_read_options: called CC=1 SCAR=0.4
27  
28 4 has 'schema' => is => 'ro', isa => Object,
29 handles => { _cache => 'cache', _lock => 'lock',
30 _log => 'log', _perms => 'perms', },
31 required => TRUE, weak_ref => TRUE;
32  
33 0 ▶has 'write_options' => is => 'ro', isa => HashRef, builder => sub { {} };
Subroutine _build_write_options: not called CC=1 SCAR=0.4
Subroutine __ANON__: called CC=1 SCAR=0.4
34  
35 10 ▶has '_locks' => is => 'ro', isa => HashRef, builder => sub { {} };
Subroutine _build__locks: called CC=1 SCAR=0.0
36  
37 # Private functions
38 17 ▶my $_get_src_attributes = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
39 17 my ($cond, $src) = @_;
40  
41 54 ▶ return grep { not m{ \A _ }mx
Condition77%7/9
!/ \A _ /mx && $_ ne 'id' && $_ ne 'name' && &$cond($_)
!ll&&!rl&&r
34020
not / \A _ /mx and $_ ne "id" and $_ ne "name"
!ll&&!rl&&r
34020
not / \A _ /mx and $_ ne "id"
!ll&&!rl&&r
171720
MC/DC50%2/4
!/ \A _ /mx && $_ ne 'id' && $_ ne 'name' && &$cond($_)
not / \A _ /mx $_ ne "id" $_ ne "name" &$cond($_)
Truth table60%3/5
not(A) and B and C && D
A= not / \A _ /mxB= $_ ne "id"C= $_ ne "name"D= &$cond($_)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
42 and $_ ne 'id' and $_ ne 'name'
43 17 and $cond->( $_ ) } keys %{ $src };
44 4 };
45  
46 147 ▶my $_lock_set = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 147 $_[ 0 ]->_lock->set( k => $_[ 1 ] ); $_[ 0 ]->_locks->{ $_[ 1 ] } = TRUE;
48 4 };
49  
50 147 ▶my $_lock_reset = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 147 $_[ 0 ]->_lock->reset( k => $_[ 1 ] ); delete $_[ 0 ]->_locks->{ $_[ 1 ] };
52 4 };
53  
54 16 ▶my $_lock_reset_all = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
55 16 my $self = shift;
56  
57 0 eval { $self->$_lock_reset( $_ ) } for (keys %{ $self->_locks });
58  
59 16 return;
60 4 };
61  
62 # Public methods
63 18 ▶sub create_or_update {
Subroutine create_or_update: called CC=3 SCAR=11.0
Pod documented
64 18 my ($self, $path, $result, $updating, $cond) = @_;
65  
66 18 my $rsrc_name = $result->result_source->name;
67  
68 18 $self->validate_params( $path, $rsrc_name ); my $updated;
69  
70 18 my $data = ($self->read_file( $path, TRUE ))[ 0 ];
71  
72 18 ▶ try {
Subroutine __ANON__: called CC=4 SCAR=13.9
73 18 ▶ my $filter = sub { $_get_src_attributes->( $cond, $_[ 0 ] ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
74 18 ▶ my $id = $result->id; $data->{ $rsrc_name } //= {};
Condition100%2/2
$data->{$rsrc_name} //= {}
l!l
126
MC/DC100%1/1
$data->{$rsrc_name} //= {}
$data->{$rsrc_name}
Truth table100%2/2
A
A= $data->{$rsrc_name}
Aresult
11
00
75  
76 18 ▶ not $updating and exists $data->{ $rsrc_name }->{ $id }
Branch: 1 branch
BranchTrueFalseTotal
not $updating and exists $data->{$rsrc_name}{$id} and &throw(RecordAlreadyExists(), [$path, $id], "level", 2) 1 17 18
Condition100%3/3
not $updating and exists $data->{$rsrc_name}{$id}
!ll&&!rl&&r
1251
MC/DC100%2/2
not $updating and exists $data->{$rsrc_name}{$id}
not $updating exists $data->{$rsrc_name}{$id}
Truth table100%3/3
not(A) and B
A= not $updatingB= exists $data->{$rsrc_name}{$id}
ABresult
0X0
100
111
77 and throw RecordAlreadyExists, [ $path, $id ], level => 2;
78  
79 $updated = merge_for_update
80 17 ( \$data->{ $rsrc_name }->{ $id }, $result, $filter );
81 }
82 18 ▶ catch { $self->$_lock_reset( $path ); throw $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
83  
84 17 ▶ if ($updated) { $self->write_file( $path, $data, not $updating ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($updated) { } 15 2 17
85 2 else { $self->$_lock_reset( $path ) }
86  
87 17 ▶ return $updated ? $result : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$updated ? : 15 2 17
88 }
89  
90 6 ▶sub delete {
Subroutine delete: called CC=4 SCAR=13.9
Pod documented
91 6 my ($self, $path, $result) = @_;
92  
93 6 my $rsrc_name = $result->result_source->name;
94  
95 6 $self->validate_params( $path, $rsrc_name );
96  
97 6 my $data = ($self->read_file( $path, TRUE ))[ 0 ]; my $id = $result->id;
98  
99 6 ▶ if (exists $data->{ $rsrc_name } and exists $data->{ $rsrc_name }->{ $id }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{$rsrc_name} and exists $data->{$rsrc_name}{$id}) 5 1 6
Condition66%2/3
exists $data->{$rsrc_name} and exists $data->{$rsrc_name}{$id}
!ll&&!rl&&r
105
MC/DC50%1/2
exists $data->{$rsrc_name} and exists $data->{$rsrc_name}{$id}
exists $data->{$rsrc_name} exists $data->{$rsrc_name}{$id}
Truth table66%2/3
A and B
A= exists $data->{$rsrc_name}B= exists $data->{$rsrc_name}{$id}
ABresult
0X0
100
111
100 5 delete $data->{ $rsrc_name }->{ $id };
101 5 ▶ scalar keys %{ $data->{ $rsrc_name } } or delete $data->{ $rsrc_name };
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %{$$data{$rsrc_name};} or delete $data->{$rsrc_name} 0 5 5
102 5 $self->write_file( $path, $data );
103 5 return TRUE;
104 }
105  
106 1 $self->$_lock_reset( $path );
107 1 return FALSE;
108 }
109  
110 16 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.0
Pod documented
111 16 ▶ my ($self, $gd) = @_; $gd and return; $self->$_lock_reset_all(); return;
Branch: 1 branch
BranchTrueFalseTotal
$gd and return 0 16 16
112 }
113  
114 8 ▶sub dump {
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
115 8 my ($self, $path, $data) = @_;
116  
117 8 ▶ return $self->txn_do( $path, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
118 8 $self->$_lock_set( $path ); $self->write_file( $path, $data, TRUE ) } );
119 }
120  
121 6 ▶sub insert {
Subroutine insert: called CC=1 SCAR=0.0
Pod documented
122 6 my ($self, $path, $result) = @_;
123  
124 6 ▶ return $self->create_or_update( $path, $result, FALSE, sub { TRUE } );
Subroutine __ANON__: called CC=1 SCAR=0.0
125 }
126  
127 6 ▶sub load {
Subroutine load: called CC=6 SCAR=18.0
Pod documented
128 6 ▶ my ($self, @paths) = @_; $paths[ 0 ] or return {};
Branch: 1 branch
BranchTrueFalseTotal
$paths[0] or return {} 6 0 6
129  
130 6 ▶ scalar @paths == 1 and return ($self->read_file( $paths[ 0 ], FALSE ))[ 0 ];
Branch: 1 branch
BranchTrueFalseTotal
scalar @paths == 1 and return ($self->read_file($paths[0], 0))[0] 5 1 6
131  
132 1 my ($loaded, $meta, $newest) = $self->_cache->get_by_paths( \@paths );
133 1 my $cache_mtime = $self->meta_unpack( $meta );
134  
135 1 ▶ not is_stale $loaded, $cache_mtime, $newest and return $loaded;
Branch: 1 branch
BranchTrueFalseTotal
&is_stale($loaded, $cache_mtime, $newest) or return $loaded 1 0 1
136  
137 1 $loaded = {}; $newest = 0;
138  
139 1 for my $path (@paths) {
140 2 my ($red, $path_mtime) = $self->read_file( $path, FALSE );
141  
142 2 ▶ $path_mtime > $newest and $newest = $path_mtime;
Branch: 1 branch
BranchTrueFalseTotal
$path_mtime > $newest and $newest = $path_mtime 1 1 2
143 2 merge_file_data $loaded, $red;
144 }
145  
146 1 $self->_cache->set_by_paths( \@paths, $loaded, $self->meta_pack( $newest ) );
147 1 return $loaded;
148 }
149  
150 34 ▶sub meta_pack { # Modified in a subclass
Subroutine meta_pack: called CC=1 SCAR=0.0
Pod documented
151 34 my ($self, $mtime) = @_; return { mtime => $mtime };
152 }
153  
154 83 ▶sub meta_unpack { # Modified in a subclass
Subroutine meta_unpack: called CC=2 SCAR=6.9
Pod documented
155 83 ▶ my ($self, $attr) = @_; return $attr ? $attr->{mtime} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$attr ? : 73 10 83
156 }
157  
158 82 ▶sub read_file {
Subroutine read_file: called CC=2 SCAR=6.9
Pod documented
159 82 my ($self, $path, $for_update) = @_;
160  
161 82 $self->$_lock_set( $path ); my ($data, $path_mtime);
162  
163 82 ▶ try {
Subroutine __ANON__: called CC=5 SCAR=16.1
164 82 ▶ my $stat = $path->stat; defined $stat and $path_mtime = $stat->{mtime};
Branch: 1 branch
BranchTrueFalseTotal
defined $stat and $path_mtime = $stat->{"mtime"} 80 2 82
165  
166 82 my $meta; ($data, $meta) = $self->_cache->get( $path );
167  
168 82 my $cache_mtime = $self->meta_unpack( $meta );
169  
170 82 ▶ if (is_stale $data, $cache_mtime, $path_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_stale($data, $cache_mtime, $path_mtime)) { } 36 46 82
171 36 ▶ if ($for_update and not $path->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($for_update and not $path->exists) { } 0 36 36
Condition66%2/3
$for_update and not $path->exists
!ll&&!rl&&r
3060
MC/DC0%0/2
$for_update and not $path->exists
$for_update not $path->exists
Truth table66%2/3
A and not(B)
A= $for_updateB= not $path->exists
ABresult
0X0
100
111
172 0 $data = {}; # uncoverable statement
173 }
174 else {
175 36 $data = $self->read_from_file( $path->lock ); $path->close;
176 33 $meta = $self->meta_pack( $path_mtime );
177 33 $self->_cache->set( $path, $data, $meta );
178 33 $self->_log->debug( "Read file ${path}" );
179 }
180 }
181 46 else { $self->_log->debug( "Read cache ${path}" ) }
182 }
183 82 ▶ catch { $self->$_lock_reset( $path ); throw $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
184  
185 79 ▶ $for_update or $self->$_lock_reset( $path );
Branch: 1 branch
BranchTrueFalseTotal
$for_update or $self->$_lock_reset($path) 24 55 79
186  
187 79 return ($data, $path_mtime);
188 }
189  
190 1 ▶sub read_from_file {
Subroutine read_from_file: called CC=1 SCAR=0.0
Pod documented
191 1 throw 'Method [_1] not overridden in subclass [_2]',
192 [ 'read_from_file', blessed $_[ 0 ] ];
193 }
194  
195 43 ▶sub select {
Subroutine select: called CC=2 SCAR=6.9
Pod documented
196 43 my ($self, $path, $rsrc_name) = @_;
197  
198 43 $self->validate_params( $path, $rsrc_name );
199  
200 43 my $data = ($self->read_file( $path, FALSE ))[ 0 ];
201  
202 43 ▶ return exists $data->{ $rsrc_name } ? $data->{ $rsrc_name } : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $data->{$rsrc_name} ? : 38 5 43
203 }
204  
205 57 ▶sub txn_do {
Subroutine txn_do: called CC=2 SCAR=6.9
Pod documented
206 57 my ($self, $path, $code_ref) = @_;
207  
208 57 my $wantarray = wantarray; $self->validate_params( $path, TRUE );
209  
210 57 my $key = "txn:${path}"; $self->$_lock_set( $key ); my $res;
211  
212 57 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
213 57 ▶ if ($wantarray) { $res = [ $code_ref->() ] }
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 1 56 57
214 56 else { $res = $code_ref->() }
215 }
216 57 ▶ catch { $self->$_lock_reset( $key ); throw $_, { level => 4 } };
Subroutine __ANON__: called CC=1 SCAR=0.0
217  
218 52 $self->$_lock_reset( $key );
219  
220 52 ▶ return $wantarray ? @{ $res } : $res;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 51 52
221 }
222  
223 11 ▶sub update {
Subroutine update: called CC=4 SCAR=14.3
Pod documented
224 11 my ($self, $path, $result, $updating, $cond) = @_;
225  
226 11 ▶ $updating //= TRUE; $cond //= sub { TRUE };
Subroutine __ANON__: called CC=1 SCAR=0.5
Condition40%2/5
$updating //= 1
l!l
011
$cond //= sub { 1; }
l!l&&r!l&&!r
0110
MC/DC0%0/1
$updating //= 1
$updating
MC/DC0%0/2
$cond //= sub { 1; }
$cond sub { 1; }
Truth table50%1/2
A
A= $updating
Aresult
11
00
Truth table33%1/3
A //= B
A= $condB= sub { 1; }
ABresult
1X1
011
000
227  
228 11 ▶ my $updated = $self->create_or_update( $path, $result, $updating, $cond )
Branch: 1 branch
BranchTrueFalseTotal
my $updated = $self->create_or_update($path, $result, $updating, $cond) or &throw(NothingUpdated(), "level", 2) 10 1 11
229 or throw NothingUpdated, level => 2;
230  
231 10 return $updated;
232 }
233  
234 125 ▶sub validate_params {
Subroutine validate_params: called CC=4 SCAR=14.6
Pod documented
235 125 my ($self, $path, $rsrc_name) = @_;
236  
237 125 ▶ $path or throw Unspecified, [ 'path name' ], level => 2;
Branch: 1 branch
BranchTrueFalseTotal
$path or &throw(Unspecified(), ["path name"], "level", 2) 125 0 125
238 125 ▶ blessed $path or throw 'Path [_1] is not blessed', [ $path ], level => 2;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($path) or &throw("Path [_1] is not blessed", [$path], "level", 2) 125 0 125
239 125 ▶ $rsrc_name or throw 'Path [_1] result source not specified', [ $path ],
Branch: 1 branch
BranchTrueFalseTotal
$rsrc_name or &throw("Path [_1] result source not specified", [$path], "level", 2) 125 0 125
240 level => 2;
241  
242 125 return;
243 }
244  
245 28 ▶sub write_file {
Subroutine write_file: called CC=1 SCAR=0.0
Pod documented
246 28 my ($self, $path, $data, $create) = @_; my $exists = $path->exists;
247  
248 28 ▶ try {
Subroutine __ANON__: called CC=9 SCAR=22.3
249 28 ▶ $create or $exists or throw PathNotFound, [ $path ];
Branch: 1 branch
BranchTrueFalseTotal
$create or $exists or &throw(PathNotFound(), [$path]) 28 0 28
Condition66%2/3
$create or $exists
l!l&&r!l&&!r
12160
MC/DC0%0/2
$create or $exists
$create $exists
Truth table66%2/3
A or B
A= $createB= $exists
ABresult
1X1
011
000
250 28 ▶ $exists or $path->perms( $self->_perms );
Branch: 1 branch
BranchTrueFalseTotal
$exists or $path->perms($self->_perms) 21 7 28
251 28 ▶ $self->atomic_write and $path->atomic;
Branch: 1 branch
BranchTrueFalseTotal
$self->atomic_write and $path->atomic 28 0 28
252  
253 28 ▶ if ($exists and $self->backup and not $path->empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exists and $self->backup and not $path->empty) 3 25 28
Condition83%5/6
$exists and $self->backup and not $path->empty
!ll&&!rl&&r
2503
$exists and $self->backup
!ll&&!rl&&r
7183
MC/DC66%2/3
$exists and $self->backup and not $path->empty
$exists $self->backup not $path->empty
Truth table75%3/4
A and B and not(C)
A= $existsB= $self->backupC= not $path->empty
ABCresult
0XX0
10X0
1100
1111
254 3 ▶ copy( "${path}", $path.$self->backup )
Branch: 1 branch
BranchTrueFalseTotal
copy("$path", $path . $self->backup) or &throw("Backup copy failed: [_1]", [$File::DataClass::Storage::OS_ERROR]) 3 0 3
255 or throw 'Backup copy failed: [_1]', [ $OS_ERROR ];
256 }
257  
258 28 ▶ try { $data = $self->write_to_file( $path->lock, $data ); $path->close }
Subroutine __ANON__: called CC=1 SCAR=0.0
259 28 ▶ catch { $path->delete; throw $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
260  
261 27 $self->_cache->remove( $path );
262 27 $self->_log->debug( "Write file ${path}" )
263 }
264 28 ▶ catch { $self->$_lock_reset( $path ); throw $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
265  
266 27 $self->$_lock_reset( $path );
267 27 return $data;
268 }
269  
270 1 ▶sub write_to_file {
Subroutine write_to_file: called CC=1 SCAR=0.0
Pod documented
271 1 throw 'Method [_1] not overridden in subclass [_2]',
272 [ 'write_to_file', blessed $_[ 0 ] ];
273 }
274  
275 # Backcompat
276 1 ▶sub _read_file {
Subroutine _read_file: called CC=1 SCAR=0.0
277 1 throw 'Class [_1] should never call _read_file', [ blessed $_[ 0 ] ];
278 }
279  
280 1 ▶sub _write_file {
Subroutine _write_file: called CC=1 SCAR=0.0
281 1 throw 'Class [_1] should never call _write_file', [ blessed $_[ 0 ] ];
282 }
283  
284 4 1;
285  
286 __END__
« lib/File/DataClass/ResultSet.pm ↑ Summary lib/File/DataClass/Storage/Any.pm »