pub fn flush(&mut self) -> Result<(), Error> {
        // batch all the vectors.
        let mut buf = Vec::new();
        let drain = self.batch.drain(0..self.batch.len());
        for mut log_line in drain {
            // Encode log line offset.
            let mut line_offset = [0u8; mem::size_of::<u64>()];
            line_offset
                .as_mut()
                .write_u64::<LittleEndian>(self.file_offset)
                .unwrap();
            // Encode log line.
            let line_buf = log_line.raw_data;
            // Encode time stamp.
            let mut ts_buf = [0u8; mem::size_of::<u64>()];
            ts_buf
                .as_mut()
                .write_u64::<LittleEndian>(log_line.ts)
                .unwrap();
            let indexes = log_line.indexes.drain(0..log_line.indexes.len());
            // we'll track of all the offsets because If no search given, we should give
            // all the lines.
            self.entry_offsets.extend(line_offset.to_vec());
            for term in indexes {
                self.index_size = self.index_size + term.len();
                if let Some(posting_list) = self.index_posting_list.get_mut(&term) {
                    posting_list.extend(line_offset.to_vec().iter());
                } else {
                    self.index_posting_list
                        .insert(term.clone(), line_offset.to_vec());
                }
            }

            // Populate posting list of json keys as well.
            let json_keys = log_line.json_keys.drain(0..log_line.json_keys.len());
            for key in json_keys {
                if let Some(posting_list) = self.json_key_posting_list.get_mut(&key) {
                    posting_list.extend(line_offset.to_vec().iter());
                } else {
                    self.json_key_posting_list.insert(key, line_offset.to_vec());
                }
            }
            // TS LENGTH + STRUCTURED KEY + LINE LENGTH.
            let entry_length = (line_buf.len() + ts_buf.len() + 1) as u64;
            // advance the file offset.
            self.file_offset = self.file_offset + entry_length + 8;
            // encode entry length.
            let mut len_buf = [0u8; mem::size_of::<u64>()];
            len_buf
                .as_mut()
                .write_u64::<LittleEndian>(entry_length)
                .unwrap();
            buf.push(len_buf.to_vec());
            buf.push(ts_buf.to_vec());
            // If it is json set the 1 otherwise set 0.
            if log_line.structured {
                buf.push(vec![STRUCTURED_DATA]);
            } else {
                buf.push(vec![UN_STRUCTURED_DATA]);
            }
            // index this line.
            buf.push(line_buf);
            // advance line ts.
            // assert!(self.end_ts < log_line.ts);
            self.end_ts = log_line.ts;
        }
        let mut io_slices = Vec::new();
        for slice in buf.iter() {
            io_slices.push(IoSlice::new(slice));
        }
        // write them in a single syscall. But we have to verify
        // the performance here.
        self.segment_file.write_vectored(&io_slices)?;
        self.segment_file.flush()?;
        self.batch.clear();
        self.flushed_offset = self.file_offset;
        Ok(())
    }
/// flush writes all the batched logline to the segment file.
flush writes all the batched logline to the segment file.
flush writes all the batched logline to the segment file .
pub fn flush(&mut self) -> Result<(), Error> {
        // batch all the vectors.
        let mut buf = Vec::new();
        let drain = self.batch.drain(0..self.batch.len());
        for mut log_line in drain {
            // Encode log line offset.
            let mut line_offset = [0u8; mem::size_of::<u64>()];
            line_offset
                .as_mut()
                .write_u64::<LittleEndian>(self.file_offset)
                .unwrap();
            // Encode log line.
            let line_buf = log_line.raw_data;
            // Encode time stamp.
            let mut ts_buf = [0u8; mem::size_of::<u64>()];
            ts_buf
                .as_mut()
                .write_u64::<LittleEndian>(log_line.ts)
                .unwrap();
            let indexes = log_line.indexes.drain(0..log_line.indexes.len());
            // we'll track of all the offsets because If no search given, we should give
            // all the lines.
            self.entry_offsets.extend(line_offset.to_vec());
            for term in indexes {
                self.index_size = self.index_size + term.len();
                if let Some(posting_list) = self.index_posting_list.get_mut(&term) {
                    posting_list.extend(line_offset.to_vec().iter());
                } else {
                    self.index_posting_list
                        .insert(term.clone(), line_offset.to_vec());
                }
            }
            // Populate posting list of json keys as well.
            let json_keys = log_line.json_keys.drain(0..log_line.json_keys.len());
            for key in json_keys {
                if let Some(posting_list) = self.json_key_posting_list.get_mut(&key) {
                    posting_list.extend(line_offset.to_vec().iter());
                } else {
                    self.json_key_posting_list.insert(key, line_offset.to_vec());
                }
            }
            // TS LENGTH + STRUCTURED KEY + LINE LENGTH.
            let entry_length = (line_buf.len() + ts_buf.len() + 1) as u64;
            // advance the file offset.
            self.file_offset = self.file_offset + entry_length + 8;
            // encode entry length.
            let mut len_buf = [0u8; mem::size_of::<u64>()];
            len_buf
                .as_mut()
                .write_u64::<LittleEndian>(entry_length)
                .unwrap();
            buf.push(len_buf.to_vec());
            buf.push(ts_buf.to_vec());
            // If it is json set the 1 otherwise set 0.
            if log_line.structured {
                buf.push(vec![STRUCTURED_DATA]);
            } else {
                buf.push(vec![UN_STRUCTURED_DATA]);
            }
            // index this line.
            buf.push(line_buf);
            // advance line ts.
            // assert!(self.end_ts < log_line.ts);
            self.end_ts = log_line.ts;
        }
        let mut io_slices = Vec::new();
        for slice in buf.iter() {
            io_slices.push(IoSlice::new(slice));
        }
        // write them in a single syscall. But we have to verify
        // the performance here.
        self.segment_file.write_vectored(&io_slices)?;
        self.segment_file.flush()?;
        self.batch.clear();
        self.flushed_offset = self.file_offset;
        Ok(())
    }
pub fn flush ( & mut self ) -> Result < ( ) , Error > { // batch all the vectors. let mut buf = Vec :: new ( ) ; let drain = self . batch . drain ( 0 .. self . batch . len ( ) ) ; for mut log_line in drain { // Encode log line offset. let mut line_offset = [ 0u8 ; mem :: size_of :: < u64 > ( ) ] ; line_offset . as_mut ( ) . write_u64 :: < LittleEndian > ( self . file_offset ) . unwrap ( ) ; // Encode log line. let line_buf = log_line . raw_data ; // Encode time stamp. let mut ts_buf = [ 0u8 ; mem :: size_of :: < u64 > ( ) ] ; ts_buf . as_mut ( ) . write_u64 :: < LittleEndian > ( log_line . ts ) . unwrap ( ) ; let indexes = log_line . indexes . drain ( 0 .. log_line . indexes . len ( ) ) ; // we'll track of all the offsets because If no search given, we should give // all the lines. self . entry_offsets . extend ( line_offset . to_vec ( ) ) ; for term in indexes { self . index_size = self . index_size + term . len ( ) ; if let Some ( posting_list ) = self . index_posting_list . get_mut ( & term ) { posting_list . extend ( line_offset . to_vec ( ) . iter ( ) ) ; } else { self . index_posting_list . insert ( term . clone ( ) , line_offset . to_vec ( ) ) ; } } // Populate posting list of json keys as well. let json_keys = log_line . json_keys . drain ( 0 .. log_line . json_keys . len ( ) ) ; for key in json_keys { if let Some ( posting_list ) = self . json_key_posting_list . get_mut ( & key ) { posting_list . extend ( line_offset . to_vec ( ) . iter ( ) ) ; } else { self . json_key_posting_list . insert ( key , line_offset . to_vec ( ) ) ; } } // TS LENGTH + STRUCTURED KEY + LINE LENGTH. let entry_length = ( line_buf . len ( ) + ts_buf . len ( ) + 1 ) as u64 ; // advance the file offset. self . file_offset = self . file_offset + entry_length + 8 ; // encode entry length. let mut len_buf = [ 0u8 ; mem :: size_of :: < u64 > ( ) ] ; len_buf . as_mut ( ) . write_u64 :: < LittleEndian > ( entry_length ) . unwrap ( ) ; buf . push ( len_buf . to_vec ( ) ) ; buf . push ( ts_buf . to_vec ( ) ) ; // If it is json set the 1 otherwise set 0. if log_line . structured { buf . push ( vec ! [ STRUCTURED_DATA ] ) ; } else { buf . push ( vec ! [ UN_STRUCTURED_DATA ] ) ; } // index this line. buf . push ( line_buf ) ; // advance line ts. // assert!(self.end_ts < log_line.ts); self . end_ts = log_line . ts ; } let mut io_slices = Vec :: new ( ) ; for slice in buf . iter ( ) { io_slices . push ( IoSlice :: new ( slice ) ) ; } // write them in a single syscall. But we have to verify // the performance here. self . segment_file . write_vectored ( & io_slices ) ? ; self . segment_file . flush ( ) ? ; self . batch . clear ( ) ; self . flushed_offset = self . file_offset ; Ok ( ( ) ) }
flush writes all the batched logline to the segment file.
flush writes all the batched logline to the segment file .
&mut self