continuwuity/src/database/stream/keys.rs
Jason Volk 98e6c81e49 offload remaining db iterator initial seeks on cache miss
consume task budget on cache hit

Signed-off-by: Jason Volk <jason@zemos.net>
2025-01-01 23:28:01 -05:00

42 lines
903 B
Rust

use std::pin::Pin;
use conduwuit::Result;
use futures::{
stream::FusedStream,
task::{Context, Poll},
Stream,
};
use super::{slice_longevity, Cursor, State};
use crate::keyval::Key;
pub(crate) struct Keys<'a> {
state: State<'a>,
}
impl<'a> From<State<'a>> for Keys<'a> {
fn from(state: State<'a>) -> Self { Self { state } }
}
impl<'a> Cursor<'a, Key<'a>> for Keys<'a> {
fn state(&self) -> &State<'a> { &self.state }
#[inline]
fn fetch(&self) -> Option<Key<'a>> { self.state.fetch_key().map(slice_longevity) }
#[inline]
fn seek(&mut self) { self.state.seek_fwd(); }
}
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]
fn is_terminated(&self) -> bool { !self.state.init && !self.state.valid() }
}