Files
continuwuity/src/database/stream/items.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.3 KiB
Rust
Raw Normal View History

use std::{convert, pin::Pin, sync::Arc};
2024-08-08 17:18:30 +00:00
2024-12-14 21:58:01 -05:00
use conduwuit::Result;
2024-08-08 17:18:30 +00:00
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 Items<'a> {
state: State<'a>,
}
impl<'a> Items<'a> {
pub(crate) fn new(db: &'a Arc<Engine>, cf: &'a Arc<ColumnFamily>, opts: ReadOptions) -> Self {
2024-08-08 17:18:30 +00:00
Self {
state: State::new(db, cf, opts),
}
}
}
impl<'a> convert::From<State<'a>> for Items<'a> {
fn from(state: State<'a>) -> Self {
Self {
state,
2024-08-08 17:18:30 +00:00
}
}
}
impl<'a> Cursor<'a, KeyVal<'a>> for Items<'a> {
fn state(&self) -> &State<'a> { &self.state }
fn fetch(&self) -> Option<KeyVal<'a>> { self.state.fetch().map(keyval_longevity) }
#[inline]
2024-08-08 17:18:30 +00:00
fn seek(&mut self) { self.state.seek_fwd(); }
#[inline]
fn init(self, from: From<'a>) -> Self {
Self {
state: self.state.init_fwd(from),
}
}
2024-08-08 17:18:30 +00:00
}
impl<'a> Stream for Items<'a> {
type Item = Result<KeyVal<'a>>;
fn poll_next(mut self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(self.seek_and_get())
}
}
impl FusedStream for Items<'_> {
#[inline]
2024-08-08 17:18:30 +00:00
fn is_terminated(&self) -> bool { !self.state.init && !self.state.valid() }
}