Added very basic storage via RocksDB.

This commit is contained in:
Micheal Smith
2025-11-05 23:22:12 -06:00
parent 4876ac631a
commit 05b46e7b1d
3 changed files with 248 additions and 2 deletions

View File

@@ -1,12 +1,65 @@
use color_eyre::Result;
use rocksdb::DB;
use std::path::{Path, PathBuf};
pub struct Store {
database: DB,
path: PathBuf,
}
// As simple as it gets for now.
impl Store {
pub async fn new(path: impl AsRef<Path>) -> Store {
Store { path: path.as_ref().to_path_buf() }
pub async fn new(path: impl AsRef<Path>) -> Result<Store> {
let db = DB::open_default(path.as_ref())?;
Ok(Store {
database: db,
path: path.as_ref().to_path_buf(),
})
}
pub async fn put(&mut self, key: impl AsRef<[u8]>, val: impl AsRef<[u8]>) -> Result<()> {
Ok(self.database.put(key, val)?)
}
// Use get_pinned to avoid copy
pub async fn get(&self, key: impl AsRef<[u8]>) -> Result<Option<Vec<u8>>> {
Ok(self.database.get(key)?)
}
}
#[cfg(test)]
mod tests {
use rstest::*;
use super::*;
use tempfile::tempdir;
#[fixture]
async fn temp_store() -> Store {
let tmp_dir = tempdir().unwrap();
let path = tmp_dir.path();
Store::new(path).await.unwrap()
}
#[tokio::test]
async fn test_create() {
let tmp_dir = tempdir().unwrap();
let path = tmp_dir.path();
let store = Store::new(path).await.unwrap();
}
#[rstest]
#[tokio::test]
async fn test_put(#[future] temp_store: Store) {
let mut store = temp_store.await;
store.put("one", "two").await.unwrap();
}
#[rstest]
#[tokio::test]
async fn test_get(#[future] temp_store: Store) {
let mut store = temp_store.await;
store.put("one", "two").await.unwrap();
assert_eq!(store.get("one").await.unwrap(), Some("two".as_bytes().to_vec()));
}
}