| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package DarkPAN::Indexer; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use CLI::Simple::Constants qw(:booleans); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use CLI::Simple::Utils qw(slurp_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use File::Temp qw(tempfile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use Scalar::Util qw(openhandle); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use Module::Load (); # runtime require by name (or Module::Runtime::use_module) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | __PACKAGE__->follow_best_practice; | |||||||||||||||||||||||||||
| 19 | 6 | __PACKAGE__->mk_accessors( | |||||||||||||||||||||||||||
| 20 | qw( | ||||||||||||||||||||||||||||
| 21 | storage | ||||||||||||||||||||||||||||
| 22 | format | ||||||||||||||||||||||||||||
| 23 | config | ||||||||||||||||||||||||||||
| 24 | config_file | ||||||||||||||||||||||||||||
| 25 | profile | ||||||||||||||||||||||||||||
| 26 | ) | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 6 | ▶ | use parent qw(Class::Accessor::Fast); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 6 | ▶ | caller or exit __PACKAGE__->main(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 6 | our $VERSION = '1.0.2'; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # The config entry this indexer operates on. Selected by name (--profile / | ||||||||||||||||||||||||||||
| 36 | # a bucket key) from the multi-DarkPAN config, or the whole file if it is a | ||||||||||||||||||||||||||||
| 37 | # single flat entry. Kept generic so the storage/format keys drive behavior. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | ######################################################################## | ||||||||||||||||||||||||||||
| 40 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | ######################################################################## | ||||||||||||||||||||||||||||
| 42 | 5 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 5 | ▶ | my $options = ref $args[0] ? $args[0] : {@args}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 5 | my $self = $class->SUPER::new($options); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 5 | $self->_load_config; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 5 | $self->_init_storage; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 5 | $self->_init_format; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5 | return $self; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | ######################################################################## | ||||||||||||||||||||||||||||
| 58 | # Resolve a bare backend name to its class, honoring the '+' verbatim | ||||||||||||||||||||||||||||
| 59 | # escape hatch, load it, and assert it consumed the expected role. | ||||||||||||||||||||||||||||
| 60 | ######################################################################## | ||||||||||||||||||||||||||||
| 61 | 10 | ▶ | sub _load_engine { | ||||||||||||||||||||||||||
|
Subroutine
_load_engine: called CC=6 SCAR=19.4
| |||||||||||||||||||||||||||||
| 62 | ######################################################################## | ||||||||||||||||||||||||||||
| 63 | 10 | my ( $self, $name, $family, $role ) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 10 | ▶ | croak "ERROR: no $family engine specified in config\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $name or $name eq ""
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name
$name eq ""
Truth table33%1/3
not(A) or B
A= not defined $nameB= $name eq ""
| |||||||||||||||||||||||||||||
| 66 | if !defined $name || $name eq q{}; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 10 | ▶ | my $class = $name =~ s/^[+]//xsm ? $name : "DarkPAN::Indexer::${family}::${name}"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 10 | local $EVAL_ERROR; | |||||||||||||||||||||||||||
| 71 | 10 | ▶ | eval { Module::Load::load($class); 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | or croak "ERROR: could not load $family engine '$class': $EVAL_ERROR"; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 10 | ▶ | croak "ERROR: $class does not consume $role\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | if !$class->DOES($role); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 10 | return $class; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | ######################################################################## | ||||||||||||||||||||||||||||
| 81 | 5 | ▶ | sub _init_storage { | ||||||||||||||||||||||||||
|
Subroutine
_init_storage: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||
| 82 | ######################################################################## | ||||||||||||||||||||||||||||
| 83 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 5 | my $config = $self->get_config; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # config->{storage}{type} selects the backend; legacy configs with only | ||||||||||||||||||||||||||||
| 88 | # an AWS block default to S3 (the only backend the old configs meant). | ||||||||||||||||||||||||||||
| 89 | 5 | ▶ | my $type = $config->{storage} ? $config->{storage}->{type} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 5 | ▶ | $type //= 'S3' if $config->{AWS}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$type //= "S3"
MC/DC0%0/1
$type //= "S3"
$type
Truth table0%0/2
A
A= $type
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 5 | my $class = $self->_load_engine( $type, 'Storage', 'DarkPAN::Indexer::Storage' ); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # S3->new (and every engine) takes the whole config entry and reads its | ||||||||||||||||||||||||||||
| 95 | # own slice; nothing storage-specific is threaded from here. | ||||||||||||||||||||||||||||
| 96 | 5 | $self->set_storage( $class->new($config) ); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 5 | return $self; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | ######################################################################## | ||||||||||||||||||||||||||||
| 102 | 5 | ▶ | sub _init_format { | ||||||||||||||||||||||||||
|
Subroutine
_init_format: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 103 | ######################################################################## | ||||||||||||||||||||||||||||
| 104 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 5 | my $config = $self->get_config; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 5 | ▶ | my $type = $config->{format} ? $config->{format}->{type} : 'SQLite'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 5 | my $class = $self->_load_engine( $type, 'Format', 'DarkPAN::Indexer::Format' ); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 5 | ▶ | $self->set_format( $class->new( $config->{format} // {} ) ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"format"} // {}
MC/DC0%0/1
$config->{"format"} // {}
$config->{"format"}
Truth table50%1/2
A
A= $config->{"format"}
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 5 | return $self; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | ######################################################################## | ||||||||||||||||||||||||||||
| 118 | # Where the version index lives in the storage container. From config | ||||||||||||||||||||||||||||
| 119 | # (packages_version_index), e.g. 'orepan2/modules/packages.db.gz'. The | ||||||||||||||||||||||||||||
| 120 | # .gz suffix drives compression in retrieve_index/publish_index. | ||||||||||||||||||||||||||||
| 121 | ######################################################################## | ||||||||||||||||||||||||||||
| 122 | 8 | ▶ | sub _index_key { | ||||||||||||||||||||||||||
|
Subroutine
_index_key: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 123 | ######################################################################## | ||||||||||||||||||||||||||||
| 124 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 8 | my $key = $self->get_config->{packages_version_index}; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 8 | ▶ | croak "ERROR: packages_version_index not set in config\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $key or $key eq ""
MC/DC0%0/2
not defined $key or $key eq ""
not defined $key
$key eq ""
Truth table33%1/3
not(A) or B
A= not defined $keyB= $key eq ""
| |||||||||||||||||||||||||||||
| 129 | if !defined $key || $key eq q{}; | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 8 | return $key; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | ######################################################################## | ||||||||||||||||||||||||||||
| 135 | # create_index: build a fresh index from every distribution in storage. | ||||||||||||||||||||||||||||
| 136 | # list_distributions -> format->create_index(fh) -> load_index(create) | ||||||||||||||||||||||||||||
| 137 | # -> publish_index. Full rebuild; authoritative. | ||||||||||||||||||||||||||||
| 138 | ######################################################################## | ||||||||||||||||||||||||||||
| 139 | 4 | ▶ | sub create_index { | ||||||||||||||||||||||||||
|
Subroutine
create_index: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | ######################################################################## | ||||||||||||||||||||||||||||
| 141 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 4 | my $storage = $self->get_storage; | |||||||||||||||||||||||||||
| 144 | 4 | my $format = $self->get_format; | |||||||||||||||||||||||||||
| 145 | 4 | my $index = $self->_index_key; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | ▶ | my $guard = $storage->lock($index) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | or croak "ERROR: could not acquire lock on $index\n"; | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # format writes the neutral TSV interchange for all distributions... | ||||||||||||||||||||||||||||
| 151 | 4 | my ( $tsv_fh, $tsv_file ) = File::Temp::tempfile( DIR => '/tmp', UNLINK => 1 ); | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | my $stats = $format->create_index( | ||||||||||||||||||||||||||||
| 154 | output => $tsv_fh, | ||||||||||||||||||||||||||||
| 155 | distributions => [ $storage->list_distributions ], | ||||||||||||||||||||||||||||
| 156 | 6 | ▶ | fetch => sub { $self->_fetch_dist_to_tmp( $_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 157 | 4 | ); | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 4 | close $tsv_fh; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # ...then loads that interchange into a fresh database, which we publish. | ||||||||||||||||||||||||||||
| 162 | 4 | my ( undef, $db_file ) = File::Temp::tempfile( DIR => '/tmp', UNLINK => 1, SUFFIX => '.db' ); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 4 | $format->load_index( | |||||||||||||||||||||||||||
| 165 | input => $tsv_file, | ||||||||||||||||||||||||||||
| 166 | database => $db_file, | ||||||||||||||||||||||||||||
| 167 | create => $TRUE, | ||||||||||||||||||||||||||||
| 168 | ); | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 4 | $storage->publish_index( $db_file, $index ); | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 4 | return $stats; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | ######################################################################## | ||||||||||||||||||||||||||||
| 176 | # update_index: incrementally re-index ONE distribution. | ||||||||||||||||||||||||||||
| 177 | # retrieve current index -> open it -> format->update_index(that dist) | ||||||||||||||||||||||||||||
| 178 | # -> publish. Locked for the whole read-modify-write. | ||||||||||||||||||||||||||||
| 179 | ######################################################################## | ||||||||||||||||||||||||||||
| 180 | 3 | ▶ | sub update_index { | ||||||||||||||||||||||||||
|
Subroutine
update_index: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 181 | ######################################################################## | ||||||||||||||||||||||||||||
| 182 | 3 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | my $distribution = $args{distribution} | ||||||||||||||||||||||||||||
| 185 | 3 | ▶ | or croak "ERROR: update_index requires a distribution\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 3 | my $storage = $self->get_storage; | |||||||||||||||||||||||||||
| 188 | 3 | my $format = $self->get_format; | |||||||||||||||||||||||||||
| 189 | 3 | my $index = $self->_index_key; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 3 | ▶ | my $guard = $storage->lock($index) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | or croak "ERROR: could not acquire lock on $index\n"; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # bring the current index down to a local, writable db file | ||||||||||||||||||||||||||||
| 195 | 3 | my $db_file = $storage->retrieve_index( $index, '.db' ); | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | # delete-then-insert for this distribution happens inside update_index; | ||||||||||||||||||||||||||||
| 198 | # the format opens $db_file, mutates, and (via load) commits. | ||||||||||||||||||||||||||||
| 199 | $format->update_index( | ||||||||||||||||||||||||||||
| 200 | distribution => $distribution, | ||||||||||||||||||||||||||||
| 201 | database => $db_file, | ||||||||||||||||||||||||||||
| 202 | 2 | ▶ | fetch => sub { $self->_fetch_dist_to_tmp( $_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 3 | ); | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 1 | $storage->publish_index( $db_file, $index ); | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 1 | unlink $db_file; # we received ownership from retrieve_index | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1 | return $self; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | ######################################################################## | ||||||||||||||||||||||||||||
| 214 | # delete_from_index: remove one distribution's rows from the index. | ||||||||||||||||||||||||||||
| 215 | # retrieve -> update_index(delete_only) -> publish. Same locked round | ||||||||||||||||||||||||||||
| 216 | # trip, no re-insert. | ||||||||||||||||||||||||||||
| 217 | ######################################################################## | ||||||||||||||||||||||||||||
| 218 | 1 | ▶ | sub delete_from_index { | ||||||||||||||||||||||||||
|
Subroutine
delete_from_index: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 219 | ######################################################################## | ||||||||||||||||||||||||||||
| 220 | 1 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | my $distribution = $args{distribution} | ||||||||||||||||||||||||||||
| 223 | 1 | ▶ | or croak "ERROR: delete_from_index requires a distribution\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1 | my $storage = $self->get_storage; | |||||||||||||||||||||||||||
| 226 | 1 | my $format = $self->get_format; | |||||||||||||||||||||||||||
| 227 | 1 | my $index = $self->_index_key; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 1 | ▶ | my $guard = $storage->lock($index) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | or croak "ERROR: could not acquire lock on $index\n"; | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 1 | my $db_file = $storage->retrieve_index( $index, '.db' ); | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | $format->update_index( | ||||||||||||||||||||||||||||
| 235 | distribution => $distribution, | ||||||||||||||||||||||||||||
| 236 | database => $db_file, | ||||||||||||||||||||||||||||
| 237 | delete_only => $TRUE, | ||||||||||||||||||||||||||||
| 238 | 0 | ▶ | fetch => sub { $self->_fetch_dist_to_tmp( $_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 239 | 1 | ); | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 1 | $storage->publish_index( $db_file, $index ); | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 1 | unlink $db_file; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 1 | return $self; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | ######################################################################## | ||||||||||||||||||||||||||||
| 249 | 5 | ▶ | sub _load_config { | ||||||||||||||||||||||||||
|
Subroutine
_load_config: called CC=10 SCAR=29.2
| |||||||||||||||||||||||||||||
| 250 | ######################################################################## | ||||||||||||||||||||||||||||
| 251 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 5 | ▶ | return $self | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->get_config and ref $self->get_config
MC/DC50%1/2
$self->get_config and ref $self->get_config
$self->get_config
ref $self->get_config
Truth table66%2/3
A and B
A= $self->get_configB= ref $self->get_config
| |||||||||||||||||||||||||||||
| 254 | if $self->get_config && ref $self->get_config; | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 1 | ▶ | my $config_file = $self->get_config_file // $ENV{OREPAN2_S3_CONFIG}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->get_config_file // $ENV{"OREPAN2_S3_CONFIG"}
MC/DC0%0/2
$self->get_config_file // $ENV{"OREPAN2_S3_CONFIG"}
$self->get_config_file
$ENV{"OREPAN2_S3_CONFIG"}
Truth table33%1/3
A // B
A= $self->get_config_fileB= $ENV{"OREPAN2_S3_CONFIG"}
| |||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 1 | ▶ | croak "ERROR: no config_file\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | if !$config_file; | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 1 | my $config = eval { | |||||||||||||||||||||||||||
| 262 | 1 | ▶ | return slurp_json($config_file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&openhandle($config_file) or $config_file =~ /[.]json$/msx
MC/DC0%0/2
&openhandle($config_file) or $config_file =~ /[.]json$/msx
&openhandle($config_file)
$config_file =~ /[.]json$/msx
Truth table33%1/3
A or B
A= &openhandle($config_file)B= $config_file =~ /[.]json$/msx
| |||||||||||||||||||||||||||||
| 263 | if openhandle($config_file) || $config_file =~ /[.]json$/xsm; | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | ▶ | croak "config file format unsupported\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | if $config_file !~ /[.]ya?ml$/xsm; | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | require YAML::Tiny; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | return YAML::Tiny->read($config_file); | |||||||||||||||||||||||||||
| 271 | }; | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 1 | ▶ | croak "ERROR: $EVAL_ERROR" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $config or $DarkPAN::Indexer::EVAL_ERROR
MC/DC0%0/2
not $config or $DarkPAN::Indexer::EVAL_ERROR
not $config
$DarkPAN::Indexer::EVAL_ERROR
Truth table33%1/3
not(A) or B
A= not $configB= $DarkPAN::Indexer::EVAL_ERROR
| |||||||||||||||||||||||||||||
| 274 | if !$config || $EVAL_ERROR; | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 1 | $self->set_config($config); | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 1 | return $self; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | ######################################################################## | ||||||||||||||||||||||||||||
| 282 | 8 | ▶ | sub _fetch_dist_to_tmp { | ||||||||||||||||||||||||||
|
Subroutine
_fetch_dist_to_tmp: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 283 | ######################################################################## | ||||||||||||||||||||||||||||
| 284 | 8 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | # reap the previous distribution's tempfile before fetching the next, | ||||||||||||||||||||||||||||
| 287 | # so a full rebuild never materializes more than one tarball on disk. | ||||||||||||||||||||||||||||
| 288 | 8 | ▶ | if ( my $prev = $self->{_dist_tmp} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 4 | ▶ | unlink $prev if -e $prev; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 8 | my $bytes = $self->get_storage->fetch_object($key); | |||||||||||||||||||||||||||
| 293 | 8 | ▶ | croak "ERROR: could not fetch $key\n" if !defined $bytes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 7 | my ( $fh, $path ) = File::Temp::tempfile( DIR => '/tmp', UNLINK => 0, SUFFIX => '.tar.gz' ); | |||||||||||||||||||||||||||
| 296 | 7 | binmode $fh; | |||||||||||||||||||||||||||
| 297 | 7 | print {$fh} $bytes; | |||||||||||||||||||||||||||
| 298 | 7 | close $fh; | |||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 7 | $self->{_dist_tmp} = $path; # remember so the NEXT call (or cleanup) reaps it | |||||||||||||||||||||||||||
| 301 | 7 | return $path; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | # call after create_index finishes, to reap the last one: | ||||||||||||||||||||||||||||
| 305 | ######################################################################## | ||||||||||||||||||||||||||||
| 306 | 0 | ▶ | sub _reap_dist_tmp { | ||||||||||||||||||||||||||
|
Subroutine
_reap_dist_tmp: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 307 | ######################################################################## | ||||||||||||||||||||||||||||
| 308 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 309 | 0 | ▶ | my $prev = delete $self->{_dist_tmp} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 0 | ▶ | unlink $prev if -e $prev; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 0 | return; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | ######################################################################## | ||||||||||||||||||||||||||||
| 315 | 0 | ▶ | sub main { | ||||||||||||||||||||||||||
|
Subroutine
main: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 316 | ######################################################################## | ||||||||||||||||||||||||||||
| 317 | 0 | local $ENV{OREPAN2_S3_CONFIG} = sprintf '%s/.orepan2-s3.json', $ENV{HOME}; | |||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 0 | my $indexer = __PACKAGE__->new; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 0 | print {*STDERR} Dumper( [ config => $indexer->get_config ] ); | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 0 | return 0; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 6 | 1; | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =pod | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =head1 NAME | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | DarkPAN::Indexer - build and maintain a multi-version index of a DarkPAN | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | use DarkPAN::Indexer; | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | my $indexer = DarkPAN::Indexer->new( config_file => '/path/to/darkpan.json' ); | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | # full build from every distribution in the repository | ||||||||||||||||||||||||||||
| 341 | my $stats = $indexer->create_index; | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | # incrementally (re)index a single distribution | ||||||||||||||||||||||||||||
| 344 | $indexer->update_index( distribution => 'authors/id/A/AB/AUTHOR/Foo-1.0.tar.gz' ); | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | # remove a distribution's packages from the index | ||||||||||||||||||||||||||||
| 347 | $indexer->delete_from_index( distribution => 'authors/id/A/AB/AUTHOR/Foo-1.0.tar.gz' ); | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | C<DarkPAN::Indexer> builds and maintains a B<multi-version> package index for a | ||||||||||||||||||||||||||||
| 352 | DarkPAN. Unlike C<02packages.details.txt.gz>, which records only the latest | ||||||||||||||||||||||||||||
| 353 | version of each package, this index records B<every> version of every package | ||||||||||||||||||||||||||||
| 354 | present in the repository, so a client can resolve and install a specific | ||||||||||||||||||||||||||||
| 355 | historical version by name -- not just the latest. | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | The indexer is an B<orchestrator>. It composes two pluggable pieces and moves | ||||||||||||||||||||||||||||
| 358 | opaque data between them: | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =over 4 | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | =item * A B<storage> engine (L</"STORAGE ENGINES">) -- where the distributions | ||||||||||||||||||||||||||||
| 363 | and the index physically live (S3, local filesystem, ...). | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =item * A B<format> engine (L</"FORMAT ENGINES">) -- how the index is built and | ||||||||||||||||||||||||||||
| 366 | queried (SQLite, ...). | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =back | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | The orchestrator itself knows nothing about S3 or SQLite. It reads config, | ||||||||||||||||||||||||||||
| 371 | constructs the two engines named there, and drives them. This is what lets the | ||||||||||||||||||||||||||||
| 372 | same code index an S3-backed DarkPAN behind CloudFront and a plain directory of | ||||||||||||||||||||||||||||
| 373 | tarballs on a laptop. | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =head2 new | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | my $indexer = DarkPAN::Indexer->new( config_file => $path ); | ||||||||||||||||||||||||||||
| 380 | my $indexer = DarkPAN::Indexer->new( config => \%config ); | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | Constructs an indexer. Provide either C<config_file> (a path to a JSON config, | ||||||||||||||||||||||||||||
| 383 | see L</CONFIGURATION>) or C<config> (an already-loaded config hashref). The | ||||||||||||||||||||||||||||
| 384 | storage and format engines are constructed immediately from the config. | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =head1 METHODS | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head2 create_index | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | my $stats = $indexer->create_index; | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | Builds a fresh index from B<every> distribution in the repository. Enumerates | ||||||||||||||||||||||||||||
| 393 | the repository (C<< storage->list_distributions >>), scans each distribution for | ||||||||||||||||||||||||||||
| 394 | the packages it provides, loads them into a new index, and publishes the index | ||||||||||||||||||||||||||||
| 395 | back to storage. This is the full-rebuild / authoritative operation; run it to | ||||||||||||||||||||||||||||
| 396 | create the index initially or to rebuild it from ground truth. | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | Returns a stats hashref (distributions seen, distributions indexed, failures, | ||||||||||||||||||||||||||||
| 399 | modules written). | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =head2 update_index | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | $indexer->update_index( distribution => $key ); | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | Incrementally (re)indexes a single distribution. Retrieves the current index, | ||||||||||||||||||||||||||||
| 406 | performs a delete-then-insert for the named distribution's packages, and | ||||||||||||||||||||||||||||
| 407 | publishes the updated index. C<$key> is the storage key of the distribution | ||||||||||||||||||||||||||||
| 408 | tarball (e.g. C<authors/id/A/AB/AUTHOR/Foo-1.0.tar.gz>). The whole | ||||||||||||||||||||||||||||
| 409 | read-modify-write is performed under a storage lock. | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =head2 delete_from_index | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | $indexer->delete_from_index( distribution => $key ); | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | Removes a single distribution's packages from the index. Retrieves the current | ||||||||||||||||||||||||||||
| 416 | index, deletes the rows for the named distribution, and publishes. Also | ||||||||||||||||||||||||||||
| 417 | performed under a storage lock. | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | The config (JSON file via C<config_file>, or a hashref via C<config>) describes | ||||||||||||||||||||||||||||
| 422 | one DarkPAN. The keys the indexer reads are: | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =over 4 | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =item C<storage> | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | Selects and configures the storage engine, e.g.: | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | "storage" : { "type" : "S3", "bucket" : "my-darkpan", "region" : "us-east-1" } | ||||||||||||||||||||||||||||
| 431 | "storage" : { "type" : "Filesystem", "root" : "/srv/darkpan" } | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | C<type> names the engine (resolved to C<DarkPAN::Indexer::Storage::E<lt>typeE<gt>>, | ||||||||||||||||||||||||||||
| 434 | or a C<+Fully::Qualified> name). For backward compatibility, a config with a | ||||||||||||||||||||||||||||
| 435 | legacy C<AWS> block and no C<storage> block is treated as S3. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =item C<format> | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | Selects the index format engine, e.g. C<< "format" : { "type" : "SQLite" } >>. | ||||||||||||||||||||||||||||
| 440 | Defaults to C<SQLite> if omitted. | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =item C<packages_version_index> | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | The storage key of the published index, e.g. | ||||||||||||||||||||||||||||
| 445 | C<orepan2/modules/packages.db.gz>. A C<.gz> suffix causes the index to be | ||||||||||||||||||||||||||||
| 446 | stored compressed. | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =back | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =head1 STORAGE ENGINES | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | A storage engine consumes the C<DarkPAN::Indexer::Storage> role and provides: | ||||||||||||||||||||||||||||
| 453 | C<list_distributions>, C<fetch_object>, C<save_object>, C<has_object>, C<lock>, | ||||||||||||||||||||||||||||
| 454 | and C<base_url>, plus the role-provided C<retrieve_index>/C<publish_index>. | ||||||||||||||||||||||||||||
| 455 | C<DarkPAN::Indexer::Storage::S3> and C<DarkPAN::Indexer::Storage::Filesystem> | ||||||||||||||||||||||||||||
| 456 | ship with this distribution. | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =head1 FORMAT ENGINES | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | A format engine consumes the C<DarkPAN::Indexer::Format> role and provides: | ||||||||||||||||||||||||||||
| 461 | C<create_index>, C<update_index>, C<load_index>, and C<delete_from_index>. The | ||||||||||||||||||||||||||||
| 462 | role provides the shared C<index_distribution> (tarball -> package records) | ||||||||||||||||||||||||||||
| 463 | machinery. C<DarkPAN::Indexer::Format::SQLite> ships with this distribution. | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | L<DarkPAN::Indexer::CLI>, L<DarkPAN::Resolver::SQLite>, L<OrePAN2::Lite> | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | Rob Lauer - <rlauer@treasurersbriefcase.com> | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =cut | ||||||||||||||||||||||||||||