| 1 | package MS::Reader; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 11 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 11 | ▶ | use Digest::MD5; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 11 | ▶ | use File::stat; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 11 | ▶ | use Storable qw/nstore_fd retrieve_fd/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 11 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 11 | ▶ | use PerlIO::gzip; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | 11 | ▶ | use MS; # because we need access to the package version | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 11 | ▶ | use constant BGZF_MAGIC => pack 'H*', '1f8b0804'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 3 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 3 | my ($class, $fn, %args) = @_; | |||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 3 | my $self = bless {} => $class; | |||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | 3 | ▶ | $self->{__use_cache} = $args{use_cache} ? 1 : 0; # remember accessed records | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 23 | 3 | ▶ | $self->{__paranoid} = $args{paranoid} ? 1 : 0; # calc MD5 each time | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 24 | 3 | $self->{__fn} = undef; # to allow dunlock even if not loaded | |||||||||||||||||
| 25 | 3 | $self->{__fh} = undef; # to allow dunlock even if not loaded | |||||||||||||||||
| 26 | 3 | $self->{__version} = $MS::VERSION; | |||||||||||||||||
| 27 | 3 | ▶ | $self->{__lock} = $args{lock} // 0; | ||||||||||||||||
|
Condition50%1/2
$args{"lock"} // 0
MC/DC0%0/1
$args{"lock"} // 0
$args{"lock"}
Truth table50%1/2
A
A= $args{"lock"}
| |||||||||||||||||||
| 28 | 3 | $self->{__app_data} = {}; | |||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | 3 | ▶ | $self->load($fn) if (defined $fn); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | # check expected methods in subclasses | ||||||||||||||||||
| 33 | 3 | $self->_check_interface; | |||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | 3 | return $self; | |||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | } | ||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | # to be defined by subclass | ||||||||||||||||||
| 40 | 3 | ▶ | sub _check_interface { return } | ||||||||||||||||
|
Subroutine
_check_interface: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | 49 | ▶ | sub _read_element { | ||||||||||||||||
|
Subroutine
_read_element: called CC=2 SCAR=7.0
| |||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | 49 | my ($self, $offset, $to_read) = @_; | |||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | 49 | seek $self->{__fh}, $offset, 0; | |||||||||||||||||
| 47 | 49 | my $r = read($self->{__fh}, my $el, $to_read); | |||||||||||||||||
| 48 | 49 | ▶ | croak "returned unexpected byte count" if ($r != $to_read); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | 49 | return $el; | |||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | } | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | 3 | ▶ | sub load { | ||||||||||||||||
|
Subroutine
load: called CC=14 SCAR=37.9
Pod
documented
| |||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | 3 | my ($self, $fn) = @_; | |||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | 3 | my $use_cache = $self->{__use_cache}; | |||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 3 | ▶ | croak "input file not found" if (! -e $fn); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 61 | 3 | $self->{__fn} = $fn; | |||||||||||||||||
| 62 | 3 | open my $fh, '<', $fn; | |||||||||||||||||
| 63 | 3 | my $old_layers = join '', map {":$_"} PerlIO::get_layers($fh); | |||||||||||||||||
| 64 | 3 | binmode $fh; | |||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | # check for BGZIP compression | ||||||||||||||||||
| 67 | 3 | read($fh, my $magic, 4); | |||||||||||||||||
| 68 | 3 | binmode($fh, $old_layers); | |||||||||||||||||
| 69 | 3 | ▶ | if ($magic eq BGZF_MAGIC) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 70 | require Compress::BGZF::Reader | ||||||||||||||||||
| 71 | 3 | ▶ | or croak "Compress::BGZF::Reader required to handle BGZF files"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 72 | 3 | close $fh; | |||||||||||||||||
| 73 | 3 | $fh = Compress::BGZF::Reader->new_filehandle($fn); | |||||||||||||||||
| 74 | } | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | 3 | $self->{__fh} = $fh; | |||||||||||||||||
| 77 | 3 | seek $fh, 0, 0; | |||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | # Use a simple/fast file check (file size + mod time) | ||||||||||||||||||
| 80 | 3 | my $st = stat($fn); | |||||||||||||||||
| 81 | 3 | my $statsum = $st->size . $st->mtime; | |||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | 3 | my $checksum; | |||||||||||||||||
| 84 | 3 | ▶ | if ($self->{__paranoid}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | # Calculate MD5 on whole file (NOTE: currently addfile() (XS-based) will | ||||||||||||||||||
| 87 | # fail on tied filehandle - don't use. | ||||||||||||||||||
| 88 | 0 | my $d = Digest::MD5->new(); | |||||||||||||||||
| 89 | 0 | ▶ | while (my $r = read $fh, my $buf, 8192) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 90 | 0 | $d->add($buf); | |||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | 0 | $checksum = $d->hexdigest; | |||||||||||||||||
| 93 | 0 | seek $fh, 0, 0; | |||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | } | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | # Check for existing index. If present, compare checksums and load | ||||||||||||||||||
| 98 | # existing index upon match. Croak if no match. | ||||||||||||||||||
| 99 | 3 | my $fn_idx = $fn . '.idx'; | |||||||||||||||||
| 100 | 3 | ▶ | while (-r $fn_idx) { # 'while' instead of 'if' so we can break out | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | 0 | ▶ | open my $fhi, '<:gzip', $fn_idx or die "Error opening index: $!\n"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 103 | 0 | my $existing = retrieve_fd($fhi); | |||||||||||||||||
| 104 | 0 | close $fhi; | |||||||||||||||||
| 105 | 0 | ▶ | if ($existing->{__version} < $MS::VERSION) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 106 | 0 | croak "Index files were generated by an older version of " | |||||||||||||||||
| 107 | . blessed($self) . ". Not all versions are backwards compatible" | ||||||||||||||||||
| 108 | . " - please remove the old indices and try again\n"; | ||||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | elsif ($statsum ne $existing->{__statsum}) { | ||||||||||||||||||
| 111 | 0 | croak "size/mtime check in existing index $fn_idx failed. If" | |||||||||||||||||
| 112 | . " data file has changed, please remove existing index and" | ||||||||||||||||||
| 113 | . " try again.\n"; | ||||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | elsif ($self->{__paranoid}) { | ||||||||||||||||||
| 116 | 0 | ▶ | if (! exists $existing->{__md5sum}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 117 | 0 | carp "No MD5 sum in existing index $fn_idx. Will regenerate" | |||||||||||||||||
| 118 | . " index with MD5 sum"; | ||||||||||||||||||
| 119 | 0 | last; | |||||||||||||||||
| 120 | } | ||||||||||||||||||
| 121 | 0 | ▶ | if ($checksum ne $existing->{__md5sum}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 122 | 0 | croak "MD5 check in existing index $fn_idx failed. If data file" | |||||||||||||||||
| 123 | . " has changed, please remove existing index and try again.\n"; | ||||||||||||||||||
| 124 | } | ||||||||||||||||||
| 125 | } | ||||||||||||||||||
| 126 | 0 | %$self = %$existing; | |||||||||||||||||
| 127 | 0 | $self->{__fh} = $fh; | |||||||||||||||||
| 128 | 0 | $self->{__fn} = $fn; | |||||||||||||||||
| 129 | 0 | $self->{__use_cache} = $use_cache; | |||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | # defined in subclasses | ||||||||||||||||||
| 132 | 0 | $self->_post_load; | |||||||||||||||||
| 133 | 0 | $self->_lock; | |||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | 0 | return; | |||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | } | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | # Otherwise, do full file parse and store index | ||||||||||||||||||
| 140 | 3 | $self->{__statsum} = $statsum; | |||||||||||||||||
| 141 | 3 | ▶ | $self->{__md5sum} = $checksum if ($self->{__paranoid}); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 142 | 3 | delete $self->{__paranoid}; | |||||||||||||||||
| 143 | |||||||||||||||||||
| 144 | # This is where the actual file parsing takes place. The _load_new() | ||||||||||||||||||
| 145 | # method must be defined for each format-specific subclass. | ||||||||||||||||||
| 146 | 3 | $self->_pre_load; | |||||||||||||||||
| 147 | 3 | $self->_load_new; | |||||||||||||||||
| 148 | 3 | $self->_post_load; | |||||||||||||||||
| 149 | |||||||||||||||||||
| 150 | # Store data structure as index | ||||||||||||||||||
| 151 | 3 | $self->_write_index; | |||||||||||||||||
| 152 | 3 | $self->_lock; | |||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | 3 | return; | |||||||||||||||||
| 155 | |||||||||||||||||||
| 156 | } | ||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | 0 | ▶ | sub get_app_data { | ||||||||||||||||
|
Subroutine
get_app_data: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||
| 159 | |||||||||||||||||||
| 160 | 0 | my ($self, $app_id, $field) = @_; | |||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | 0 | ▶ | return if (! exists $self->{__app_data}->{$app_id}); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 163 | 0 | ▶ | return if (! exists $self->{__app_data}->{$app_id}->{$field}); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 164 | 0 | return $self->{__app_data}->{$app_id}->{$field}; | |||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | } | ||||||||||||||||||
| 167 | |||||||||||||||||||
| 168 | 0 | ▶ | sub set_app_data { | ||||||||||||||||
|
Subroutine
set_app_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 169 | |||||||||||||||||||
| 170 | 0 | my ($self, $app_id, $field, $val) = @_; | |||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | 0 | $self->_unlock; | |||||||||||||||||
| 173 | 0 | $self->{__app_data}->{$app_id}->{$field} = $val; | |||||||||||||||||
| 174 | 0 | $self->_write_index; | |||||||||||||||||
| 175 | 0 | $self->_lock; | |||||||||||||||||
| 176 | |||||||||||||||||||
| 177 | 0 | return 1; | |||||||||||||||||
| 178 | |||||||||||||||||||
| 179 | } | ||||||||||||||||||
| 180 | |||||||||||||||||||
| 181 | |||||||||||||||||||
| 182 | 3 | ▶ | sub _write_index { | ||||||||||||||||
|
Subroutine
_write_index: called CC=4 SCAR=14.0
| |||||||||||||||||||
| 183 | |||||||||||||||||||
| 184 | 3 | my ($self) = @_; | |||||||||||||||||
| 185 | |||||||||||||||||||
| 186 | 3 | $self->_unlock; | |||||||||||||||||
| 187 | 3 | my @saved; | |||||||||||||||||
| 188 | 3 | for (@{ $self->{__memoized_refs} }) { | |||||||||||||||||
| 189 | 0 | push @saved, delete $_->{__memoized}; | |||||||||||||||||
| 190 | } | ||||||||||||||||||
| 191 | 3 | my $tmp_fh = delete $self->{__fh}; | |||||||||||||||||
| 192 | 3 | my $fn_idx = $self->{__fn} . '.idx'; | |||||||||||||||||
| 193 | 3 | open my $fh, '>:gzip', $fn_idx; | |||||||||||||||||
| 194 | 3 | ▶ | nstore_fd($self => $fh) or die "failed to store self: $!\n"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 195 | 3 | close $fh; | |||||||||||||||||
| 196 | 3 | $self->{__fh} = $tmp_fh; | |||||||||||||||||
| 197 | 3 | for (@{ $self->{__memoized_refs} }) { | |||||||||||||||||
| 198 | 0 | $_->{__memoized} = shift @saved; | |||||||||||||||||
| 199 | } | ||||||||||||||||||
| 200 | 3 | $self->_lock; | |||||||||||||||||
| 201 | |||||||||||||||||||
| 202 | 3 | return; | |||||||||||||||||
| 203 | |||||||||||||||||||
| 204 | } | ||||||||||||||||||
| 205 | |||||||||||||||||||
| 206 | 3 | ▶ | sub _pre_load {} # defined by subclass | ||||||||||||||||
|
Subroutine
_pre_load: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 207 | 0 | ▶ | sub _post_load {} # defined by subclass | ||||||||||||||||
|
Subroutine
_post_load: not called CC=1 SCAR=0.0
| |||||||||||||||||||
| 208 | 6 | ▶ | sub _lock {} # defined by subclass | ||||||||||||||||
|
Subroutine
_lock: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 209 | 3 | ▶ | sub _unlock {} # defined by subclass | ||||||||||||||||
|
Subroutine
_unlock: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 210 | |||||||||||||||||||
| 211 | 11 | 1; | |||||||||||||||||
| 212 | |||||||||||||||||||
| 213 | __END__ | ||||||||||||||||||