2022-05-05 15:50:44 +02:00
|
|
|
use lmdb::{DatabaseFlags, Environment, RwTransaction, Transaction, WriteFlags};
|
2021-10-07 16:44:01 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct RawDB {
|
|
|
|
db: lmdb::Database,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RawDB {
|
|
|
|
pub fn open(env: &Environment, name: Option<&str>) -> lmdb::Result<Self> {
|
|
|
|
env.open_db(name).map(|db| Self { db })
|
|
|
|
}
|
2022-05-05 15:50:44 +02:00
|
|
|
|
|
|
|
pub fn create(
|
|
|
|
env: &Environment,
|
|
|
|
name: Option<&str>,
|
|
|
|
flags: DatabaseFlags,
|
|
|
|
) -> lmdb::Result<Self> {
|
2021-10-07 16:44:01 +02:00
|
|
|
env.create_db(name, flags).map(|db| Self { db })
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:50:44 +02:00
|
|
|
pub fn get<'txn, T: Transaction, K>(
|
|
|
|
&self,
|
|
|
|
txn: &'txn T,
|
|
|
|
key: &K,
|
|
|
|
) -> lmdb::Result<Option<&'txn [u8]>>
|
|
|
|
where
|
|
|
|
K: AsRef<[u8]>,
|
2021-10-07 16:44:01 +02:00
|
|
|
{
|
|
|
|
match txn.get(self.db, key) {
|
|
|
|
Ok(buf) => Ok(Some(buf)),
|
|
|
|
Err(lmdb::Error::NotFound) => Ok(None),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:50:44 +02:00
|
|
|
pub fn put<K, V>(
|
|
|
|
&self,
|
|
|
|
txn: &mut RwTransaction,
|
|
|
|
key: &K,
|
|
|
|
value: &V,
|
|
|
|
flags: WriteFlags,
|
|
|
|
) -> lmdb::Result<()>
|
|
|
|
where
|
|
|
|
K: AsRef<[u8]>,
|
|
|
|
V: AsRef<[u8]>,
|
2021-10-07 16:44:01 +02:00
|
|
|
{
|
|
|
|
txn.put(self.db, key, value, flags)
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:50:44 +02:00
|
|
|
pub fn reserve<'txn, K>(
|
|
|
|
&self,
|
|
|
|
txn: &'txn mut RwTransaction,
|
|
|
|
key: &K,
|
|
|
|
size: usize,
|
|
|
|
flags: WriteFlags,
|
|
|
|
) -> lmdb::Result<&'txn mut [u8]>
|
|
|
|
where
|
|
|
|
K: AsRef<[u8]>,
|
2021-10-07 16:44:01 +02:00
|
|
|
{
|
|
|
|
txn.reserve(self.db, key, size, flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn del<K, V>(&self, txn: &mut RwTransaction, key: &K, value: Option<&V>) -> lmdb::Result<()>
|
2022-05-05 15:50:44 +02:00
|
|
|
where
|
|
|
|
K: AsRef<[u8]>,
|
|
|
|
V: AsRef<[u8]>,
|
2021-10-07 16:44:01 +02:00
|
|
|
{
|
|
|
|
txn.del(self.db, key, value.map(AsRef::as_ref))
|
|
|
|
}
|
|
|
|
|
2022-04-30 20:52:32 +02:00
|
|
|
pub fn clear(&self, txn: &mut RwTransaction) -> lmdb::Result<()> {
|
|
|
|
txn.clear_db(self.db)
|
|
|
|
}
|
|
|
|
|
2021-10-07 16:44:01 +02:00
|
|
|
pub fn iter<'txn, C: lmdb::Cursor<'txn>>(&self, cursor: &'txn mut C) -> lmdb::Iter<'txn> {
|
|
|
|
cursor.iter_start()
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:50:44 +02:00
|
|
|
pub fn open_ro_cursor<'txn, T: Transaction>(
|
|
|
|
&self,
|
|
|
|
txn: &'txn T,
|
|
|
|
) -> lmdb::Result<lmdb::RoCursor<'txn>> {
|
2021-10-07 16:44:01 +02:00
|
|
|
txn.open_ro_cursor(self.db)
|
|
|
|
}
|
|
|
|
}
|