fn last_id(file: &mut File, length: u32, read_chunk_size: u32) -> io::Result<Option<u32>> {
    let mut buf = vec![0u8; read_chunk_size as usize];
    let mut total = 0;
    let mut items = 0;

    while total < length {
        let pos = length - total;

        let starting_at = if pos < read_chunk_size {
            0
        } else {
            pos - read_chunk_size
        };

        let bytes_to_read = pos - starting_at;
        file.seek(SeekFrom::Start(starting_at as u64))?;
        file.read_exact(&mut buf[0..bytes_to_read as usize])?;
        total += bytes_to_read;

        let mut i = 1;

        while i < bytes_to_read {
            let p = bytes_to_read - i;

            if buf[p as usize] == MARKER_SEPARATOR && items > 0 {
                return Ok(Some(pos - p));
            } else if buf[p as usize] == MARKER_SEPARATOR {
                items += 1;
            }

            i += 1;
        }
    }

    if length == 0 {
        Ok(None)
    } else if items == 0 {
        Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "file missing record separator",
        ))
    } else {
        Ok(Some(0))
    }
}
/// Given an open file and its total length, extract the last id
/// that was written. Note that this by design only works with
/// 32bit unsigned integers in length, so the caller must validate
/// this before hand.
Given an open file and its total length, extract the last id
that was written. Note that this by design only works with
32bit unsigned integers in length, so the caller must validate
this before hand.
Given an open file and its total length extract the last id that was written . Note that this by design only works with 32bit unsigned integers in length so the caller must validate this before hand .
fn last_id(file: &mut File, length: u32, read_chunk_size: u32) -> io::Result<Option<u32>> {
    let mut buf = vec![0u8; read_chunk_size as usize];
    let mut total = 0;
    let mut items = 0;
    while total < length {
        let pos = length - total;
        let starting_at = if pos < read_chunk_size {
            0
        } else {
            pos - read_chunk_size
        };
        let bytes_to_read = pos - starting_at;
        file.seek(SeekFrom::Start(starting_at as u64))?;
        file.read_exact(&mut buf[0..bytes_to_read as usize])?;
        total += bytes_to_read;
        let mut i = 1;
        while i < bytes_to_read {
            let p = bytes_to_read - i;
            if buf[p as usize] == MARKER_SEPARATOR && items > 0 {
                return Ok(Some(pos - p));
            } else if buf[p as usize] == MARKER_SEPARATOR {
                items += 1;
            }
            i += 1;
        }
    }
    if length == 0 {
        Ok(None)
    } else if items == 0 {
        Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "file missing record separator",
        ))
    } else {
        Ok(Some(0))
    }
}
fn last_id ( file : & mut File , length : u32 , read_chunk_size : u32 ) -> io :: Result < Option < u32 > > { let mut buf = vec ! [ 0u8 read_chunk_size as usize ] ; let mut total = 0 ; let mut items = 0 ; while total < length { let pos = length - total ; let starting_at = if pos < read_chunk_size { 0 } else { pos - read_chunk_size } ; let bytes_to_read = pos - starting_at ; file . seek ( SeekFrom :: Start ( starting_at as u64 ) ) ? ; file . read_exact ( & mut buf [ 0 .. bytes_to_read as usize ] ) ? ; total += bytes_to_read ; let mut i = 1 ; while i < bytes_to_read { let p = bytes_to_read - i ; if buf [ p as usize ] == MARKER_SEPARATOR && items > 0 { return Ok ( Some ( pos - p ) ) ; } else if buf [ p as usize ] == MARKER_SEPARATOR { items += 1 ; } i += 1 ; } } if length == 0 { Ok ( None ) } else if items == 0 { Err ( io :: Error :: new ( io :: ErrorKind :: InvalidInput , " " , ) ) } else { Ok ( Some ( 0 ) ) } }
Given an open file and its total length, extract the last id
that was written.
Given an open file and its total length extract the last id that was written .
file
&mut File
length
u32
read_chunk_size
u32
file
&mut File
length
u32
read_chunk_size
u32