| 1 | package KinoSearch1::Index::TermInfosWriter; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use KinoSearch1::Util::ToolSet; | ||
| 5 | use base qw( KinoSearch1::Util::Class ); | ||
| 6 | |||
| 7 | BEGIN { | ||
| 8 | __PACKAGE__->init_instance_vars( | ||
| 9 | # constructor params | ||
| 10 | invindex => undef, | ||
| 11 | seg_name => undef, | ||
| 12 | is_index => 0, | ||
| 13 | index_interval => 1024, | ||
| 14 | skip_interval => 16, | ||
| 15 | ); | ||
| 16 | } | ||
| 17 | our %instance_vars; | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my $class = shift; | ||
| 21 | confess kerror() unless verify_args( \%instance_vars, @_ ); | ||
| 22 | my %args = ( %instance_vars, @_ ); | ||
| 23 | my $invindex = $args{invindex}; | ||
| 24 | |||
| 25 | # open an outstream | ||
| 26 | my $suffix = $args{is_index} ? 'tii' : 'tis'; | ||
| 27 | my $filename = "$args{seg_name}.$suffix"; | ||
| 28 | $invindex->delete_file($filename) if $invindex->file_exists($filename); | ||
| 29 | my $outstream = $args{invindex}->open_outstream($filename); | ||
| 30 | |||
| 31 | my $self = _new( $outstream, | ||
| 32 | @args{qw( is_index index_interval skip_interval )} ); | ||
| 33 | |||
| 34 | # create the tii doppelganger | ||
| 35 | if ( !$args{is_index} ) { | ||
| 36 | my $other = __PACKAGE__->new( | ||
| 37 | invindex => $invindex, | ||
| 38 | seg_name => $args{seg_name}, | ||
| 39 | is_index => 1, | ||
| 40 | ); | ||
| 41 | $self->_set_other($other); | ||
| 42 | $other->_set_other($self); | ||
| 43 | } | ||
| 44 | |||
| 45 | return $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub finish { | ||
| 49 | my $self = shift; | ||
| 50 | my $outstream = $self->_get_outstream; | ||
| 51 | |||
| 52 | # seek to near the head and write the number of terms processed | ||
| 53 | $outstream->seek(4); | ||
| 54 | $outstream->lu_write( 'Q', $self->_get_size ); | ||
| 55 | |||
| 56 | # cue the doppelganger's exit | ||
| 57 | if ( !$self->_get_is_index ) { | ||
| 58 | $self->_get_other()->finish; | ||
| 59 | } | ||
| 60 | |||
| 61 | $outstream->close; | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | __END__ |