| 1 | package KinoSearch1::Index::CompoundFileWriter; | ||||||||||
| 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 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 34 | __PACKAGE__->init_instance_vars( | |||||||||
| 9 | # constructor params / members | ||||||||||
| 10 | invindex => undef, | ||||||||||
| 11 | filename => undef, | ||||||||||
| 12 | # members | ||||||||||
| 13 | entries => undef, | ||||||||||
| 14 | merged => 0, | ||||||||||
| 15 | ); | ||||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | 61 | ▶ | sub init_instance { | ||||||||
|
Subroutine
init_instance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 19 | 61 | my $self = shift; | |||||||||
| 20 | 61 | $self->{entries} = {}; | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | # Add a file to the list of files-to-merge. | ||||||||||
| 24 | 553 | ▶ | sub add_file { | ||||||||
|
Subroutine
add_file: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 25 | 553 | my ( $self, $filename ) = @_; | |||||||||
| 26 | croak("filename '$filename' already added") | ||||||||||
| 27 | 553 | ▶ | if $self->{entries}{$filename}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 553 | $self->{entries}{$filename} = 1; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | # Write a compound file. | ||||||||||
| 32 | 61 | ▶ | sub finish { | ||||||||
|
Subroutine
finish: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 33 | 61 | my $self = shift; | |||||||||
| 34 | 61 | my $invindex = $self->{invindex}; | |||||||||
| 35 | 61 | my $filename = $self->{filename}; | |||||||||
| 36 | 61 | my @files_to_merge = keys %{ $self->{entries} }; | |||||||||
| 37 | 61 | ▶ | croak('no entries defined') unless @files_to_merge; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | # ensure that the file only gets written once; open the outfile | ||||||||||
| 40 | 61 | ▶ | croak('merge already performed') if $self->{merged}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 61 | $self->{merged} = 1; | |||||||||
| 42 | 61 | ▶ | $invindex->delete_file($filename) if $invindex->file_exists($filename); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 61 | my $outstream = $invindex->open_outstream($filename); | |||||||||
| 44 | |||||||||||
| 45 | # write number of files, plus data_offset placeholders | ||||||||||
| 46 | 61 | my @to_write = map { ( 0, $_ ) } @files_to_merge; | |||||||||
| 47 | 61 | unshift @to_write, scalar @files_to_merge; | |||||||||
| 48 | 61 | my $template = 'V' . ( 'QT' x scalar @files_to_merge ); | |||||||||
| 49 | 61 | $outstream->lu_write( $template, @to_write ); | |||||||||
| 50 | |||||||||||
| 51 | # copy data | ||||||||||
| 52 | 61 | my @data_offsets; | |||||||||
| 53 | 61 | my $out_fh = $outstream; | |||||||||
| 54 | 61 | for my $file (@files_to_merge) { | |||||||||
| 55 | 553 | push @data_offsets, $outstream->tell; | |||||||||
| 56 | 553 | my $instream = $invindex->open_instream($file); | |||||||||
| 57 | 553 | $outstream->absorb($instream); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | # rewrite number of files, plus real data offsets | ||||||||||
| 61 | 61 | $outstream->seek(0); | |||||||||
| 62 | 61 | @to_write = map { ( shift @data_offsets, $_ ) } @files_to_merge; | |||||||||
| 63 | 61 | unshift @to_write, scalar @files_to_merge; | |||||||||
| 64 | 61 | $outstream->lu_write( $template, @to_write ); | |||||||||
| 65 | |||||||||||
| 66 | 61 | $outstream->close; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 34 | 1; | |||||||||
| 70 | |||||||||||
| 71 | __END__ | ||||||||||