TER1 (Statement): 100.00%
TER2 (Branch): 100.00%
TER3 (LCSAJ): 100.0% (4/4)
Approximate LCSAJ segments: 25
โ Covered โ this LCSAJ path was executed during testing.
โ Not covered โ this LCSAJ path was never executed. These are the paths to focus on.
Multiple dots on a line indicate that multiple control-flow paths begin at that line. Hovering over any dot shows:
start โ end โ jump
Uncovered paths show [NOT COVERED] in the tooltip.
1: package Database::BI::Extract; 2: 3: use strict; 4: use warnings; 5: 6: our $VERSION = '0.025.0'; 7: 8: use Carp qw(croak carp); 9: use Digest::MD5 qw(md5_hex); 10: use Encode qw(encode_utf8); 11: use File::Path qw(make_path remove_tree); 12: use Mojo::File; 13: use Readonly; 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, ratherMutants (Total: 2, Killed: 2, Survived: 0)
32: # than in it: evict() removes every .db file it finds, and a CACHEDIR such as 33: # ~/.cache holds other programs' files. 34: 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
Mutants (Total: 2, Killed: 2, Survived: 0)
40: # database-bi-extracts folder. With none of those set they stay in 41: # <model_dir>/extracts, where they always were. 42: sub new { โ43 โ 57 โ 64 43: my ($class, %args) = @_; 44: 45: # dir names the folder outright, choosing nothing.
Mutants (Total: 2, Killed: 2, Survived: 0)
46: return bless { model_dir => $args{model_dir}, dir => $args{dir} }, $class 47: if defined($args{dir}) && length($args{dir}); 48: 49: require Database::BI::CacheDir; 50: my $cache_dir = Database::BI::CacheDir::resolve( 51: class => $class, 52: params => \%args, 53: (exists($args{config}) ? (config => $args{config}, home => $args{home}) : ()), 54: ); 55:
Mutants (Total: 2, Killed: 2, Survived: 0)
56: my $dir; 57: if (defined $cache_dir) { 58: $dir = Mojo::File->new($cache_dir)->child($CACHE_SUBDIR)->to_string; 59: } else { 60: croak 'model_dir is required' unless defined $args{model_dir} && length $args{model_dir}; 61: $dir = Mojo::File->new($args{model_dir})->child('extracts')->to_string; 62: } 63: 64: return bless { model_dir => $args{model_dir}, dir => $dir }, $class; 65: }
Mutants (Total: 2, Killed: 2, Survived: 0)
66: 67: # dir( $self ) -> $path_string 68: # 69: # The folder the extracts are kept in. It need not exist yet. 70: sub dir { 71: my $self = shift; 72: return $self->{dir}; 73: } 74: 75: # _fingerprint( $spec, $mtime, $size ) -> $hex_string 76: sub _fingerprint { 77: my ($spec, $mtime, $size) = @_;
Mutants (Total: 1, Killed: 1, Survived: 0)
78: $mtime //= 0; 79: $size //= 0; 80: return md5_hex(encode_utf8(join("\0", $spec, $mtime, $size))); 81: } 82: 83: # _extract_dir( $self ) -> $path_string 84: sub _extract_dir { 85: my $self = shift; 86: return $self->{dir};
Mutants (Total: 2, Killed: 2, Survived: 0)
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: sub path { 94: my ($self, $spec, $mtime, $size) = @_; 95: my $fp = _fingerprint($spec, $mtime, $size); 96: 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: sub get { 104: my ($self, $spec, $mtime, $size) = @_; 105: my $p = $self->path($spec, $mtime, $size);Mutants (Total: 2, Killed: 0, Survived: 2)
- BOOL_NEGATE_101_2: Negate boolean return expression
MEDIUM: Add tests asserting both true and false outcomes๐งช Suggested Test# Boolean branch test suggestion ok( !func(INPUT), 'Verify boolean branch behaviour' );- RETURN_UNDEF_101_2: Replace return expression with undef
LOW: Mutation survived, but impact may be minor๐งช Suggested Test# Return value assertion is( func(INPUT), EXPECTED, 'Verify correct return value' );106: return -f $p ? $p : undef; 107: } 108: 109: # put( $spec, $mtime, $size, $sqlite_bytes ) -> $path_stringMutants (Total: 1, Killed: 0, Survived: 1)
- COND_INV_105_3: Invert condition if to unless
MEDIUM: Add tests asserting both true and false outcomes110: # 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: sub put { โ114 โ 121 โ 126 114: my ($self, $spec, $mtime, $size, $bytes) = @_; 115: croak 'sqlite_bytes are required' unless defined $bytes && length $bytes; 116: 117: # A caller's $@ and $! are its own: the writes below use both on the way. 118: local ($@, $!); 119: 120: my $dir = $self->_extract_dir; 121: unless (-d $dir) { 122: eval { make_path($dir) }; 123: croak "Cannot create extract dir '$dir': $@" if $@; 124: } 125: โ126 โ 134 โ 139 126: 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: my $temp = "$p.$$.tmp"; 132: my $ok = eval { Mojo::File->new($temp)->spurt($bytes); rename $temp, $p or die "$!\n"; 1 }; 133: my $why = $@; 134: unless ($ok) { 135: unlink $temp; 136: croak "Cannot write extract '$p': $why"; 137: } 138: 139: 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: sub evict { โ152 โ 159 โ 165 152: my ($self, $spec) = @_; 153: my $dir = $self->_extract_dir; 154: return 0 unless -d $dir; 155: 156: 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: for my $f (grep { /\.db\z/ } @{ Mojo::File->new($dir)->list->map('to_string') }) { 160: if (unlink $f) { 161: $count++; 162: } 163: } 164: # A temporary file left by a process that was killed mid-write. 165: unlink grep { /\.tmp\z/ } @{ Mojo::File->new($dir)->list->map('to_string') }; 166: return $count; 167: } 168: 169: 1; 170: 171: __END__ 172: 173: =head1 NAME 174: 175: Database::BI::Extract -- SQLite materialisation cache for Database::BI 176: 177: =head1 SYNOPSIS 178: 179: use Database::BI::Extract; 180: 181: my $cache = Database::BI::Extract->new(model_dir => '.bi'); 182: 183: # Check cache 184: my $path = $cache->get($spec, $mtime, $size); 185: unless ($path) { 186: my $bytes = ...; # build the SQLite file 187: $path = $cache->put($spec, $mtime, $size, $bytes); 188: } 189: 190: =head1 DESCRIPTION 191: 192: Stores SQLite materialisation files (extracts) under C<model_dir/extracts/>, 193: or under F<database-bi-extracts/> in the cache directory when one is set (see 194: L</new>). 195: The cache key is an MD5 of the dataset spec, its source mtime, and its size. 196: Deleting the extracts directory leaves the application fully functional. 197: 198: The application reads the extract of an accelerated dataset through C<get>, 199: and C<script/bi-extract> builds one through C<put>; see 200: L<Database::BI/Accelerated datasets>. The caller decides when to build an 201: extract, and the class only stores and finds the bytes. 202: 203: =head1 METHODS 204: 205: =head2 new 206: 207: =head3 API SPECIFICATION 208: 209: =head4 INPUT 210: 211: { 212: model_dir => 'string', # extracts live in <model_dir>/extracts/ when no cache directory is set 213: dir => 'string', # optional; the folder itself, used as given: nothing below is read 214: cache_dir => 'string', # optional; read through Object::Configure (below) 215: config => 'hashref', # optional; the application's config (default: database_bi.conf is read) 216: home => 'string', # optional; the folder a relative cache_dir in config is under 217: } 218: 219: The cache directory is the first of these that is set (see 220: L<Database::BI::CacheDir>): C<cache_dir> through L<Object::Configure> (its 221: config files, then C<Database__BI__Extract__cache_dir>; the C<cache_dir> 222: argument is what those fall back on), C<$ENV{CACHEDIR}>, C<$ENV{CACHE_DIR}>, 223: C<cache_dir> in F<database_bi.conf>. The extracts are then kept in its 224: F<database-bi-extracts/> folder, never in it directly, because C<evict> removes 225: every C<.db> file in the folder it uses. 226: 227: =head4 OUTPUT 228: 229: A C<Database::BI::Extract> object. Croaks C<model_dir is required> when 230: neither C<model_dir> nor a cache directory is set. Nothing is created on disc 231: until C<put>. 232: 233: =head2 dir 234: 235: =head3 API SPECIFICATION 236: 237: =head4 INPUT 238: 239: None. 240: 241: =head4 OUTPUT 242: 243: 'string' # the folder the extracts are kept in; it need not exist 244: 245: =head2 path 246: 247: =head3 API SPECIFICATION 248: 249: =head4 INPUT 250: 251: $spec # the dataset spec, e.g. 'table:sales' 252: $mtime # source modification time (undef is 0) 253: $size # source size in bytes (undef is 0) 254: 255: =head4 OUTPUT 256: 257: 'string' # <dir>/<md5 of spec, mtime and size>.db 258: 259: The path is computed, not checked: the file need not exist. 260: 261: =head2 get 262: 263: =head3 API SPECIFICATION 264: 265: =head4 INPUT 266: 267: The same three arguments as C<path>. 268: 269: =head4 OUTPUT 270: 271: 'string' | undef # the path when that file exists, otherwise undef 272: 273: A changed mtime or size gives a different path, so a stale extract is a miss. 274: 275: =head2 put 276: 277: =head3 API SPECIFICATION 278: 279: =head4 INPUT 280: 281: $spec, $mtime, $size # as for path 282: $bytes # required; the SQLite file's contents 283: 284: =head4 OUTPUT 285: 286: 'string' # the path written 287: 288: Creates C<dir> when it is missing. Croaks C<sqlite_bytes are required> 289: when C<$bytes> is undefined or empty, and croaks naming the path when the 290: directory or file cannot be written. 291: 292: =head2 evict 293: 294: =head3 API SPECIFICATION 295: 296: =head4 INPUT 297: 298: $spec # accepted but not used: see below 299: 300: =head4 OUTPUT 301: 302: 'integer' # the number of files removed (0 when there is no dir) 303: 304: A fingerprint cannot be traced back to its spec, so C<evict> removes every 305: C<.db> file in C<dir>, not only C<$spec>'s. 306: 307: =head1 AUTHOR 308: 309: Nigel Horne C<< <njh@nigelhorne.com> >> 310: 311: =head1 LICENCE AND COPYRIGHT 312: 313: Copyright 2026 Nigel Horne. 314: Usage is subject to the GPL2 licence terms. 315: 316: =cutMutants (Total: 2, Killed: 0, Survived: 2)
- BOOL_NEGATE_109_2: Negate boolean return expression
MEDIUM: Add tests asserting both true and false outcomes๐งช Suggested Test# Boolean branch test suggestion ok( !func(INPUT), 'Verify boolean branch behaviour' );- RETURN_UNDEF_109_2: Replace return expression with undef
LOW: Mutation survived, but impact may be minor๐งช Suggested Test# Return value assertion is( func(INPUT), EXPECTED, 'Verify correct return value' );