pub fn open(path: impl Into<PathBuf>) -> Result<KvStore> {
        let mut path = path.into();
        std::fs::create_dir_all(&path)?;
        let mut readers = HashMap::new();
        let mut index = HashMap::new();

        buf.push("database");
        if !buf.exists() {
            return Ok(KvStore {
                data_file: buf,
                offsets: HashMap::new(),
                operations: 0,
            });
        }
        let f = File::open(&buf)?;
        let md = std::fs::metadata(&buf)?;
        let file_size = md.len();
        debug!("file size: {:?}", md.len());
        let mut reader = BufReader::new(f);
        let mut size_buffer: [u8; 4] = [0; 4];
        let mut offset = 0;
        let mut offsets = HashMap::new();

        while offset < file_size {
            match reader.read_exact(&mut size_buffer) {
                Ok(_) => {
                    let data_size = u32::from_le_bytes(size_buffer) as usize;
                    debug!("data_size: {}", data_size);
                    let mut data_buffer: Vec<u8> = vec![0; data_size];
                    reader.read_exact(&mut data_buffer)?;
                    debug!("data: {:?}", data_buffer);
                    let pair: KvPair = serde_json::from_slice(&data_buffer)?;

                    if pair.value.is_some() {
                        offsets.insert(
                            pair.key,
                            Offset {
                                start: offset + 4,
                                len: data_size,
                            },
                        );
                    } else {
                        // the key is deleted
                        offsets.remove(&pair.key);
                    }
                    offset += 4 + data_size as u64;
                }
                Err(err) => return Err(IoError(err)),
            }
        }

        Ok(KvStore {
            data_file: buf,
            offsets,
            operations: 0,
        })
    }
/// Open a directory and return a KvStore object.
/// This will create a new directory if the given one doesn't exist yet.
Open a directory and return a KvStore object.
This will create a new directory if the given one doesn't exist yet.
Open a directory and return a KvStore object . This will create a new directory if the given one doesn ' t exist yet .
pub fn open(path: impl Into<PathBuf>) -> Result<KvStore> {
        let mut path = path.into();
        std::fs::create_dir_all(&path)?;
        let mut readers = HashMap::new();
        let mut index = HashMap::new();
        buf.push("database");
        if !buf.exists() {
            return Ok(KvStore {
                data_file: buf,
                offsets: HashMap::new(),
                operations: 0,
            });
        }
        let f = File::open(&buf)?;
        let md = std::fs::metadata(&buf)?;
        let file_size = md.len();
        debug!("file size: {:?}", md.len());
        let mut reader = BufReader::new(f);
        let mut size_buffer: [u8; 4] = [0; 4];
        let mut offset = 0;
        let mut offsets = HashMap::new();
        while offset < file_size {
            match reader.read_exact(&mut size_buffer) {
                Ok(_) => {
                    let data_size = u32::from_le_bytes(size_buffer) as usize;
                    debug!("data_size: {}", data_size);
                    let mut data_buffer: Vec<u8> = vec![0; data_size];
                    reader.read_exact(&mut data_buffer)?;
                    debug!("data: {:?}", data_buffer);
                    let pair: KvPair = serde_json::from_slice(&data_buffer)?;
                    if pair.value.is_some() {
                        offsets.insert(
                            pair.key,
                            Offset {
                                start: offset + 4,
                                len: data_size,
                            },
                        );
                    } else {
                        // the key is deleted
                        offsets.remove(&pair.key);
                    }
                    offset += 4 + data_size as u64;
                }
                Err(err) => return Err(IoError(err)),
            }
        }
        Ok(KvStore {
            data_file: buf,
            offsets,
            operations: 0,
        })
    }
pub fn open ( path : impl Into < PathBuf > ) -> Result < KvStore > { let mut path = path . into ( ) ; std :: fs :: create_dir_all ( & path ) ? ; let mut readers = HashMap :: new ( ) ; let mut index = HashMap :: new ( ) ; buf . push ( " " ) ; if ! buf . exists ( ) { return Ok ( KvStore { data_file : buf , offsets : HashMap :: new ( ) , operations : 0 , } ) ; } let f = File :: open ( & buf ) ? ; let md = std :: fs :: metadata ( & buf ) ? ; let file_size = md . len ( ) ; debug ! ( " " md len ( ) ) ; let mut reader = BufReader :: new ( f ) ; let mut size_buffer : [ u8 ; 4 ] = [ 0 ; 4 ] ; let mut offset = 0 ; let mut offsets = HashMap :: new ( ) ; while offset < file_size { match reader . read_exact ( & mut size_buffer ) { Ok ( _ ) => { let data_size = u32 :: from_le_bytes ( size_buffer ) as usize ; debug ! ( " " data_size ) ; let mut data_buffer : Vec < u8 > = vec ! [ 0 data_size ] ; reader . read_exact ( & mut data_buffer ) ? ; debug ! ( " " data_buffer ) ; let pair : KvPair = serde_json :: from_slice ( & data_buffer ) ? ; if pair . value . is_some ( ) { offsets . insert ( pair . key , Offset { start : offset + 4 , len : data_size , } , ) ; } else { // the key is deleted offsets . remove ( & pair . key ) ; } offset += 4 + data_size as u64 ; } Err ( err ) => return Err ( IoError ( err ) ) , } } Ok ( KvStore { data_file : buf , offsets , operations : 0 , } ) }
Open a directory and return a KvStore object.
Open a directory and return a KvStore object .
path
impl Into<PathBuf>
path
impl Into<PathBuf>