| 1 | package KinoSearch1::Index::PostingsWriter; | ||
| 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 / members | ||
| 10 | invindex => undef, | ||
| 11 | seg_name => undef, | ||
| 12 | |||
| 13 | # members | ||
| 14 | sort_pool => undef, | ||
| 15 | ); | ||
| 16 | } | ||
| 17 | |||
| 18 | use KinoSearch1::Index::TermInfo; | ||
| 19 | use KinoSearch1::Index::TermInfosWriter; | ||
| 20 | use KinoSearch1::Util::SortExternal; | ||
| 21 | |||
| 22 | sub init_instance { | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | # create a SortExternal object which autosorts the posting list cache | ||
| 26 | $self->{sort_pool} = KinoSearch1::Util::SortExternal->new( | ||
| 27 | invindex => $self->{invindex}, | ||
| 28 | seg_name => $self->{seg_name}, | ||
| 29 | ); | ||
| 30 | } | ||
| 31 | |||
| 32 | # Add all the postings in an inverted document to the sort pool. | ||
| 33 | sub add_postings { | ||
| 34 | my ( $self, $postings_array ) = @_; | ||
| 35 | $self->{sort_pool}->feed(@$postings_array); | ||
| 36 | } | ||
| 37 | |||
| 38 | # Bulk add all the postings in a segment to the sort pool. | ||
| 39 | sub add_segment { | ||
| 40 | my ( $self, $seg_reader, $doc_map ) = @_; | ||
| 41 | my $term_enum = $seg_reader->terms; | ||
| 42 | my $term_docs = $seg_reader->term_docs; | ||
| 43 | $term_docs->set_read_positions(1); | ||
| 44 | _add_segment( $self->{sort_pool}, $term_enum, $term_docs, $doc_map ); | ||
| 45 | } | ||
| 46 | |||
| 47 | =for comment | ||
| 48 | |||
| 49 | Process all the postings in the sort pool. Generate the freqs and positions | ||
| 50 | files. Hand off data to TermInfosWriter for the generating the term | ||
| 51 | dictionaries. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub write_postings { | ||
| 56 | my $self = shift; | ||
| 57 | my ( $invindex, $seg_name ) = @{$self}{ 'invindex', 'seg_name' }; | ||
| 58 | |||
| 59 | $self->{sort_pool}->sort_all; | ||
| 60 | |||
| 61 | my $tinfos_writer = KinoSearch1::Index::TermInfosWriter->new( | ||
| 62 | invindex => $invindex, | ||
| 63 | seg_name => $seg_name, | ||
| 64 | ); | ||
| 65 | my $frq_file = "$seg_name.frq"; | ||
| 66 | my $prx_file = "$seg_name.prx"; | ||
| 67 | for ( $frq_file, $prx_file ) { | ||
| 68 | $invindex->delete_file($_) if $invindex->file_exists($_); | ||
| 69 | } | ||
| 70 | my $frq_out = $invindex->open_outstream($frq_file); | ||
| 71 | my $prx_out = $invindex->open_outstream($prx_file); | ||
| 72 | |||
| 73 | _write_postings( $self->{sort_pool}, $tinfos_writer, $frq_out, $prx_out ); | ||
| 74 | |||
| 75 | $frq_out->close; | ||
| 76 | $prx_out->close; | ||
| 77 | $tinfos_writer->finish; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub finish { | ||
| 81 | my $self = shift; | ||
| 82 | $self->{sort_pool}->close; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 | |||
| 87 | __END__ |