aboutsummaryrefslogtreecommitdiff
path: root/database/src/backends/redb.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-12-18 18:45:53 +0100
committermetamuffin <metamuffin@disroot.org>2025-12-18 18:45:53 +0100
commitda985cc06e4caa7501222dbf54f212536fd42b0c (patch)
tree106ebacb66abb8fd97a7be4e802ac45d8ce9852d /database/src/backends/redb.rs
parentfc7f3ae8e39a0398ceba7b9c44f58679c01a98da (diff)
downloadjellything-da985cc06e4caa7501222dbf54f212536fd42b0c.tar
jellything-da985cc06e4caa7501222dbf54f212536fd42b0c.tar.bz2
jellything-da985cc06e4caa7501222dbf54f212536fd42b0c.tar.zst
transaction interface
Diffstat (limited to 'database/src/backends/redb.rs')
-rw-r--r--database/src/backends/redb.rs67
1 files changed, 49 insertions, 18 deletions
diff --git a/database/src/backends/redb.rs b/database/src/backends/redb.rs
index 1b672b6..d2849de 100644
--- a/database/src/backends/redb.rs
+++ b/database/src/backends/redb.rs
@@ -4,11 +4,10 @@
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
-use std::path::Path;
-
-use crate::backends::KV;
+use crate::backends::{Db, ReadTransaction, ReadTxnFunction, WriteTransaction, WriteTxnFunction};
use anyhow::Result;
-use redb::{Database, ReadableDatabase, TableDefinition};
+use redb::{Database, ReadableDatabase, ReadableTable, TableDefinition};
+use std::path::Path;
pub struct Redb {
db: Database,
@@ -23,29 +22,62 @@ impl Redb {
})
}
}
-impl KV for Redb {
- fn set(&self, key: &[u8], value: &[u8]) -> Result<()> {
- let txn = self.db.begin_write()?;
- txn.open_table(TABLE)?.insert(key, value)?;
+impl Db for Redb {
+ fn write_transaction(&self, f: &mut WriteTxnFunction) -> Result<()> {
+ let mut txn = self.db.begin_write()?;
+ f(&mut txn)?;
txn.commit()?;
Ok(())
}
- fn del(&self, key: &[u8]) -> Result<()> {
- let txn = self.db.begin_write()?;
- txn.open_table(TABLE)?.remove(key)?;
- txn.commit()?;
+ fn read_transaction(&self, f: &mut ReadTxnFunction) -> Result<()> {
+ let mut txn = self.db.begin_read()?;
+ f(&mut txn)?;
Ok(())
}
+}
+impl WriteTransaction for redb::WriteTransaction {
+ fn set(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
+ self.open_table(TABLE)?.insert(key, value)?;
+ Ok(())
+ }
+ fn del(&mut self, key: &[u8]) -> Result<()> {
+ self.open_table(TABLE)?.remove(key)?;
+ Ok(())
+ }
+}
+impl ReadTransaction for redb::WriteTransaction {
+ fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ match self.open_table(TABLE)?.get(key)? {
+ Some(v) => Ok(Some(v.value().to_vec())),
+ None => Ok(None),
+ }
+ }
+ fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ let table = self.open_table(TABLE)?;
+ let mut iter = table.range(key..)?;
+ match iter.next() {
+ Some(k) => Ok(Some(k?.0.value().to_vec())),
+ None => Ok(None),
+ }
+ }
+ fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
+ let table = self.open_table(TABLE)?;
+ let mut iter = table.range(..key)?;
+ match iter.next_back() {
+ Some(k) => Ok(Some(k?.0.value().to_vec())),
+ None => Ok(None),
+ }
+ }
+}
+impl ReadTransaction for redb::ReadTransaction {
fn get<'a>(&'a self, key: &[u8]) -> Result<Option<Vec<u8>>> {
- let txn = self.db.begin_read()?;
- match txn.open_table(TABLE)?.get(key)? {
+ match self.open_table(TABLE)?.get(key)? {
Some(v) => Ok(Some(v.value().to_vec())),
None => Ok(None),
}
}
fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
- let txn = self.db.begin_read()?;
- let table = txn.open_table(TABLE)?;
+ let table = self.open_table(TABLE)?;
let mut iter = table.range(key..)?;
match iter.next() {
Some(k) => Ok(Some(k?.0.value().to_vec())),
@@ -53,8 +85,7 @@ impl KV for Redb {
}
}
fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
- let txn = self.db.begin_read()?;
- let table = txn.open_table(TABLE)?;
+ let table = self.open_table(TABLE)?;
let mut iter = table.range(..key)?;
match iter.next_back() {
Some(k) => Ok(Some(k?.0.value().to_vec())),