Image

prompt

Think in step by step, write a Memory Mapped Log Structured Hash Table implementation that implements the IDatabase trait in Rust. The implementation should use the crate following crates: ``` serde = "1.0" serde_json = "1.0" memmap2 = "0.9" ``` You may use any number of other crates if you see it fit Implement the data structure in the memmap2 byte array, such that it is propperly mapped to disk, and uses incremental append operations to take full advantage of memory mapped file io. ```rs /// IDatabase trait abstracts the underlying Database implementation /// ``` pub trait IDatabase: Send + Sync { /// Checks if the sensor with id exists in the Database fn contains(&self, id: &str) -> bool; /// Get the record with rec_id from the sensor with id in the /// Database /// /// May load data from disk, if it is not in-memory, /// as such it is mutable, even though a get normaly /// is immutable. fn get(&mut self, id: &str, rec_id: usize) -> Vec<u8>; /// Post a record with value to the sensor with id in the Database fn post(&mut self, id: &str, value: &[u8]); /// Get metadata from the sensor with id in the Database fn get_meta(&mut self, id: &str) -> Vec<u8>; /// Post metadata with value to the sensor with id in the Database fn post_meta(&mut self, id: &str, value: Vec<u8>); /// Get aggregates from the sensor with id in the Database fn get_aggregates(&self, id: &str) -> Vec<u8>; /// Get the latest record from the sensor with id in the Database fn get_latest(&mut self, id: &str) -> Vec<u8>; /// Get the latest limit number of records from the sensor with id /// in the Database /// /// May load data from disk, if it is not in-memory, /// as such it is mutable, even though a get latest limit normaly is /// immutable. fn get_latest_with_limit(&mut self, id: &str, limit: usize) -> Vec<Vec<u8>>; /// Get a range from start to end of records from the sensor with /// id in the Database /// /// May load data from disk, if it is not in-memory, /// as such it is mutable, even though a range scan normaly is /// immutable. fn get_range(&mut self, id: &str, start: usize, end: usize) -> Vec<Vec<u8>>; /// Get metadata from all sensors in the Database fn get_all_meta(&mut self) -> std::collections::HashMap<&str, Vec<u8>>; /// Get aggregates from all sensors in the Database fn get_all_aggregates(&self) -> std::collections::HashMap<&str, Vec<u8>>; /// Get the latest record from all sensors in the Database fn get_all_latest(&mut self) -> std::collections::HashMap<&str, Vec<u8>>; /// Get the latest limit number of records from all sensors in the /// Database /// /// May load data from disk, if it is not in-memory, /// as such it is mutable, even though a get latest limit normaly is /// immutable. fn get_all_latest_with_limit( &mut self, limit: usize, ) -> std::collections::HashMap<&str, Vec<Vec<u8>>>; } ```