diff options
Diffstat (limited to 'shared/src/store.rs')
-rw-r--r-- | shared/src/store.rs | 33 |
1 files changed, 33 insertions, 0 deletions
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<u128, &[u8]> = TableDefinition::new("e"); + +pub struct ResourceStore { + db: Database, +} +impl ResourceStore { + pub fn new(path: &Path) -> Result<Self> { + Ok(Self { + db: Database::create(path)?, + }) + } + pub fn get(&self, key: Resource) -> Result<Option<Vec<u8>>> { + 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(()) + } +} |