diff options
| author | metamuffin <metamuffin@disroot.org> | 2025-12-24 09:02:17 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2025-12-24 09:02:17 +0100 |
| commit | d543f6fe11a32dcead2310f1fb4c2abd303f5f8c (patch) | |
| tree | 3ba8274f0906250809ca5fa1b08c9b5f53b88cc7 /database/src/backends/redb.rs | |
| parent | eac0de36221440571fe686074b04b71bf98cf727 (diff) | |
| download | jellything-d543f6fe11a32dcead2310f1fb4c2abd303f5f8c.tar jellything-d543f6fe11a32dcead2310f1fb4c2abd303f5f8c.tar.bz2 jellything-d543f6fe11a32dcead2310f1fb4c2abd303f5f8c.tar.zst | |
db abstraction looks good
Diffstat (limited to 'database/src/backends/redb.rs')
| -rw-r--r-- | database/src/backends/redb.rs | 59 |
1 files changed, 28 insertions, 31 deletions
diff --git a/database/src/backends/redb.rs b/database/src/backends/redb.rs index d2849de..409a551 100644 --- a/database/src/backends/redb.rs +++ b/database/src/backends/redb.rs @@ -6,7 +6,7 @@ use crate::backends::{Db, ReadTransaction, ReadTxnFunction, WriteTransaction, WriteTxnFunction}; use anyhow::Result; -use redb::{Database, ReadableDatabase, ReadableTable, TableDefinition}; +use redb::{AccessGuard, Database, ReadableDatabase, ReadableTable, StorageError, TableDefinition}; use std::path::Path; pub struct Redb { @@ -52,21 +52,20 @@ impl ReadTransaction for redb::WriteTransaction { 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), - } + fn iter<'a>( + &'a self, + 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!() } } impl ReadTransaction for redb::ReadTransaction { @@ -76,20 +75,18 @@ impl ReadTransaction for redb::ReadTransaction { 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), - } + fn iter<'a>( + &'a self, + 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)) + }) } } |