refactor: use reverse walker (#938)

Co-authored-by: lambdaclass-user <github@lambdaclass.com>
This commit is contained in:
Mariano A. Nicolini
2023-01-20 14:38:26 -03:00
committed by GitHub
parent d86c9890bd
commit 25e9b399f3
9 changed files with 70 additions and 31 deletions

View File

@ -34,6 +34,16 @@ pub trait DbCursorRO<'tx, T: Table> {
) -> Result<Walker<'cursor, 'tx, T, Self>, Error>
where
Self: Sized;
/// Returns an iterator that walks backwards through the table. If `start_key`
/// is None, starts from the last entry of the table. If it not, starts at a key
/// greater or equal than the key value wrapped inside Some().
fn walk_back<'cursor>(
&'cursor mut self,
start_key: Option<T::Key>,
) -> Result<ReverseWalker<'cursor, 'tx, T, Self>, Error>
where
Self: Sized;
}
/// Read only cursor over DupSort table.

View File

@ -3,7 +3,9 @@ use std::collections::BTreeMap;
use crate::{
common::{PairResult, ValueOnlyResult},
cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW, DupWalker, Walker},
cursor::{
DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW, DupWalker, ReverseWalker, Walker,
},
database::{Database, DatabaseGAT},
table::{DupSort, Table},
transaction::{DbTx, DbTxGAT, DbTxMut, DbTxMutGAT},
@ -132,6 +134,16 @@ impl<'tx, T: Table> DbCursorRO<'tx, T> for CursorMock {
{
todo!()
}
fn walk_back<'cursor>(
&'cursor mut self,
_start_key: Option<T::Key>,
) -> Result<ReverseWalker<'cursor, 'tx, T, Self>, Error>
where
Self: Sized,
{
todo!()
}
}
impl<'tx, T: DupSort> DbDupCursorRO<'tx, T> for CursorMock {

View File

@ -37,9 +37,9 @@ pub trait Decode: Send + Sync + Sized + Debug {
}
/// Generic trait that enforces the database key to implement [`Encode`] and [`Decode`].
pub trait Key: Encode + Decode + Ord {}
pub trait Key: Encode + Decode + Ord + Clone {}
impl<T> Key for T where T: Encode + Decode + Ord {}
impl<T> Key for T where T: Encode + Decode + Ord + Clone {}
/// Generic trait that enforces the database value to implement [`Compress`] and [`Decompress`].
pub trait Value: Compress + Decompress + Serialize {}

View File

@ -3,7 +3,9 @@
use std::{borrow::Cow, marker::PhantomData};
use crate::{
cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW, DupWalker, Walker},
cursor::{
DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW, DupWalker, ReverseWalker, Walker,
},
table::{Compress, DupSort, Encode, Table},
tables::utils::*,
Error,
@ -81,6 +83,27 @@ impl<'tx, K: TransactionKind, T: Table> DbCursorRO<'tx, T> for Cursor<'tx, K, T>
Ok(Walker::new(self, start))
}
fn walk_back<'cursor>(
&'cursor mut self,
start_key: Option<T::Key>,
) -> Result<ReverseWalker<'cursor, 'tx, T, Self>, Error>
where
Self: Sized,
{
if let Some(start_key) = start_key {
let start = self
.inner
.set_range(start_key.encode().as_ref())
.map_err(|e| Error::Read(e.into()))?
.map(decoder::<T>);
return Ok(ReverseWalker::new(self, start))
}
let start = self.last().transpose();
Ok(ReverseWalker::new(self, start))
}
}
impl<'tx, K: TransactionKind, T: DupSort> DbDupCursorRO<'tx, T> for Cursor<'tx, K, T> {