pub fn compact(&mut self) -> Result<()> {
        // increase current gen by 2. current_gen + 1 is for the compaction file
        let compaction_gen = self.current_gen + 1;
        self.current_gen += 2;
        self.writer = self.new_log_file(self.current_gen)?;

        let mut compaction_writer = self.new_log_file(compaction_gen)?;

        let mut new_pos = 0; // pos in the new log file
        for cmd_pos in &mut self.index.values_mut() {
            let reader = self.readers.get_mut(&cmd_pos.gen).expect("Cannot find log reader");
            if reader.pos != cmd_pos.pos {
                reader.seek(SeekFrom::Start(cmd_pos.pos))?;
            }

            let mut entry_reader = reader.take(cmd_pos.len);
            let len = io::copy(&mut entry_reader, &mut compaction_writer)?;
            *cmd_pos = (compaction_gen, new_pos..new_pos + len).into();
            new_pos += len;
        }
        compaction_writer.flush()?;

        // remove stale log files
        let stale_gens: Vec<_> =
            self.readers.keys().filter(|&&gen| gen < compaction_gen).cloned().collect();
        for stale_gen in stale_gens {
            self.readers.remove(&stale_gen);
            fs::remove_file(log_path(&self.path, stale_gen))?;
        }

        self.uncompacted = 0;

        Ok(())
    }
/// Clears stale entries in the log.
Clears stale entries in the log.
Clears stale entries in the log .
pub fn compact(&mut self) -> Result<()> {
        // increase current gen by 2. current_gen + 1 is for the compaction file
        let compaction_gen = self.current_gen + 1;
        self.current_gen += 2;
        self.writer = self.new_log_file(self.current_gen)?;
        let mut compaction_writer = self.new_log_file(compaction_gen)?;
        let mut new_pos = 0; // pos in the new log file
        for cmd_pos in &mut self.index.values_mut() {
            let reader = self.readers.get_mut(&cmd_pos.gen).expect("Cannot find log reader");
            if reader.pos != cmd_pos.pos {
                reader.seek(SeekFrom::Start(cmd_pos.pos))?;
            }
            let mut entry_reader = reader.take(cmd_pos.len);
            let len = io::copy(&mut entry_reader, &mut compaction_writer)?;
            *cmd_pos = (compaction_gen, new_pos..new_pos + len).into();
            new_pos += len;
        }
        compaction_writer.flush()?;
        // remove stale log files
        let stale_gens: Vec<_> =
            self.readers.keys().filter(|&&gen| gen < compaction_gen).cloned().collect();
        for stale_gen in stale_gens {
            self.readers.remove(&stale_gen);
            fs::remove_file(log_path(&self.path, stale_gen))?;
        }
        self.uncompacted = 0;
        Ok(())
    }
pub fn compact ( & mut self ) -> Result < ( ) > { // increase current gen by 2. current_gen + 1 is for the compaction file let compaction_gen = self . current_gen + 1 ; self . current_gen += 2 ; self . writer = self . new_log_file ( self . current_gen ) ? ; let mut compaction_writer = self . new_log_file ( compaction_gen ) ? ; let mut new_pos = 0 ; // pos in the new log file for cmd_pos in & mut self . index . values_mut ( ) { let reader = self . readers . get_mut ( & cmd_pos . gen ) . expect ( " " ) ; if reader . pos != cmd_pos . pos { reader . seek ( SeekFrom :: Start ( cmd_pos . pos ) ) ? ; } let mut entry_reader = reader . take ( cmd_pos . len ) ; let len = io :: copy ( & mut entry_reader , & mut compaction_writer ) ? ; * cmd_pos = ( compaction_gen , new_pos .. new_pos + len ) . into ( ) ; new_pos += len ; } compaction_writer . flush ( ) ? ; // remove stale log files let stale_gens : Vec < _ > = self . readers . keys ( ) . filter ( | & & gen | gen < compaction_gen ) . cloned ( ) . collect ( ) ; for stale_gen in stale_gens { self . readers . remove ( & stale_gen ) ; fs :: remove_file ( log_path ( & self . path , stale_gen ) ) ? ; } self . uncompacted = 0 ; Ok ( ( ) ) }
Clears stale entries in the log.
Clears stale entries in the log .
&mut self
&mut self