blob: 83f1a25ea354884777f098af43829e6f6dfe5523 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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(())
}
}
|