| 1 | package KinoSearch1::Index::SegInfos; | ||||||||||||||||||||
| 2 | 34 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 34 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 34 | ▶ | use KinoSearch1::Util::ToolSet; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 34 | ▶ | use base qw( KinoSearch1::Util::Class ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 34 | ▶ | use constant FORMAT => -1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 34 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 34 | __PACKAGE__->init_instance_vars( | |||||||||||||||||||
| 11 | # members | ||||||||||||||||||||
| 12 | infos => undef, | ||||||||||||||||||||
| 13 | counter => 0, | ||||||||||||||||||||
| 14 | version => undef, | ||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||
| 16 | 34 | __PACKAGE__->ready_get_set(qw( counter )); | |||||||||||||||||||
| 17 | } | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 34 | ▶ | use Time::HiRes qw( time ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 129 | ▶ | sub init_instance { | ||||||||||||||||||
|
Subroutine
init_instance: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 22 | 129 | my $self = shift; | |||||||||||||||||||
| 23 | 129 | $self->{infos} = {}; | |||||||||||||||||||
| 24 | 129 | ▶ | $self->{version} ||= int( time * 1000 ); | ||||||||||||||||||
|
Condition33%1/3
$self->{"version"} ||= int &KinoSearch1::Index::SegInfos::time() * 1000
MC/DC0%0/2
$self->{"version"} ||= int &KinoSearch1::Index::SegInfos::time() * 1000
$self->{"version"}
int &KinoSearch1::Index::SegInfos::time() * 1000
Truth table33%1/3
A ||= B
A= $self->{"version"}B= int &KinoSearch1::Index::SegInfos::time() * 1000
| |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | # Add a SegInfo to the collection. | ||||||||||||||||||||
| 28 | 61 | ▶ | sub add_info { | ||||||||||||||||||
|
Subroutine
add_info: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 29 | 61 | my ( $self, $info ) = @_; | |||||||||||||||||||
| 30 | 61 | $self->{infos}{"$info->{seg_name}"} = $info; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # Remove the info corresponding to a segment; | ||||||||||||||||||||
| 34 | 14 | ▶ | sub delete_segment { | ||||||||||||||||||
|
Subroutine
delete_segment: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 35 | 14 | my ( $self, $seg_name ) = @_; | |||||||||||||||||||
| 36 | confess("no segment named '$seg_name'") | ||||||||||||||||||||
| 37 | 14 | ▶ | unless exists $self->{infos}{$seg_name}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 14 | delete $self->{infos}{$seg_name}; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | # Return number of segments in invindex. | ||||||||||||||||||||
| 42 | 0 | ▶ | sub size { scalar keys %{ $_[0]->{infos} } } | ||||||||||||||||||
|
Subroutine
size: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | # Retrieve all infos. | ||||||||||||||||||||
| 45 | 65 | ▶ | sub infos { | ||||||||||||||||||
|
Subroutine
infos: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 46 | 65 | sort { $a->{seg_name} cmp $b->{seg_name} } values %{ $_[0]->{infos} }; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | # Decode "segments" file. | ||||||||||||||||||||
| 50 | 83 | ▶ | sub read_infos { | ||||||||||||||||||
|
Subroutine
read_infos: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 51 | 83 | my ( $self, $invindex ) = @_; | |||||||||||||||||||
| 52 | 83 | my $instream = $invindex->open_instream('segments'); | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # support only recent index formats | ||||||||||||||||||||
| 55 | 83 | my $format = $instream->lu_read('i'); | |||||||||||||||||||
| 56 | 83 | ▶ | croak("unsupported format: '$format'") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | unless $format == FORMAT; | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # read header | ||||||||||||||||||||
| 60 | 83 | @{$self}{ 'version', 'counter' } = $instream->lu_read('Qi'); | |||||||||||||||||||
| 61 | 83 | my $num_segs = $instream->lu_read('i'); | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # build one SegInfo object for each segment | ||||||||||||||||||||
| 64 | 83 | ▶ | if ($num_segs) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 83 | my @file_contents = $instream->lu_read( 'Ti' x $num_segs ); | |||||||||||||||||||
| 66 | 83 | ▶ | while (@file_contents) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 132 | my ( $seg_name, $doc_count ) = splice( @file_contents, 0, 2 ); | |||||||||||||||||||
| 68 | 132 | $self->{infos}{$seg_name} = KinoSearch1::Index::SegInfo->new( | |||||||||||||||||||
| 69 | seg_name => $seg_name, | ||||||||||||||||||||
| 70 | doc_count => $doc_count, | ||||||||||||||||||||
| 71 | invindex => $invindex, | ||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | # Write "segments" file | ||||||||||||||||||||
| 78 | 107 | ▶ | sub write_infos { | ||||||||||||||||||
|
Subroutine
write_infos: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 79 | 107 | my ( $self, $invindex ) = @_; | |||||||||||||||||||
| 80 | 107 | my $num_segs = scalar keys %{ $self->{infos} }; | |||||||||||||||||||
| 81 | 107 | my $tempname = 'segments.new'; | |||||||||||||||||||
| 82 | 107 | ▶ | $invindex->delete_file($tempname) if $invindex->file_exists($tempname); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 107 | my $outstream = $invindex->open_outstream($tempname); | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | # prepare header | ||||||||||||||||||||
| 86 | 107 | $self->{version}++; | |||||||||||||||||||
| 87 | 107 | my @outstuff = ( FORMAT, $self->{version}, $self->{counter}, $num_segs ); | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | # prepare data | ||||||||||||||||||||
| 90 | push @outstuff, map { | ||||||||||||||||||||
| 91 | ( $self->{infos}{$_}{seg_name}, $self->{infos}{$_}{doc_count} ) | ||||||||||||||||||||
| 92 | 85 | } | |||||||||||||||||||
| 93 | 107 | sort keys %{ $self->{infos} }; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | # write it all out | ||||||||||||||||||||
| 96 | 107 | my $template = 'iQii' . ( 'Ti' x $num_segs ); | |||||||||||||||||||
| 97 | 107 | $outstream->lu_write( $template, @outstuff ); | |||||||||||||||||||
| 98 | 107 | $outstream->close; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | # clobber the old segments file | ||||||||||||||||||||
| 101 | 107 | $invindex->rename_file( $tempname, "segments" ); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | package KinoSearch1::Index::SegInfo; | ||||||||||||||||||||
| 105 | 34 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 106 | 34 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 107 | 34 | ▶ | use KinoSearch1::Util::ToolSet; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 108 | 34 | ▶ | use base qw( KinoSearch1::Util::Class ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 34 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 111 | 34 | __PACKAGE__->init_instance_vars( | |||||||||||||||||||
| 112 | # constructor params / members | ||||||||||||||||||||
| 113 | seg_name => '', | ||||||||||||||||||||
| 114 | doc_count => 0, | ||||||||||||||||||||
| 115 | invindex => undef, | ||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||
| 117 | 34 | __PACKAGE__->ready_get(qw( seg_name doc_count invindex )); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 34 | 1; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||