use std::{convert, pin::Pin, sync::Arc}; use conduwuit::Result; use futures::{ stream::FusedStream, task::{Context, Poll}, Stream, }; use rocksdb::{ColumnFamily, ReadOptions}; use super::{keyval_longevity, Cursor, From, State}; use crate::{keyval::KeyVal, Engine}; pub(crate) struct ItemsRev<'a> { state: State<'a>, } impl<'a> ItemsRev<'a> { pub(crate) fn new(db: &'a Arc, cf: &'a Arc, opts: ReadOptions) -> Self { Self { state: State::new(db, cf, opts) } } } impl<'a> convert::From> for ItemsRev<'a> { fn from(state: State<'a>) -> Self { Self { state } } } impl<'a> Cursor<'a, KeyVal<'a>> for ItemsRev<'a> { fn state(&self) -> &State<'a> { &self.state } fn fetch(&self) -> Option> { self.state.fetch().map(keyval_longevity) } #[inline] fn seek(&mut self) { self.state.seek_rev(); } #[inline] fn init(self, from: From<'a>) -> Self { Self { state: self.state.init_rev(from) } } } impl<'a> Stream for ItemsRev<'a> { type Item = Result>; fn poll_next(mut self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll> { Poll::Ready(self.seek_and_get()) } } impl FusedStream for ItemsRev<'_> { #[inline] fn is_terminated(&self) -> bool { !self.state.init && !self.state.valid() } }