fn load(
    gen: u64,
    reader: &mut BufReaderWithPos<File>,
    index: &mut BTreeMap<String, CommandPos>,
) -> Result<u64> {
    // To make sure we read from the beginning of the file
    let mut pos = reader.seek(SeekFrom::Start(0))?;
    let mut stream = Deserializer::from_reader(reader).into_iter::<Command>();
    let mut uncompacted = 0; // number of bytes that can be saved after a compaction
    while let Some(cmd) = stream.next() {
        let new_pos = stream.byte_offset() as u64;
        match cmd? {
            Command::Set { key, .. } => {
                if let Some(old_cmd) = index.insert(key, (gen, pos..new_pos).into()) {
                    uncompacted += old_cmd.len;
                }
            }
            Command::Remove { key } => {
                if let Some(old_cmd) = index.remove(&key) {
                    uncompacted += old_cmd.len;
                }
                // the "remove" command itself can be deleted in the next compaction
                // so we add its length to `uncompacted`
                uncompacted += new_pos - pos;
            }
        }
        pos = new_pos;
    }
    Ok(uncompacted)
}
/// Load the whole log file and store value locations in the index map.
///
/// Returns how many bytes can be saved after a compaction.
Load the whole log file and store value locations in the index map.
Returns how many bytes can be saved after a compaction.
Load the whole log file and store value locations in the index map . Returns how many bytes can be saved after a compaction .
fn load(
    gen: u64,
    reader: &mut BufReaderWithPos<File>,
    index: &mut BTreeMap<String, CommandPos>,
) -> Result<u64> {
    // To make sure we read from the beginning of the file
    let mut pos = reader.seek(SeekFrom::Start(0))?;
    let mut stream = Deserializer::from_reader(reader).into_iter::<Command>();
    let mut uncompacted = 0; // number of bytes that can be saved after a compaction
    while let Some(cmd) = stream.next() {
        let new_pos = stream.byte_offset() as u64;
        match cmd? {
            Command::Set { key, .. } => {
                if let Some(old_cmd) = index.insert(key, (gen, pos..new_pos).into()) {
                    uncompacted += old_cmd.len;
                }
            }
            Command::Remove { key } => {
                if let Some(old_cmd) = index.remove(&key) {
                    uncompacted += old_cmd.len;
                }
                // the "remove" command itself can be deleted in the next compaction
                // so we add its length to `uncompacted`
                uncompacted += new_pos - pos;
            }
        }
        pos = new_pos;
    }
    Ok(uncompacted)
}
fn load ( gen : u64 , reader : & mut BufReaderWithPos < File > , index : & mut BTreeMap < String , CommandPos > , ) -> Result < u64 > { // To make sure we read from the beginning of the file let mut pos = reader . seek ( SeekFrom :: Start ( 0 ) ) ? ; let mut stream = Deserializer :: from_reader ( reader ) . into_iter :: < Command > ( ) ; let mut uncompacted = 0 ; // number of bytes that can be saved after a compaction while let Some ( cmd ) = stream . next ( ) { let new_pos = stream . byte_offset ( ) as u64 ; match cmd ? { Command :: Set { key , .. } => { if let Some ( old_cmd ) = index . insert ( key , ( gen , pos .. new_pos ) . into ( ) ) { uncompacted += old_cmd . len ; } } Command :: Remove { key } => { if let Some ( old_cmd ) = index . remove ( & key ) { uncompacted += old_cmd . len ; } // the "remove" command itself can be deleted in the next compaction // so we add its length to `uncompacted` uncompacted += new_pos - pos ; } } pos = new_pos ; } Ok ( uncompacted ) }
Load the whole log file and store value locations in the index map.
Load the whole log file and store value locations in the index map .
gen
u64
reader
&mut BufReaderWithPos<File>
index
&mut BTreeMap<String, CommandPos>
gen
u64
reader
&mut BufReaderWithPos<File>
index
&mut BTreeMap<String, CommandPos>