offload remaining db iterator initial seeks on cache miss

consume task budget on cache hit

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-12-18 22:56:53 +00:00 committed by strawberry
parent 14341bb906
commit 98e6c81e49
16 changed files with 199 additions and 131 deletions

View file

@ -7,7 +7,9 @@ use serde::Serialize;
/// Count the total number of entries in the map. /// Count the total number of entries in the map.
#[implement(super::Map)] #[implement(super::Map)]
#[inline] #[inline]
pub fn count(&self) -> impl Future<Output = usize> + Send + '_ { self.raw_keys().count() } pub fn count(self: &Arc<Self>) -> impl Future<Output = usize> + Send + '_ {
self.raw_keys().count()
}
/// Count the number of entries in the map starting from a lower-bound. /// Count the number of entries in the map starting from a lower-bound.
/// ///

View file

@ -1,11 +1,16 @@
use conduwuit::{implement, Result}; use std::sync::Arc;
use futures::{Stream, StreamExt};
use serde::Deserialize;
use crate::{keyval, keyval::Key, stream, stream::Cursor}; use conduwuit::{implement, Result};
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt};
use rocksdb::Direction;
use serde::Deserialize;
use tokio::task;
use super::stream::is_cached;
use crate::{keyval, keyval::Key, stream};
#[implement(super::Map)] #[implement(super::Map)]
pub fn keys<'a, K>(&'a self) -> impl Stream<Item = Result<Key<'_, K>>> + Send pub fn keys<'a, K>(self: &'a Arc<Self>) -> impl Stream<Item = Result<Key<'_, K>>> + Send
where where
K: Deserialize<'a> + Send, K: Deserialize<'a> + Send,
{ {
@ -14,7 +19,33 @@ where
#[implement(super::Map)] #[implement(super::Map)]
#[tracing::instrument(skip(self), fields(%self), level = "trace")] #[tracing::instrument(skip(self), fields(%self), level = "trace")]
pub fn raw_keys(&self) -> impl Stream<Item = Result<Key<'_>>> + Send { pub fn raw_keys(self: &Arc<Self>) -> impl Stream<Item = Result<Key<'_>>> + Send {
use crate::pool::Seek;
let opts = super::iter_options_default(); let opts = super::iter_options_default();
stream::Keys::new(&self.db, &self.cf, opts).init(None) let state = stream::State::new(&self.db, &self.cf, opts);
if is_cached(self) {
let state = state.init_fwd(None);
return task::consume_budget()
.map(move |()| stream::Keys::<'_>::from(state))
.into_stream()
.flatten()
.boxed();
}
let seek = Seek {
map: self.clone(),
dir: Direction::Forward,
state: crate::pool::into_send_seek(state),
key: None,
res: None,
};
self.db
.pool
.execute_iter(seek)
.ok_into::<stream::Keys<'_>>()
.into_stream()
.try_flatten()
.boxed()
} }

View file

@ -1,11 +1,7 @@
use std::{convert::AsRef, fmt::Debug, sync::Arc}; use std::{convert::AsRef, fmt::Debug, sync::Arc};
use conduwuit::{implement, Result}; use conduwuit::{implement, Result};
use futures::{ use futures::{future, Stream, StreamExt, TryStreamExt};
future,
stream::{Stream, StreamExt},
TryStreamExt,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::keyval::{result_deserialize_key, serialize_key, Key}; use crate::keyval::{result_deserialize_key, serialize_key, Key};

View file

@ -1,11 +1,16 @@
use conduwuit::{implement, Result}; use std::sync::Arc;
use futures::{Stream, StreamExt};
use serde::Deserialize;
use crate::{keyval, keyval::Key, stream, stream::Cursor}; use conduwuit::{implement, Result};
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt};
use rocksdb::Direction;
use serde::Deserialize;
use tokio::task;
use super::rev_stream::is_cached;
use crate::{keyval, keyval::Key, stream};
#[implement(super::Map)] #[implement(super::Map)]
pub fn rev_keys<'a, K>(&'a self) -> impl Stream<Item = Result<Key<'_, K>>> + Send pub fn rev_keys<'a, K>(self: &'a Arc<Self>) -> impl Stream<Item = Result<Key<'_, K>>> + Send
where where
K: Deserialize<'a> + Send, K: Deserialize<'a> + Send,
{ {
@ -14,7 +19,33 @@ where
#[implement(super::Map)] #[implement(super::Map)]
#[tracing::instrument(skip(self), fields(%self), level = "trace")] #[tracing::instrument(skip(self), fields(%self), level = "trace")]
pub fn rev_raw_keys(&self) -> impl Stream<Item = Result<Key<'_>>> + Send { pub fn rev_raw_keys(self: &Arc<Self>) -> impl Stream<Item = Result<Key<'_>>> + Send {
use crate::pool::Seek;
let opts = super::iter_options_default(); let opts = super::iter_options_default();
stream::KeysRev::new(&self.db, &self.cf, opts).init(None) let state = stream::State::new(&self.db, &self.cf, opts);
if is_cached(self) {
let state = state.init_rev(None);
return task::consume_budget()
.map(move |()| stream::KeysRev::<'_>::from(state))
.into_stream()
.flatten()
.boxed();
}
let seek = Seek {
map: self.clone(),
dir: Direction::Reverse,
state: crate::pool::into_send_seek(state),
key: None,
res: None,
};
self.db
.pool
.execute_iter(seek)
.ok_into::<stream::KeysRev<'_>>()
.into_stream()
.try_flatten()
.boxed()
} }

View file

@ -1,11 +1,7 @@
use std::{convert::AsRef, fmt::Debug, sync::Arc}; use std::{convert::AsRef, fmt::Debug, sync::Arc};
use conduwuit::{implement, Result}; use conduwuit::{implement, Result};
use futures::{ use futures::{future, Stream, StreamExt, TryStreamExt};
future,
stream::{Stream, StreamExt},
TryStreamExt,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::keyval::{result_deserialize_key, serialize_key, Key}; use crate::keyval::{result_deserialize_key, serialize_key, Key};

View file

@ -1,14 +1,20 @@
use conduwuit::{implement, Result}; use std::sync::Arc;
use futures::stream::{Stream, StreamExt};
use serde::Deserialize;
use crate::{keyval, keyval::KeyVal, stream, stream::Cursor}; use conduwuit::{implement, Result};
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt};
use rocksdb::Direction;
use serde::Deserialize;
use tokio::task;
use crate::{keyval, keyval::KeyVal, stream};
/// Iterate key-value entries in the map from the end. /// Iterate key-value entries in the map from the end.
/// ///
/// - Result is deserialized /// - Result is deserialized
#[implement(super::Map)] #[implement(super::Map)]
pub fn rev_stream<'a, K, V>(&'a self) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send pub fn rev_stream<'a, K, V>(
self: &'a Arc<Self>,
) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send
where where
K: Deserialize<'a> + Send, K: Deserialize<'a> + Send,
V: Deserialize<'a> + Send, V: Deserialize<'a> + Send,
@ -22,9 +28,35 @@ where
/// - Result is raw /// - Result is raw
#[implement(super::Map)] #[implement(super::Map)]
#[tracing::instrument(skip(self), fields(%self), level = "trace")] #[tracing::instrument(skip(self), fields(%self), level = "trace")]
pub fn rev_raw_stream(&self) -> impl Stream<Item = Result<KeyVal<'_>>> + Send { pub fn rev_raw_stream(self: &Arc<Self>) -> impl Stream<Item = Result<KeyVal<'_>>> + Send {
let opts = super::iter_options_default(); use crate::pool::Seek;
stream::ItemsRev::new(&self.db, &self.cf, opts).init(None)
let opts = super::read_options_default();
let state = stream::State::new(&self.db, &self.cf, opts);
if is_cached(self) {
let state = state.init_rev(None);
return task::consume_budget()
.map(move |()| stream::ItemsRev::<'_>::from(state))
.into_stream()
.flatten()
.boxed();
};
let seek = Seek {
map: self.clone(),
dir: Direction::Reverse,
state: crate::pool::into_send_seek(state),
key: None,
res: None,
};
self.db
.pool
.execute_iter(seek)
.ok_into::<stream::ItemsRev<'_>>()
.into_stream()
.try_flatten()
.boxed()
} }
#[tracing::instrument( #[tracing::instrument(
@ -33,13 +65,9 @@ pub fn rev_raw_stream(&self) -> impl Stream<Item = Result<KeyVal<'_>>> + Send {
skip_all, skip_all,
fields(%map), fields(%map),
)] )]
pub(super) fn _is_cached<P>(map: &super::Map) -> bool pub(super) fn is_cached(map: &super::Map) -> bool {
where
P: AsRef<[u8]> + ?Sized,
{
let opts = super::cache_read_options_default(); let opts = super::cache_read_options_default();
let mut state = stream::State::new(&map.db, &map.cf, opts); let state = stream::State::new(&map.db, &map.cf, opts).init_rev(None);
state.seek_rev();
!state.is_incomplete() !state.is_incomplete()
} }

View file

@ -1,12 +1,10 @@
use std::{convert::AsRef, fmt::Debug, sync::Arc}; use std::{convert::AsRef, fmt::Debug, sync::Arc};
use conduwuit::{implement, Result}; use conduwuit::{implement, Result};
use futures::{ use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt};
stream::{Stream, StreamExt},
FutureExt, TryFutureExt, TryStreamExt,
};
use rocksdb::Direction; use rocksdb::Direction;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::task;
use crate::{ use crate::{
keyval::{result_deserialize, serialize_key, KeyVal}, keyval::{result_deserialize, serialize_key, KeyVal},
@ -85,7 +83,12 @@ where
let opts = super::iter_options_default(); let opts = super::iter_options_default();
let state = stream::State::new(&self.db, &self.cf, opts); let state = stream::State::new(&self.db, &self.cf, opts);
if is_cached(self, from) { if is_cached(self, from) {
return stream::ItemsRev::<'_>::from(state.init_rev(from.as_ref().into())).boxed(); let state = state.init_rev(from.as_ref().into());
return task::consume_budget()
.map(move |()| stream::ItemsRev::<'_>::from(state))
.into_stream()
.flatten()
.boxed();
}; };
let seek = Seek { let seek = Seek {

View file

@ -1,11 +1,7 @@
use std::{convert::AsRef, fmt::Debug, sync::Arc}; use std::{convert::AsRef, fmt::Debug, sync::Arc};
use conduwuit::{implement, Result}; use conduwuit::{implement, Result};
use futures::{ use futures::{future, Stream, StreamExt, TryStreamExt};
future,
stream::{Stream, StreamExt},
TryStreamExt,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::keyval::{result_deserialize, serialize_key, KeyVal}; use crate::keyval::{result_deserialize, serialize_key, KeyVal};

View file

@ -1,14 +1,20 @@
use conduwuit::{implement, Result}; use std::sync::Arc;
use futures::stream::{Stream, StreamExt};
use serde::Deserialize;
use crate::{keyval, keyval::KeyVal, stream, stream::Cursor}; use conduwuit::{implement, Result};
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt};
use rocksdb::Direction;
use serde::Deserialize;
use tokio::task;
use crate::{keyval, keyval::KeyVal, stream};
/// Iterate key-value entries in the map from the beginning. /// Iterate key-value entries in the map from the beginning.
/// ///
/// - Result is deserialized /// - Result is deserialized
#[implement(super::Map)] #[implement(super::Map)]
pub fn stream<'a, K, V>(&'a self) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send pub fn stream<'a, K, V>(
self: &'a Arc<Self>,
) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send
where where
K: Deserialize<'a> + Send, K: Deserialize<'a> + Send,
V: Deserialize<'a> + Send, V: Deserialize<'a> + Send,
@ -21,9 +27,35 @@ where
/// - Result is raw /// - Result is raw
#[implement(super::Map)] #[implement(super::Map)]
#[tracing::instrument(skip(self), fields(%self), level = "trace")] #[tracing::instrument(skip(self), fields(%self), level = "trace")]
pub fn raw_stream(&self) -> impl Stream<Item = Result<KeyVal<'_>>> + Send { pub fn raw_stream(self: &Arc<Self>) -> impl Stream<Item = Result<KeyVal<'_>>> + Send {
let opts = super::iter_options_default(); use crate::pool::Seek;
stream::Items::new(&self.db, &self.cf, opts).init(None)
let opts = super::read_options_default();
let state = stream::State::new(&self.db, &self.cf, opts);
if is_cached(self) {
let state = state.init_fwd(None);
return task::consume_budget()
.map(move |()| stream::Items::<'_>::from(state))
.into_stream()
.flatten()
.boxed();
};
let seek = Seek {
map: self.clone(),
dir: Direction::Forward,
state: crate::pool::into_send_seek(state),
key: None,
res: None,
};
self.db
.pool
.execute_iter(seek)
.ok_into::<stream::Items<'_>>()
.into_stream()
.try_flatten()
.boxed()
} }
#[tracing::instrument( #[tracing::instrument(
@ -32,13 +64,9 @@ pub fn raw_stream(&self) -> impl Stream<Item = Result<KeyVal<'_>>> + Send {
skip_all, skip_all,
fields(%map), fields(%map),
)] )]
pub(super) fn _is_cached<P>(map: &super::Map) -> bool pub(super) fn is_cached(map: &super::Map) -> bool {
where
P: AsRef<[u8]> + ?Sized,
{
let opts = super::cache_read_options_default(); let opts = super::cache_read_options_default();
let mut state = stream::State::new(&map.db, &map.cf, opts); let state = stream::State::new(&map.db, &map.cf, opts).init_fwd(None);
state.seek_fwd();
!state.is_incomplete() !state.is_incomplete()
} }

View file

@ -1,12 +1,10 @@
use std::{convert::AsRef, fmt::Debug, sync::Arc}; use std::{convert::AsRef, fmt::Debug, sync::Arc};
use conduwuit::{implement, Result}; use conduwuit::{implement, Result};
use futures::{ use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt};
stream::{Stream, StreamExt},
FutureExt, TryFutureExt, TryStreamExt,
};
use rocksdb::Direction; use rocksdb::Direction;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::task;
use crate::{ use crate::{
keyval::{result_deserialize, serialize_key, KeyVal}, keyval::{result_deserialize, serialize_key, KeyVal},
@ -82,7 +80,12 @@ where
let opts = super::read_options_default(); let opts = super::read_options_default();
let state = stream::State::new(&self.db, &self.cf, opts); let state = stream::State::new(&self.db, &self.cf, opts);
if is_cached(self, from) { if is_cached(self, from) {
return stream::Items::<'_>::from(state.init_fwd(from.as_ref().into())).boxed(); let state = state.init_fwd(from.as_ref().into());
return task::consume_budget()
.map(move |()| stream::Items::<'_>::from(state))
.into_stream()
.flatten()
.boxed();
}; };
let seek = Seek { let seek = Seek {

View file

@ -1,11 +1,7 @@
use std::{convert::AsRef, fmt::Debug, sync::Arc}; use std::{convert::AsRef, fmt::Debug, sync::Arc};
use conduwuit::{implement, Result}; use conduwuit::{implement, Result};
use futures::{ use futures::{future, Stream, StreamExt, TryStreamExt};
future,
stream::{Stream, StreamExt},
TryStreamExt,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::keyval::{result_deserialize, serialize_key, KeyVal}; use crate::keyval::{result_deserialize, serialize_key, KeyVal};

View file

@ -29,8 +29,6 @@ pub(crate) trait Cursor<'a, T> {
fn seek(&mut self); fn seek(&mut self);
fn init(self, from: From<'a>) -> Self;
fn get(&self) -> Option<Result<T>> { fn get(&self) -> Option<Result<T>> {
self.fetch() self.fetch()
.map(Ok) .map(Ok)

View file

@ -1,4 +1,4 @@
use std::{convert, pin::Pin, sync::Arc}; use std::pin::Pin;
use conduwuit::Result; use conduwuit::Result;
use futures::{ use futures::{
@ -6,22 +6,15 @@ use futures::{
task::{Context, Poll}, task::{Context, Poll},
Stream, Stream,
}; };
use rocksdb::{ColumnFamily, ReadOptions};
use super::{keyval_longevity, Cursor, From, State}; use super::{keyval_longevity, Cursor, State};
use crate::{keyval::KeyVal, Engine}; use crate::keyval::KeyVal;
pub(crate) struct Items<'a> { pub(crate) struct Items<'a> {
state: State<'a>, state: State<'a>,
} }
impl<'a> Items<'a> { impl<'a> From<State<'a>> for Items<'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 Items<'a> {
fn from(state: State<'a>) -> Self { Self { state } } fn from(state: State<'a>) -> Self { Self { state } }
} }
@ -32,9 +25,6 @@ impl<'a> Cursor<'a, KeyVal<'a>> for Items<'a> {
#[inline] #[inline]
fn seek(&mut self) { self.state.seek_fwd(); } fn seek(&mut self) { self.state.seek_fwd(); }
#[inline]
fn init(self, from: From<'a>) -> Self { Self { state: self.state.init_fwd(from) } }
} }
impl<'a> Stream for Items<'a> { impl<'a> Stream for Items<'a> {

View file

@ -1,4 +1,4 @@
use std::{convert, pin::Pin, sync::Arc}; use std::pin::Pin;
use conduwuit::Result; use conduwuit::Result;
use futures::{ use futures::{
@ -6,22 +6,15 @@ use futures::{
task::{Context, Poll}, task::{Context, Poll},
Stream, Stream,
}; };
use rocksdb::{ColumnFamily, ReadOptions};
use super::{keyval_longevity, Cursor, From, State}; use super::{keyval_longevity, Cursor, State};
use crate::{keyval::KeyVal, Engine}; use crate::keyval::KeyVal;
pub(crate) struct ItemsRev<'a> { pub(crate) struct ItemsRev<'a> {
state: State<'a>, state: State<'a>,
} }
impl<'a> ItemsRev<'a> { impl<'a> From<State<'a>> for ItemsRev<'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 ItemsRev<'a> {
fn from(state: State<'a>) -> Self { Self { state } } fn from(state: State<'a>) -> Self { Self { state } }
} }
@ -32,9 +25,6 @@ impl<'a> Cursor<'a, KeyVal<'a>> for ItemsRev<'a> {
#[inline] #[inline]
fn seek(&mut self) { self.state.seek_rev(); } 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> { impl<'a> Stream for ItemsRev<'a> {

View file

@ -1,4 +1,4 @@
use std::{convert, pin::Pin, sync::Arc}; use std::pin::Pin;
use conduwuit::Result; use conduwuit::Result;
use futures::{ use futures::{
@ -6,22 +6,15 @@ use futures::{
task::{Context, Poll}, task::{Context, Poll},
Stream, Stream,
}; };
use rocksdb::{ColumnFamily, ReadOptions};
use super::{slice_longevity, Cursor, From, State}; use super::{slice_longevity, Cursor, State};
use crate::{keyval::Key, Engine}; use crate::keyval::Key;
pub(crate) struct Keys<'a> { pub(crate) struct Keys<'a> {
state: State<'a>, state: State<'a>,
} }
impl<'a> Keys<'a> { impl<'a> From<State<'a>> for 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 } } fn from(state: State<'a>) -> Self { Self { state } }
} }
@ -33,9 +26,6 @@ impl<'a> Cursor<'a, Key<'a>> for Keys<'a> {
#[inline] #[inline]
fn seek(&mut self) { self.state.seek_fwd(); } fn seek(&mut self) { self.state.seek_fwd(); }
#[inline]
fn init(self, from: From<'a>) -> Self { Self { state: self.state.init_fwd(from) } }
} }
impl<'a> Stream for Keys<'a> { impl<'a> Stream for Keys<'a> {

View file

@ -1,4 +1,4 @@
use std::{convert, pin::Pin, sync::Arc}; use std::pin::Pin;
use conduwuit::Result; use conduwuit::Result;
use futures::{ use futures::{
@ -6,22 +6,15 @@ use futures::{
task::{Context, Poll}, task::{Context, Poll},
Stream, Stream,
}; };
use rocksdb::{ColumnFamily, ReadOptions};
use super::{slice_longevity, Cursor, From, State}; use super::{slice_longevity, Cursor, State};
use crate::{keyval::Key, Engine}; use crate::keyval::Key;
pub(crate) struct KeysRev<'a> { pub(crate) struct KeysRev<'a> {
state: State<'a>, state: State<'a>,
} }
impl<'a> KeysRev<'a> { impl<'a> From<State<'a>> for KeysRev<'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 KeysRev<'a> {
fn from(state: State<'a>) -> Self { Self { state } } fn from(state: State<'a>) -> Self { Self { state } }
} }
@ -33,9 +26,6 @@ impl<'a> Cursor<'a, Key<'a>> for KeysRev<'a> {
#[inline] #[inline]
fn seek(&mut self) { self.state.seek_rev(); } 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 KeysRev<'a> { impl<'a> Stream for KeysRev<'a> {