From 2707f03617478e2a5e521961c46c9c6511d5088d Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sat, 4 Jan 2025 22:52:42 +0100 Subject: a --- shared/src/store.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 shared/src/store.rs (limited to 'shared/src/store.rs') diff --git a/shared/src/store.rs b/shared/src/store.rs new file mode 100644 index 0000000..83f1a25 --- /dev/null +++ b/shared/src/store.rs @@ -0,0 +1,33 @@ +use crate::packets::Resource; +use anyhow::Result; +use redb::{Database, TableDefinition}; +use std::path::Path; + +const T_ENTRIES: TableDefinition = TableDefinition::new("e"); + +pub struct ResourceStore { + db: Database, +} +impl ResourceStore { + pub fn new(path: &Path) -> Result { + Ok(Self { + db: Database::create(path)?, + }) + } + pub fn get(&self, key: Resource) -> Result>> { + let txn = self.db.begin_read()?; + let ent = txn.open_table(T_ENTRIES)?; + match ent.get(key.0)? { + Some(x) => Ok(Some(x.value().to_vec())), + None => Ok(None), + } + } + pub fn set(&self, key: Resource, value: &[u8]) -> Result<()> { + let txn = self.db.begin_write()?; + let mut ent = txn.open_table(T_ENTRIES)?; + ent.insert(key.0, value)?; + drop(ent); + txn.commit()?; + Ok(()) + } +} -- cgit v1.2.3-70-g09d2