fabaccess-bffh/bffhd/resource/state/db.rs

196 lines
5.4 KiB
Rust
Raw Normal View History

2021-10-07 16:44:01 +02:00
use std::{
sync::Arc,
2021-10-07 16:44:01 +02:00
path::Path,
};
2021-12-06 21:53:42 +01:00
use rkyv::Archived;
2021-10-07 16:44:01 +02:00
2021-11-26 21:01:43 +01:00
use crate::db::{
2021-10-07 16:44:01 +02:00
DB,
Environment,
EnvironmentFlags,
DatabaseFlags,
WriteFlags,
Adapter,
AllocAdapter,
DBError,
2021-10-07 16:44:01 +02:00
Transaction,
RoTransaction,
2021-10-07 16:44:01 +02:00
RwTransaction,
LMDBorrow,
};
2021-10-07 16:44:01 +02:00
2021-11-26 02:25:48 +01:00
use crate::resource::state::State;
type StateAdapter = AllocAdapter<State>;
2021-10-07 16:44:01 +02:00
/// State Database containing the currently set state
#[derive(Clone, Debug)]
2021-10-07 16:44:01 +02:00
pub struct StateDB {
/// The environment for all the databases below
env: Arc<Environment>,
2021-10-07 16:44:01 +02:00
input: DB<StateAdapter>,
output: DB<StateAdapter>,
2021-10-07 16:44:01 +02:00
// TODO: Index resource name/id/uuid -> u64
}
impl StateDB {
fn open_env<P: AsRef<Path>>(path: P) -> lmdb::Result<Environment> {
2021-10-07 16:44:01 +02:00
Environment::new()
.set_flags( EnvironmentFlags::WRITE_MAP
| EnvironmentFlags::NO_SUB_DIR
| EnvironmentFlags::NO_TLS
| EnvironmentFlags::NO_READAHEAD)
.set_max_dbs(2)
.open(path.as_ref())
}
2021-10-07 16:44:01 +02:00
fn new(env: Environment, input: DB<StateAdapter>, output: DB<StateAdapter>) -> Self {
Self { env: Arc::new(env), input, output }
}
pub fn init<P: AsRef<Path>>(path: P) -> lmdb::Result<Self> {
2021-10-07 16:44:01 +02:00
let env = Self::open_env(path)?;
let input = unsafe {
DB::create(&env, Some("input"), DatabaseFlags::INTEGER_KEY)?
};
let output = unsafe {
DB::create(&env, Some("output"), DatabaseFlags::INTEGER_KEY)?
};
2021-10-07 16:44:01 +02:00
Ok(Self::new(env, input, output))
}
pub fn open<P: AsRef<Path>>(path: P) -> lmdb::Result<Self> {
2021-10-07 16:44:01 +02:00
let env = Self::open_env(path)?;
let input = unsafe { DB::open(&env, Some("input"))? };
let output = unsafe { DB::open(&env, Some("output"))? };
2021-10-07 16:44:01 +02:00
Ok(Self::new(env, input, output))
}
2021-10-20 18:37:50 +02:00
pub fn create<P: AsRef<Path>>(path: P) -> lmdb::Result<Self> {
let flags = DatabaseFlags::empty();
let env = Self::open_env(path)?;
let input = unsafe { DB::create(&env, Some("input"), flags)? };
let output = unsafe { DB::create(&env, Some("output"), flags)? };
Ok(Self::new(env, input, output))
}
2021-10-07 16:44:01 +02:00
fn update_txn(&self, txn: &mut RwTransaction, key: u64, input: &State, output: &State)
-> Result<(), DBError>
2021-10-07 16:44:01 +02:00
{
let flags = WriteFlags::empty();
let k = key.to_ne_bytes();
self.input.put(txn, &k, input, flags)?;
self.output.put(txn, &k, output, flags)?;
Ok(())
}
pub fn update(&self, key: u64, input: &State, output: &State)
-> Result<(), DBError>
{
2021-10-07 16:44:01 +02:00
let mut txn = self.env.begin_rw_txn().map_err(StateAdapter::from_db_err)?;
self.update_txn(&mut txn, key, input, output)?;
2021-10-07 16:44:01 +02:00
txn.commit().map_err(StateAdapter::from_db_err)
}
fn get(&self, db: &DB<StateAdapter>, key: u64)
-> Result<Option<LMDBorrow<RoTransaction, Archived<State>>>, DBError>
{
let txn = self.env.begin_ro_txn().map_err(StateAdapter::from_db_err)?;
if let Some(state) = db.get(&txn, &key.to_ne_bytes())? {
let ptr = state.into();
Ok(Some(unsafe { LMDBorrow::new(ptr, txn) }))
} else {
Ok(None)
}
}
#[inline(always)]
pub fn get_input(&self, key: u64)
-> Result<Option<LMDBorrow<RoTransaction, Archived<State>>>, DBError>
{ self.get(&self.input, key) }
#[inline(always)]
pub fn get_output(&self, key: u64)
-> Result<Option<LMDBorrow<RoTransaction, Archived<State>>>, DBError>
{ self.get(&self.output, key) }
pub fn accessor(&self, key: u64) -> StateAccessor {
StateAccessor::new(key, self.clone())
}
}
#[derive(Debug)]
pub struct StateAccessor {
key: u64,
db: StateDB
}
impl StateAccessor {
pub fn new(key: u64, db: StateDB) -> Self {
Self { key, db }
}
pub fn get_input(&self)
-> Result<Option<LMDBorrow<RoTransaction, Archived<State>>>, DBError>
{
self.db.get_input(self.key)
}
pub fn get_output(&self)
-> Result<Option<LMDBorrow<RoTransaction, Archived<State>>>, DBError>
{
self.db.get_output(self.key)
}
pub fn set(&self, input: &State, output: &State) -> Result<(), DBError> {
self.db.update(self.key, input, output)
}
}
2021-10-18 11:27:42 +02:00
#[cfg(test)]
mod tests {
use super::*;
2021-11-26 02:25:48 +01:00
use crate::resource::state::value::Vec3u8;
use crate::resource::state::value::{OID_COLOUR, OID_POWERED, OID_INTENSITY};
2021-10-18 11:27:42 +02:00
use std::ops::Deref;
#[test]
fn construct_state() {
2021-10-18 11:27:42 +02:00
let tmpdir = tempfile::tempdir().unwrap();
let mut tmppath = tmpdir.path().to_owned();
tmppath.push("db");
let db = StateDB::init(tmppath).unwrap();
let b = State::build()
2021-10-18 11:27:42 +02:00
.add(OID_COLOUR.clone(), Box::new(Vec3u8 { a: 1, b: 2, c: 3}))
.add(OID_POWERED.clone(), Box::new(true))
.add(OID_INTENSITY.clone(), Box::new(1023))
.finish();
println!("({}) {:?}", b.hash(), b);
2021-10-18 11:27:42 +02:00
let c = State::build()
.add(OID_COLOUR.clone(), Box::new(Vec3u8 { a: 1, b: 2, c: 3}))
.add(OID_POWERED.clone(), Box::new(true))
.add(OID_INTENSITY.clone(), Box::new(1023))
.finish();
2021-10-18 11:27:42 +02:00
let key = rand::random();
db.update(key, &b, &c).unwrap();
let d = db.get_input(key).unwrap().unwrap();
let e = db.get_output(key).unwrap().unwrap();
assert_eq!(&b, d.deref());
assert_eq!(&c, e.deref());
}
}