blib/lib/Database/BI/Extract.pm

filter: Statementstmt 100% 83 / 83 Branchbran 100% 24 / 24 Conditioncond 100% 13 / 13 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 16 / 16 Podpod 100% 6 / 6 totaltotal 100% 150 / 150 CC 19 SCAR 14.0
CC
19
Cov
100%
new
7.0
put
7.0
evict
4.0
CRAP 4.1
« blib/lib/Database/BI/Password.pm ↑ Summary blib/lib/Database/BI/I18N.pm »
1 package Database::BI::Extract;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.024.0';
7  
8 4 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # ---------------------------------------------------------------------------
16 # An extract is a SQLite materialisation of one dataset, stored at:
17 # <model_dir>/extracts/<fingerprint>.db
18 # or, when a cache directory is set (see new), at:
19 # <cache_dir>/database-bi-extracts/<fingerprint>.db
20 #
21 # The fingerprint is md5_hex(spec . "\0" . mtime . "\0" . size).
22 # It is a pure cache: deleting the extracts/ directory at any time leaves
23 # the application fully functional. No file under data_dir is created.
24 #
25 # The web application reads an accelerated dataset's extract through get(), and
26 # script/bi-extract builds one through put() (Dashboard::accelerate). A caller
27 # decides whether to build a new extract; Extract only stores and retrieves
28 # bytes.
29 # ---------------------------------------------------------------------------
30  
31 # The folder extracts are kept in under a cache directory that was set, rather
32 # than in it: evict() removes every .db file it finds, and a CACHEDIR such as
33 # ~/.cache holds other programs' files.
34 4 Readonly my $CACHE_SUBDIR => 'database-bi-extracts';
35  
36 # new( model_dir => $dir, cache_dir => $dir, config => \%conf, home => $dir, dir => $dir ) -> $self
37 #
38 # Database::BI::CacheDir chooses the cache directory (Object::Configure,
39 # CACHEDIR, CACHE_DIR, cache_dir in database_bi.conf); the extracts go in its
40 # database-bi-extracts folder. With none of those set they stay in
41 # <model_dir>/extracts, where they always were.
42 36 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
43 36 my ($class, %args) = @_;
44  
45 # dir names the folder outright, choosing nothing.
46 return bless { model_dir => $args{model_dir}, dir => $args{dir} }, $class
47 36 ▶ if defined($args{dir}) && length($args{dir});
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"dir"} and length $args{"dir"} 3 33 36
Condition100%3/3
defined $args{"dir"} and length $args{"dir"}
!ll&&!rl&&r
3213
MC/DC100%2/2
defined $args{"dir"} and length $args{"dir"}
defined $args{"dir"} length $args{"dir"}
Truth table100%3/3
A and B
A= defined $args{"dir"}B= length $args{"dir"}
ABresult
0X0
100
111
48  
49 33 require Database::BI::CacheDir;
50 my $cache_dir = Database::BI::CacheDir::resolve(
51 class => $class,
52 params => \%args,
53 33 ▶ (exists($args{config}) ? (config => $args{config}, home => $args{home}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'config'} ? : 10 23 33
54 );
55  
56 33 my $dir;
57 33 ▶ if (defined $cache_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache_dir) { } 1 32 33
58 1 $dir = Mojo::File->new($cache_dir)->child($CACHE_SUBDIR)->to_string;
59 } else {
60 32 ▶ croak 'model_dir is required' unless defined $args{model_dir} && length $args{model_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"model_dir"} and length $args{"model_dir"} 6 26 32
Condition100%3/3
defined $args{"model_dir"} and length $args{"model_dir"}
!ll&&!rl&&r
4226
MC/DC100%2/2
defined $args{"model_dir"} and length $args{"model_dir"}
defined $args{"model_dir"} length $args{"model_dir"}
Truth table100%3/3
A and B
A= defined $args{"model_dir"}B= length $args{"model_dir"}
ABresult
0X0
100
111
61 26 $dir = Mojo::File->new($args{model_dir})->child('extracts')->to_string;
62 }
63  
64 27 return bless { model_dir => $args{model_dir}, dir => $dir }, $class;
65 }
66  
67 # dir( $self ) -> $path_string
68 #
69 # The folder the extracts are kept in. It need not exist yet.
70 8 ▶sub dir {
Subroutine dir: called CC=1 SCAR=0.0
Pod documented
71 8 my $self = shift;
72 8 return $self->{dir};
73 }
74  
75 # _fingerprint( $spec, $mtime, $size ) -> $hex_string
76 51 ▶sub _fingerprint {
Subroutine _fingerprint: called CC=3 SCAR=11.0
77 51 my ($spec, $mtime, $size) = @_;
78 51 ▶ $mtime //= 0;
Condition100%2/2
$mtime //= 0
l!l
492
MC/DC100%1/1
$mtime //= 0
$mtime
Truth table100%2/2
A
A= $mtime
Aresult
11
00
79 51 ▶ $size //= 0;
Condition100%2/2
$size //= 0
l!l
492
MC/DC100%1/1
$size //= 0
$size
Truth table100%2/2
A
A= $size
Aresult
11
00
80 51 return md5_hex(encode_utf8(join("\0", $spec, $mtime, $size)));
81 }
82  
83 # _extract_dir( $self ) -> $path_string
84 81 ▶sub _extract_dir {
Subroutine _extract_dir: called CC=1 SCAR=0.0
85 81 my $self = shift;
86 81 return $self->{dir};
87 }
88  
89 # path( $spec, $mtime, $size ) -> $path_string
90 #
91 # Returns the filesystem path an extract for this fingerprint would occupy.
92 # Does not test for existence.
93 41 ▶sub path {
Subroutine path: called CC=1 SCAR=0.0
Pod documented
94 41 my ($self, $spec, $mtime, $size) = @_;
95 41 my $fp = _fingerprint($spec, $mtime, $size);
96 41 return Mojo::File->new($self->_extract_dir)->child("$fp.db")->to_string;
97 }
98  
99 # get( $spec, $mtime, $size ) -> $path_string | undef
100 #
101 # Returns the path to the cached SQLite file when the fingerprint matches and
102 # the file exists on disk; returns undef on a cache miss.
103 19 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
104 19 my ($self, $spec, $mtime, $size) = @_;
105 19 my $p = $self->path($spec, $mtime, $size);
106 19 ▶ return -f $p ? $p : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $p ? : 5 14 19
107 }
108  
109 # put( $spec, $mtime, $size, $sqlite_bytes ) -> $path_string
110 #
111 # Writes $sqlite_bytes to the cache. Creates the extracts/ directory if it
112 # does not exist yet. Returns the path written to. Croaks on I/O failure.
113 23 ▶sub put {
Subroutine put: called CC=7 SCAR=19.5
Pod documented
114 23 my ($self, $spec, $mtime, $size, $bytes) = @_;
115 23 ▶ croak 'sqlite_bytes are required' unless defined $bytes && length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and length $bytes 4 19 23
Condition100%3/3
defined $bytes and length $bytes
!ll&&!rl&&r
2219
MC/DC100%2/2
defined $bytes and length $bytes
defined $bytes length $bytes
Truth table100%3/3
A and B
A= defined $bytesB= length $bytes
ABresult
0X0
100
111
116  
117 # A caller's $@ and $! are its own: the writes below use both on the way.
118 19 local ($@, $!);
119  
120 19 my $dir = $self->_extract_dir;
121 19 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 15 4 19
122 15 eval { make_path($dir) };
123 15 ▶ croak "Cannot create extract dir '$dir': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 12 15
124 }
125  
126 16 my $p = $self->path($spec, $mtime, $size);
127 # Premise 1: get() trusts any plain file at $p. Premise 2: a write that
128 # fails half way (disk full, a killed process) leaves a short file where it
129 # wrote. Conclusion: write beside it and rename, so $p is the whole extract
130 # or what it was before, and a failure removes its own temporary file.
131 16 my $temp = "$p.$$.tmp";
132 16 ▶ my $ok = eval { Mojo::File->new($temp)->spurt($bytes); rename $temp, $p or die "$!\n"; 1 };
Branch: 1 branch
BranchTrueFalseTotal
rename $temp, $p or die "$!\n" 15 1 16
133 16 my $why = $@;
134 16 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 15 16
135 1 unlink $temp;
136 1 croak "Cannot write extract '$p': $why";
137 }
138  
139 15 return $p;
140 }
141  
142 # evict( $spec ) -> $count_removed
143 #
144 # Removes all cached extract files that begin with a fingerprint that could
145 # belong to $spec. Because the fingerprint includes mtime+size, a changed
146 # file naturally produces a miss without eviction; evict() is provided for
147 # explicit cache clearing. Returns the number of files removed.
148 # NOTE: because fingerprints are global md5 hashes we cannot identify which
149 # belong to a spec without re-computing; this method removes ALL extracts.
150 # Call it only when clearing the whole extract store is desired.
151 14 ▶sub evict {
Subroutine evict: called CC=4 SCAR=13.9
Pod documented
152 14 my ($self, $spec) = @_;
153 14 my $dir = $self->_extract_dir;
154 14 ▶ return 0 unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 4 10 14
155  
156 10 my $count = 0;
157 # Listed rather than globbed: glob() splits its pattern on spaces and reads
158 # brackets as a class, so the name of the directory could defeat it.
159 10 for my $f (grep { /\.db\z/ } @{ Mojo::File->new($dir)->list->map('to_string') }) {
160 13 ▶ if (unlink $f) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $f) 12 1 13
161 12 $count++;
162 }
163 }
164 # A temporary file left by a process that was killed mid-write.
165 10 unlink grep { /\.tmp\z/ } @{ Mojo::File->new($dir)->list->map('to_string') };
166 10 return $count;
167 }
168  
169 4 1;
170  
171 __END__
« blib/lib/Database/BI/Password.pm ↑ Summary blib/lib/Database/BI/I18N.pm »