blib/lib/Database/BI/Extract.pm

filter: Statementstmt 98.6% 73 / 74 Branchbran 86.3% 19 / 22 Conditioncond 84.6% 11 / 13 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 16 / 16 Podpod 100% 6 / 6 totaltotal 94.2% 131 / 139 CC 18 SCAR 13.6
CC
18
Cov
94%
new
7.4
put
6.0
evict
4.0
CRAP 3.9
« blib/lib/Database/BI/I18N.pm ↑ Summary blib/lib/Database/BI/Util.pm »
1 package Database::BI::Extract;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.023.1';
7  
8 1 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶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 1 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 18 ▶sub new {
Subroutine new: called CC=7 SCAR=20.1
Pod documented
43 18 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 18 ▶ if defined($args{dir}) && length($args{dir});
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"dir"} and length $args{"dir"} 0 18 18
Condition33%1/3
defined $args{"dir"} and length $args{"dir"}
!ll&&!rl&&r
1800
MC/DC0%0/2
defined $args{"dir"} and length $args{"dir"}
defined $args{"dir"} length $args{"dir"}
Truth table33%1/3
A and B
A= defined $args{"dir"}B= length $args{"dir"}
ABresult
0X0
100
111
48  
49 18 require Database::BI::CacheDir;
50 my $cache_dir = Database::BI::CacheDir::resolve(
51 class => $class,
52 params => \%args,
53 18 ▶ (exists($args{config}) ? (config => $args{config}, home => $args{home}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'config'} ? : 4 14 18
54 );
55  
56 18 my $dir;
57 18 ▶ if (defined $cache_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache_dir) { } 0 18 18
58 0 $dir = Mojo::File->new($cache_dir)->child($CACHE_SUBDIR)->to_string;
59 } else {
60 18 ▶ 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"} 3 15 18
Condition100%3/3
defined $args{"model_dir"} and length $args{"model_dir"}
!ll&&!rl&&r
2115
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 15 $dir = Mojo::File->new($args{model_dir})->child('extracts')->to_string;
62 }
63  
64 15 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 5 ▶sub dir {
Subroutine dir: called CC=1 SCAR=0.0
Pod documented
71 5 my $self = shift;
72 5 return $self->{dir};
73 }
74  
75 # _fingerprint( $spec, $mtime, $size ) -> $hex_string
76 32 ▶sub _fingerprint {
Subroutine _fingerprint: called CC=3 SCAR=11.0
77 32 my ($spec, $mtime, $size) = @_;
78 32 ▶ $mtime //= 0;
Condition100%2/2
$mtime //= 0
l!l
311
MC/DC100%1/1
$mtime //= 0
$mtime
Truth table100%2/2
A
A= $mtime
Aresult
11
00
79 32 ▶ $size //= 0;
Condition100%2/2
$size //= 0
l!l
311
MC/DC100%1/1
$size //= 0
$size
Truth table100%2/2
A
A= $size
Aresult
11
00
80 32 return md5_hex(encode_utf8(join("\0", $spec, $mtime, $size)));
81 }
82  
83 # _extract_dir( $self ) -> $path_string
84 44 ▶sub _extract_dir {
Subroutine _extract_dir: called CC=1 SCAR=0.0
85 44 my $self = shift;
86 44 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 22 ▶sub path {
Subroutine path: called CC=1 SCAR=0.0
Pod documented
94 22 my ($self, $spec, $mtime, $size) = @_;
95 22 my $fp = _fingerprint($spec, $mtime, $size);
96 22 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 9 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
104 9 my ($self, $spec, $mtime, $size) = @_;
105 9 my $p = $self->path($spec, $mtime, $size);
106 9 ▶ return -f $p ? $p : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $p ? : 2 7 9
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 13 ▶sub put {
Subroutine put: called CC=6 SCAR=17.9
Pod documented
114 13 my ($self, $spec, $mtime, $size, $bytes) = @_;
115 13 ▶ croak 'sqlite_bytes are required' unless defined $bytes && length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and length $bytes 2 11 13
Condition100%3/3
defined $bytes and length $bytes
!ll&&!rl&&r
1111
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 11 local ($@, $!);
119  
120 11 my $dir = $self->_extract_dir;
121 11 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 8 3 11
122 8 eval { make_path($dir) };
123 8 ▶ croak "Cannot create extract dir '$dir': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 7 8
124 }
125  
126 10 my $p = $self->path($spec, $mtime, $size);
127 10 eval { Mojo::File->new($p)->spurt($bytes) };
128 10 ▶ croak "Cannot write extract '$p': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 9 10
129  
130 9 return $p;
131 }
132  
133 # evict( $spec ) -> $count_removed
134 #
135 # Removes all cached extract files that begin with a fingerprint that could
136 # belong to $spec. Because the fingerprint includes mtime+size, a changed
137 # file naturally produces a miss without eviction; evict() is provided for
138 # explicit cache clearing. Returns the number of files removed.
139 # NOTE: because fingerprints are global md5 hashes we cannot identify which
140 # belong to a spec without re-computing; this method removes ALL extracts.
141 # Call it only when clearing the whole extract store is desired.
142 7 ▶sub evict {
Subroutine evict: called CC=4 SCAR=13.9
Pod documented
143 7 my ($self, $spec) = @_;
144 7 my $dir = $self->_extract_dir;
145 7 ▶ return 0 unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 6 7
146  
147 6 my $count = 0;
148 # Listed rather than globbed: glob() splits its pattern on spaces and reads
149 # brackets as a class, so the name of the directory could defeat it.
150 6 for my $f (grep { /\.db\z/ } @{ Mojo::File->new($dir)->list->map('to_string') }) {
151 6 ▶ if (unlink $f) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $f) 6 0 6
152 6 $count++;
153 }
154 }
155 6 return $count;
156 }
157  
158 1 1;
159  
160 __END__
« blib/lib/Database/BI/I18N.pm ↑ Summary blib/lib/Database/BI/Util.pm »