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

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

53 lines
1.2 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::{slice_longevity, Cursor, From, State};
use crate::{keyval::Key, Engine};
pub(crate) struct Keys<'a> {
state: State<'a>,
}
impl<'a> Keys<'a> {
pub(crate) fn new(db: &'a Arc<Engine>, cf: &'a Arc<ColumnFamily>, opts: ReadOptions) -> Self {
Self { state: State::new(db, cf, opts) }
}
}
impl<'a> convert::From<State<'a>> for Keys<'a> {
fn from(state: State<'a>) -> Self { Self { state } }
2024-08-08 17:18:30 +00:00
}
impl<'a> Cursor<'a, Key<'a>> for Keys<'a> {
fn state(&self) -> &State<'a> { &self.state }
#[inline]
2024-08-08 17:18:30 +00:00
fn fetch(&self) -> Option<Key<'a>> { self.state.fetch_key().map(slice_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 Keys<'a> {
type Item = Result<Key<'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 Keys<'_> {
#[inline]
2024-08-08 17:18:30 +00:00
fn is_terminated(&self) -> bool { !self.state.init && !self.state.valid() }
}