CHI/Driver/File.pm

filter: Statementstmt 96.7% 147 / 152 Branchbran 76.1% 32 / 42 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 33 / 33 Podpod 9.0% 1 / 11 totaltotal 87.9% 227 / 258 CC 27 SCAR 11.8
CC
27
Cov
91%
store
9.0
path_to_key
6.0
__ANON__
3.0
CRAP 3.3
« CHI/t/Driver/FastMmap.pm ↑ Summary CHI/Driver/FastMmap.pm »
1 package CHI::Driver::File;
2 9 $CHI::Driver::File::VERSION = '0.60';
3 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Cwd qw(realpath cwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use CHI::Util
6 9 ▶ qw(fast_catdir fast_catfile unique_id read_dir read_file write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Digest::JHash qw(jhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use File::Path qw(mkpath rmtree);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use File::Spec::Functions qw(catdir catfile splitdir tmpdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 extends 'CHI::Driver';
19  
20 403 ▶has '+max_key_length' => ( default => sub { 248 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
21 198 ▶has 'depth' => ( is => 'ro', isa => Int, default => sub { 2 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
22 405 ▶has 'dir_create_mode' => ( is => 'ro', isa => Int, default => sub { oct(775) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23 405 ▶has 'file_create_mode' => ( is => 'ro', isa => Int, default => sub { oct(666) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
24 405 ▶has 'file_extension' => ( is => 'ro', isa => Str, default => sub { '.dat' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
25 9 has 'path_to_namespace' => ( is => 'lazy' );
26 2 ▶has 'root_dir' => ( is => 'ro', isa => Str, default => sub { catdir( tmpdir(), 'chi-driver-file' ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
27  
28 sub BUILDARGS {
29 405 ▶ my ( $class, %params ) = @_;
Subroutine BUILDARGS: called CC=2 SCAR=8.4
Pod undocumented
30  
31 # Backward compat
32 #
33 405 ▶ if ( defined( $params{key_digest} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"key_digest"}) 0 405 405
34 0 $params{key_digester} = $params{key_digest};
35 0 $params{max_key_length} = 0;
36 }
37  
38 405 return \%params;
39 }
40  
41 sub _build_path_to_namespace {
42 378 ▶ my $self = shift;
Subroutine _build_path_to_namespace: called CC=2 SCAR=7.0
43  
44 378 my $namespace = $self->escape_for_filename( $self->namespace );
45 378 ▶ $namespace = $self->digest_key($namespace)
Branch: 1 branch
BranchTrueFalseTotal
if length $namespace > $self->max_key_length 0 378 378
46 if length($namespace) > $self->max_key_length;
47 378 return catdir( $self->root_dir, $namespace );
48 }
49  
50 # Escape key to make safe for filesystem; if it then grows larger than
51 # max_key_length, digest it.
52 #
53 sub escape_key {
54 9666 ▶ my ( $self, $key ) = @_;
Subroutine escape_key: called CC=3 SCAR=11.0
Pod undocumented
55  
56 9666 my $new_key = $self->escape_for_filename($key);
57 9666 ▶ if ( length($new_key) > length($key)
Branch: 1 branch
BranchTrueFalseTotal
if (length $new_key > length $key and length $new_key > $self->max_key_length) 97 9569 9666
Condition100%3/3
length $new_key > length $key and length $new_key > $self->max_key_length
!ll&&!rl&&r
7490207997
MC/DC100%2/2
length $new_key > length $key and length $new_key > $self->max_key_length
length $new_key > length $key length $new_key > $self->max_key_length
Truth table100%3/3
A and B
A= length $new_key > length $keyB= length $new_key > $self->max_key_length
ABresult
0X0
100
111
58 && length($new_key) > $self->max_key_length() )
59 {
60 97 $new_key = $self->digest_key($new_key);
61 }
62 9666 return $new_key;
63 }
64  
65 sub unescape_key {
66 2850 ▶ my ( $self, $key ) = @_;
Subroutine unescape_key: called CC=1 SCAR=0.0
Pod undocumented
67  
68 2850 return $self->unescape_for_filename($key);
69 }
70  
71 sub fetch {
72 6495 ▶ my ( $self, $key ) = @_;
Subroutine fetch: called CC=3 SCAR=11.0
Pod undocumented
73  
74 6495 my $file = $self->path_to_key($key);
75 6495 ▶ if ( defined $file && -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and -f $file) { } 5406 1089 6495
Condition66%2/3
defined $file and -f $file
!ll&&!rl&&r
010895406
MC/DC50%1/2
defined $file and -f $file
defined $file -f $file
Truth table66%2/3
A and B
A= defined $fileB= -f $file
ABresult
0X0
100
111
76 5406 return read_file($file);
77 }
78 else {
79 1089 return undef;
80 }
81 }
82  
83 sub store {
84 2228 ▶ my ( $self, $key, $data ) = @_;
Subroutine store: called CC=8 SCAR=22.0
Pod undocumented
85  
86 2228 my $dir;
87 2228 ▶ my $file = $self->path_to_key( $key, \$dir ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->path_to_key($key, \$dir) or return undef 0 2228 2228
88  
89 2228 ▶ mkpath( $dir, 0, $self->{dir_create_mode} ) if !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 997 1231 2228
90  
91 # Possibly generate a temporary file - if generate_temporary_filename returns undef,
92 # store to the destination file directly
93 #
94 2228 my $temp_file = $self->generate_temporary_filename( $dir, $file );
95 2228 ▶ my $store_file = defined($temp_file) ? $temp_file : $file;
Branch: 1 branch
BranchTrueFalseTotal
defined $temp_file ? : 2226 2 2228
96  
97 2228 write_file( $store_file, $data, $self->{file_create_mode} );
98  
99 2226 ▶ if ( defined($temp_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $temp_file) 2224 2 2226
100  
101 # Rename can fail in rare race conditions...try multiple times
102 #
103 2224 ▶ for ( my $try = 0 ; $try < 3 ; $try++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try < 3) 2224 0 2224
104 2224 ▶ last if ( rename( $temp_file, $file ) );
Branch: 1 branch
BranchTrueFalseTotal
if rename $temp_file, $file 2224 0 2224
105 }
106 2224 ▶ if ( -f $temp_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $temp_file) 0 2224 2224
107 0 my $error = $!;
108 0 unlink($temp_file);
109 0 die "could not rename '$temp_file' to '$file': $error";
110 }
111 }
112 }
113  
114 sub remove {
115 536 ▶ my ( $self, $key ) = @_;
Subroutine remove: called CC=2 SCAR=7.1
Pod undocumented
116  
117 536 ▶ my $file = $self->path_to_key($key) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->path_to_key($key) or return undef 0 536 536
118 536 unlink($file);
119 }
120  
121 sub clear {
122 460 ▶ my ($self) = @_;
Subroutine clear: called CC=3 SCAR=11.0
Pod undocumented
123  
124 460 my $namespace_dir = $self->path_to_namespace;
125 460 ▶ return if !-d $namespace_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $namespace_dir 159 301 460
126 301 my $renamed_dir = join( ".", $namespace_dir, unique_id() );
127 301 rename( $namespace_dir, $renamed_dir );
128 301 rmtree($renamed_dir);
129 301 ▶ die "could not remove '$renamed_dir'"
Branch: 1 branch
BranchTrueFalseTotal
if -d $renamed_dir 0 301 301
130 if -d $renamed_dir;
131 }
132  
133 sub get_keys {
134 492 ▶ my ($self) = @_;
Subroutine get_keys: called CC=1 SCAR=0.0
Pod undocumented
135  
136 492 my @filepaths;
137 492 my $re = quotemeta( $self->file_extension );
138 492 ▶ my $wanted = sub { push( @filepaths, $_ ) if -f && /${re}$/ };
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ and /$re$/ 2446 5427 7873
Condition66%2/3
-f $_ and /$re$/
!ll&&!rl&&r
542702446
MC/DC50%1/2
-f $_ and /$re$/
-f $_ /$re$/
Truth table66%2/3
A and B
A= -f $_B= /$re$/
ABresult
0X0
100
111
139 492 my @keys = $self->_collect_keys_via_file_find( \@filepaths, $wanted );
140 492 return @keys;
141 }
142  
143 sub _collect_keys_via_file_find {
144 492 ▶ my ( $self, $filepaths, $wanted ) = @_;
Subroutine _collect_keys_via_file_find: called CC=3 SCAR=11.0
145  
146 492 my $namespace_dir = $self->path_to_namespace;
147 492 ▶ return () if !-d $namespace_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $namespace_dir 24 468 492
148  
149 468 find( { wanted => $wanted, no_chdir => 1 }, $namespace_dir );
150  
151 468 my @keys;
152 468 my $key_start = length($namespace_dir) + 1 + $self->depth * 2;
153 468 my $subtract = -1 * length( $self->file_extension );
154 468 foreach my $filepath (@$filepaths) {
155 2446 my $key = substr( $filepath, $key_start, $subtract );
156 2446 $key = $self->unescape_key( join( "", splitdir($key) ) );
157 2446 push( @keys, $key );
158 }
159 468 return @keys;
160 }
161  
162 sub generate_temporary_filename {
163 2224 ▶ my ( $self, $dir, $file ) = @_;
Subroutine generate_temporary_filename: called CC=1 SCAR=0.0
Pod undocumented
164  
165 # Generate a temporary filename using unique_id - faster than tempfile, as long as
166 # we don't need automatic removal.
167 # Note: $file not used here, but might be used in an override.
168 #
169 2224 return fast_catfile( $dir, unique_id() );
170 }
171  
172 sub get_namespaces {
173 46 ▶ my ($self) = @_;
Subroutine get_namespaces: called CC=2 SCAR=6.9
Pod undocumented
174  
175 46 my $root_dir = $self->root_dir();
176 46 ▶ return () if !-d $root_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $root_dir 0 46 46
177 46 my @contents = read_dir($root_dir);
178 my @namespaces =
179 182 map { $self->unescape_for_filename($_) }
180 186 grep { $self->is_escaped_for_filename($_) }
181 46 grep { -d fast_catdir( $root_dir, $_ ) } @contents;
182 46 return @namespaces;
183 }
184  
185 9 my %hex_strings = map { ( $_, sprintf( "%x", $_ ) ) } ( 0x0 .. 0xf );
186  
187 sub path_to_key {
188 9262 ▶ my ( $self, $key, $dir_ref ) = @_;
Subroutine path_to_key: called CC=6 SCAR=17.9
Pod documented
189 9262 ▶ return undef if !defined($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 9262 9262
190  
191 9262 my @paths = ( $self->path_to_namespace );
192  
193 9262 my $orig_key = $key;
194 9262 $key = $self->escape_key($key);
195  
196 # Hack: If key is exactly 32 hex chars, assume it's an md5 digest and
197 # take a prefix of it for bucket. Digesting will usually happen in
198 # transform_key and there's no good way for us to know it occurred.
199 #
200 9262 ▶ if ( $key =~ /^[0-9a-f]{32}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^[0-9a-f]{32}$/) { } 159 9103 9262
201 push( @paths,
202 159 map { substr( $key, $_, 1 ) } ( 0 .. $self->{depth} - 1 ) );
203 }
204 else {
205  
206 # Hash key to a 32-bit integer (using non-escaped key for back compat)
207 #
208 9103 my $bucket = jhash($orig_key);
209  
210 # Create $self->{depth} subdirectories, containing a maximum of 64
211 # subdirectories each, by successively shifting 4 bits off the
212 # bucket and converting to hex.
213 #
214 9103 ▶ for ( my $d = $self->{depth} ; $d > 0 ; $d-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d > 0) 14154 9103 23257
215 14154 push( @paths, $hex_strings{ $bucket & 0xf } );
216 14154 $bucket >>= 4;
217 }
218 }
219  
220 # Join paths together, computing dir separately if $dir_ref was passed.
221 #
222 9262 my $filename = $key . $self->file_extension;
223 9262 my $filepath;
224 9262 ▶ if ( defined $dir_ref && ref($dir_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dir_ref and ref $dir_ref) { } 2228 7034 9262
Condition66%2/3
defined $dir_ref and ref $dir_ref
!ll&&!rl&&r
703402228
MC/DC50%1/2
defined $dir_ref and ref $dir_ref
defined $dir_ref ref $dir_ref
Truth table66%2/3
A and B
A= defined $dir_refB= ref $dir_ref
ABresult
0X0
100
111
225 2228 my $dir = fast_catdir(@paths);
226 2228 $filepath = fast_catfile( $dir, $filename );
227 2228 $$dir_ref = $dir;
228 }
229 else {
230 7034 $filepath = fast_catfile( @paths, $filename );
231 }
232  
233 9262 return $filepath;
234 }
235  
236 9 1;
237  
238 __END__
« CHI/t/Driver/FastMmap.pm ↑ Summary CHI/Driver/FastMmap.pm »