aboutsummaryrefslogtreecommitdiff
path: root/database/src/backends/redb.rs
diff options
context:
space:
mode:
Diffstat (limited to 'database/src/backends/redb.rs')
-rw-r--r--database/src/backends/redb.rs57
1 files changed, 27 insertions, 30 deletions
diff --git a/database/src/backends/redb.rs b/database/src/backends/redb.rs
index d1a0f8c..70623b5 100644
--- a/database/src/backends/redb.rs
+++ b/database/src/backends/redb.rs
@@ -4,50 +4,48 @@
Copyright (C) 2026 metamuffin <metamuffin.org>
*/
-use crate::backends::{Db, ReadTransaction, ReadTxnFunction, WriteTransaction, WriteTxnFunction};
+use crate::backends::{
+ Database, ReadTransaction, ReadTxnFunction, WriteTransaction, WriteTxnFunction,
+};
use anyhow::Result;
-use redb::{AccessGuard, Database, ReadableDatabase, ReadableTable, StorageError, TableDefinition};
+use redb::{AccessGuard, ReadableDatabase, ReadableTable, StorageError, Table, TableDefinition};
use std::path::Path;
-pub struct Redb {
- db: Database,
-}
-
const TABLE: TableDefinition<&[u8], &[u8]> = TableDefinition::new("kv");
-impl Redb {
- pub fn new(path: &Path) -> Result<Self> {
- Ok(Self {
- db: Database::create(path)?,
- })
- }
+pub fn new(path: &Path) -> Result<redb::Database> {
+ Ok(redb::Database::create(path)?)
}
-impl Db for Redb {
+
+impl Database for redb::Database {
fn write_transaction(&self, f: &mut WriteTxnFunction) -> Result<()> {
- let mut txn = self.db.begin_write()?;
- f(&mut txn)?;
+ let txn = self.begin_write()?;
+ let mut table = txn.open_table(TABLE)?;
+ f(&mut table)?;
+ drop(table);
txn.commit()?;
Ok(())
}
fn read_transaction(&self, f: &mut ReadTxnFunction) -> Result<()> {
- let mut txn = self.db.begin_read()?;
+ let mut txn = self.begin_read()?;
f(&mut txn)?;
Ok(())
}
}
-impl WriteTransaction for redb::WriteTransaction {
+
+impl WriteTransaction for Table<'_, &[u8], &[u8]> {
fn set(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
- self.open_table(TABLE)?.insert(key, value)?;
+ self.insert(key, value)?;
Ok(())
}
fn del(&mut self, key: &[u8]) -> Result<()> {
- self.open_table(TABLE)?.remove(key)?;
+ self.remove(key)?;
Ok(())
}
}
-impl ReadTransaction for redb::WriteTransaction {
+impl ReadTransaction for Table<'_, &[u8], &[u8]> {
fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> {
- match self.open_table(TABLE)?.get(key)? {
+ match <Self as ReadableTable<_, _>>::get(self, key)? {
Some(v) => Ok(Some(v.value().to_vec())),
None => Ok(None),
}
@@ -57,15 +55,14 @@ impl ReadTransaction for redb::WriteTransaction {
key: &[u8],
reverse: bool,
) -> Result<Box<dyn Iterator<Item = Result<Vec<u8>>> + 'a>> {
- // let k = |e: Result<(AccessGuard<'_, &[u8]>, AccessGuard<'_, &[u8]>), StorageError>| {
- // e.map(|e| e.0.value().to_vec()).map_err(|e| e.into())
- // };
- // Ok(if reverse {
- // Box::new(self.open_table(TABLE)?.range(..=key)?.rev().map(k))
- // } else {
- // Box::new(self.open_table(TABLE)?.range(key..)?.map(k))
- // })
- todo!()
+ let k = |e: Result<(AccessGuard<'_, &[u8]>, AccessGuard<'_, &[u8]>), StorageError>| {
+ e.map(|e| e.0.value().to_vec()).map_err(|e| e.into())
+ };
+ Ok(if reverse {
+ Box::new(self.range(..=key)?.rev().map(k))
+ } else {
+ Box::new(self.range(key..)?.map(k))
+ })
}
}
impl ReadTransaction for redb::ReadTransaction {