diff options
Diffstat (limited to 'database/src/backends')
| -rw-r--r-- | database/src/backends/memory.rs | 42 | ||||
| -rw-r--r-- | database/src/backends/mod.rs | 7 | ||||
| -rw-r--r-- | database/src/backends/redb.rs | 59 | ||||
| -rw-r--r-- | database/src/backends/rocksdb.rs | 30 |
4 files changed, 71 insertions, 67 deletions
diff --git a/database/src/backends/memory.rs b/database/src/backends/memory.rs index 3c2fdea..302b232 100644 --- a/database/src/backends/memory.rs +++ b/database/src/backends/memory.rs @@ -41,33 +41,31 @@ impl ReadTransaction for RwLockWriteGuard<'_, Inner> { fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { Ok((**self).get(key).cloned()) } - fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { - Ok((**self) - .range(key.to_vec()..) - .next() - .map(|(k, _)| k.to_owned())) - } - fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { - Ok((**self) - .range(..key.to_vec()) - .next_back() - .map(|(k, _)| k.to_owned())) + fn iter<'a>( + &'a self, + key: &[u8], + reverse: bool, + ) -> Result<Box<dyn Iterator<Item = Result<Vec<u8>>> + 'a>> { + Ok(if reverse { + Box::new(self.range(key.to_vec()..).map(|e| Ok(e.0.to_vec()))) + } else { + Box::new(self.range(..=key.to_vec()).rev().map(|e| Ok(e.0.to_vec()))) + }) } } impl ReadTransaction for RwLockReadGuard<'_, Inner> { fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { Ok((**self).get(key).cloned()) } - fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { - Ok((**self) - .range(key.to_vec()..) - .next() - .map(|(k, _)| k.to_owned())) - } - fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { - Ok((**self) - .range(..key.to_vec()) - .next_back() - .map(|(k, _)| k.to_owned())) + fn iter<'a>( + &'a self, + key: &[u8], + reverse: bool, + ) -> Result<Box<dyn Iterator<Item = Result<Vec<u8>>> + 'a>> { + Ok(if reverse { + Box::new(self.range(key.to_vec()..).map(|e| Ok(e.0.to_vec()))) + } else { + Box::new(self.range(..=key.to_vec()).rev().map(|e| Ok(e.0.to_vec()))) + }) } } diff --git a/database/src/backends/mod.rs b/database/src/backends/mod.rs index a95b00a..dd028f4 100644 --- a/database/src/backends/mod.rs +++ b/database/src/backends/mod.rs @@ -25,8 +25,11 @@ pub trait WriteTransaction: ReadTransaction { } pub trait ReadTransaction { fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>; - fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>>; - fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>>; + fn iter<'a>( + &'a self, + key: &[u8], + reverse: bool, + ) -> Result<Box<dyn Iterator<Item = Result<Vec<u8>>> + 'a>>; } pub fn create_backend(driver: &str, path: &Path) -> Result<Arc<dyn Db>> { 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)) + }) } } diff --git a/database/src/backends/rocksdb.rs b/database/src/backends/rocksdb.rs index cdcb60a..92c6a6d 100644 --- a/database/src/backends/rocksdb.rs +++ b/database/src/backends/rocksdb.rs @@ -6,7 +6,7 @@ use crate::backends::{Db, ReadTransaction, WriteTransaction, WriteTxnFunction}; use anyhow::Result; -use rocksdb::{ErrorKind, OptimisticTransactionDB}; +use rocksdb::{Direction, ErrorKind, IteratorMode, OptimisticTransactionDB}; use std::path::Path; pub struct Rocksdb { @@ -57,17 +57,23 @@ impl ReadTransaction for rocksdb::Transaction<'_, OptimisticTransactionDB> { fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { Ok(self.get(key)?) } - fn next(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { - let mut it = self.raw_iterator(); - it.seek_for_prev(key); - it.next(); - Ok(it.key().map(Vec::from)) - } - fn prev(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { - let mut it = self.raw_iterator(); - it.seek(key); - it.prev(); - Ok(it.key().map(Vec::from)) + fn iter<'a>( + &'a self, + key: &[u8], + reverse: bool, + ) -> Result<Box<dyn Iterator<Item = Result<Vec<u8>>> + 'a>> { + let mut iter = self.iterator(IteratorMode::Start); + iter.set_mode(IteratorMode::From( + key, + if reverse { + Direction::Reverse + } else { + Direction::Forward + }, + )); + Ok(Box::new(iter.map(|e| { + e.map(|(k, _)| k.into_vec()).map_err(|e| e.into()) + }))) } } |