11use std:: io;
22
3- use common:: OwnedBytes ;
4-
5- use crate :: directory:: FileSlice ;
3+ use crate :: directory:: { BufferedFileSlice , FileSlice } ;
64use crate :: positions:: PositionReader ;
75use crate :: postings:: { BlockSegmentPostings , SegmentPostings , TermInfo } ;
86use crate :: schema:: IndexRecordOption ;
@@ -11,16 +9,17 @@ use crate::termdict::TermDictionary;
119/// The inverted index reader is in charge of accessing
1210/// the inverted index associated with a specific field.
1311///
14- /// This is optimized for merging in that it full reads
15- /// the postings and positions files into memory when opened.
16- /// This eliminates all disk I/O to these files during merging.
12+ /// This is optimized for merging in that it uses a buffered reader
13+ /// for the postings and positions files.
14+ /// This eliminates most disk I/O to these files during merging, without
15+ /// reading the entire file into memory at once.
1716///
1817/// NB: This is a copy/paste from [`InvertedIndexReader`] and trimmed
1918/// down to only include the methods required by the merge process.
2019pub ( crate ) struct MergeOptimizedInvertedIndexReader {
2120 termdict : TermDictionary ,
22- postings_bytes : OwnedBytes ,
23- positions_bytes : OwnedBytes ,
21+ postings_reader : BufferedFileSlice ,
22+ positions_reader : BufferedFileSlice ,
2423 record_option : IndexRecordOption ,
2524}
2625
@@ -34,8 +33,8 @@ impl MergeOptimizedInvertedIndexReader {
3433 let ( _, postings_body) = postings_file_slice. split ( 8 ) ;
3534 Ok ( MergeOptimizedInvertedIndexReader {
3635 termdict,
37- postings_bytes : postings_body . read_bytes ( ) ? ,
38- positions_bytes : positions_file_slice . read_bytes ( ) ? ,
36+ postings_reader : BufferedFileSlice :: new_with_default_buffer_size ( postings_body ) ,
37+ positions_reader : BufferedFileSlice :: new_with_default_buffer_size ( positions_file_slice ) ,
3938 record_option,
4039 } )
4140 }
@@ -45,8 +44,8 @@ impl MergeOptimizedInvertedIndexReader {
4544 pub fn empty ( record_option : IndexRecordOption ) -> MergeOptimizedInvertedIndexReader {
4645 MergeOptimizedInvertedIndexReader {
4746 termdict : TermDictionary :: empty ( ) ,
48- postings_bytes : FileSlice :: empty ( ) . read_bytes ( ) . unwrap ( ) ,
49- positions_bytes : FileSlice :: empty ( ) . read_bytes ( ) . unwrap ( ) ,
47+ postings_reader : BufferedFileSlice :: empty ( ) ,
48+ positions_reader : BufferedFileSlice :: empty ( ) ,
5049 record_option,
5150 }
5251 }
@@ -65,7 +64,9 @@ impl MergeOptimizedInvertedIndexReader {
6564 term_info : & TermInfo ,
6665 requested_option : IndexRecordOption ,
6766 ) -> io:: Result < BlockSegmentPostings > {
68- let postings_data = self . postings_bytes . slice ( term_info. postings_range . clone ( ) ) ;
67+ let postings_data = self . postings_reader . get_bytes (
68+ term_info. postings_range . start as u64 ..term_info. postings_range . end as u64 ,
69+ ) ?;
6970 BlockSegmentPostings :: open (
7071 term_info. doc_freq ,
7172 postings_data,
@@ -88,9 +89,9 @@ impl MergeOptimizedInvertedIndexReader {
8889 let block_postings = self . read_block_postings_from_terminfo ( term_info, option) ?;
8990 let position_reader = {
9091 if option. has_positions ( ) {
91- let positions_data = self
92- . positions_bytes
93- . slice ( term_info . positions_range . clone ( ) ) ;
92+ let positions_data = self . positions_reader . get_bytes (
93+ term_info . positions_range . start as u64 ..term_info . positions_range . end as u64 ,
94+ ) ? ;
9495 let position_reader = PositionReader :: open ( positions_data) ?;
9596 Some ( position_reader)
9697 } else {
0 commit comments