From ca34f23432c243e16a1cdc41a253037bf82355d4 Mon Sep 17 00:00:00 2001 From: NYBACHOK Date: Thu, 10 Oct 2024 13:38:56 +0300 Subject: [PATCH] revert iter for kv_store --- kv_store/src/range.rs | 41 ++++++++++++++++++++++++++++++++----- trees/src/iavl/range/mod.rs | 32 +++++++++++++++-------------- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/kv_store/src/range.rs b/kv_store/src/range.rs index c27a773be..14249a0af 100644 --- a/kv_store/src/range.rs +++ b/kv_store/src/range.rs @@ -1,14 +1,27 @@ -use std::borrow::Cow; +use std::{borrow::Cow, ops::Bound}; use database::Database; -use trees::iavl::Range as TreeRange; +use trees::iavl; use crate::utils::MergedRange; #[derive(Debug, Clone)] pub enum Range<'a, DB> { Merged(MergedRange<'a>), - Tree(TreeRange<'a, DB>), + Tree(iavl::Range<'a, DB>), + MergedRev(std::iter::Rev>), + TreeRev(iavl::RevRange<'a, DB, Vec, (Bound>, Bound>)>), +} + +impl<'a, DB> Range<'a, DB> { + pub fn rev_iter(self) -> Range<'a, DB> { + match self { + Range::Merged(range) => Range::MergedRev(range.rev()), + Range::Tree(range) => Range::TreeRev(range.rev_iter()), + Range::MergedRev(rev) => Range::MergedRev(rev), + Range::TreeRev(rev_range) => Range::TreeRev(rev_range), + } + } } impl<'a, DB: Database> Iterator for Range<'a, DB> { @@ -20,6 +33,10 @@ impl<'a, DB: Database> Iterator for Range<'a, DB> { Range::Tree(range) => range .next() .map(|(first, second)| (Cow::Owned(first), Cow::Owned(second))), + Range::MergedRev(range) => range.next(), + Range::TreeRev(range) => range + .next() + .map(|(first, second)| (Cow::Owned(first), Cow::Owned(second))), } } } @@ -30,8 +47,22 @@ impl<'a, DB> From> for Range<'a, DB> { } } -impl<'a, DB> From> for Range<'a, DB> { - fn from(value: TreeRange<'a, DB>) -> Self { +impl<'a, DB> From> for Range<'a, DB> { + fn from(value: iavl::Range<'a, DB>) -> Self { Self::Tree(value) } } + +impl<'a, DB> From>> for Range<'a, DB> { + fn from(value: std::iter::Rev>) -> Self { + Self::MergedRev(value) + } +} + +impl<'a, DB> From, (Bound>, Bound>)>> + for Range<'a, DB> +{ + fn from(value: iavl::RevRange<'a, DB, Vec, (Bound>, Bound>)>) -> Self { + Self::TreeRev(value) + } +} diff --git a/trees/src/iavl/range/mod.rs b/trees/src/iavl/range/mod.rs index d66338bba..ed1113cd5 100644 --- a/trees/src/iavl/range/mod.rs +++ b/trees/src/iavl/range/mod.rs @@ -17,6 +17,23 @@ pub struct Range<'a, DB> { node_db: &'a NodeDB, } +impl<'a, DB> Range<'a, DB> { + pub fn rev_iter(self) -> RevRange<'a, DB, Vec, (Bound>, Bound>)> { + let Self { + range, + delayed_nodes, + node_db, + } = self; + + RevRange { + range: range, + delayed_nodes: delayed_nodes.into(), + node_db, + _marker: PhantomData, + } + } +} + impl<'a, DB: Database> Range<'a, DB> { pub(crate) fn new>>( range: R, @@ -33,21 +50,6 @@ impl<'a, DB: Database> Range<'a, DB> { } } - pub fn rev_iter(self) -> RevRange<'a, DB, Vec, (Bound>, Bound>)> { - let Self { - range, - delayed_nodes, - node_db, - } = self; - - RevRange { - range: range, - delayed_nodes: delayed_nodes.into(), - node_db, - _marker: PhantomData, - } - } - fn traverse(&mut self) -> Option<(Vec, Vec)> { let node = self.delayed_nodes.pop()?;