Files
continuwuity/src/database/map/stream_from.rs
T

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

100 lines
2.4 KiB
Rust
Raw Normal View History

use std::{convert::AsRef, fmt::Debug, sync::Arc};
2024-08-08 17:18:30 +00:00
2024-12-14 21:58:01 -05:00
use conduwuit::{implement, Result};
use futures::{
stream::{Stream, StreamExt},
FutureExt, TryFutureExt, TryStreamExt,
};
use rocksdb::Direction;
2024-08-08 17:18:30 +00:00
use serde::{Deserialize, Serialize};
2024-11-28 05:54:34 +00:00
use crate::{
keyval::{result_deserialize, serialize_key, KeyVal},
stream,
};
2024-08-08 17:18:30 +00:00
/// Iterate key-value entries in the map starting from lower-bound.
///
/// - Query is serialized
/// - Result is deserialized
#[implement(super::Map)]
pub fn stream_from<'a, K, V, P>(
self: &'a Arc<Self>,
from: &P,
) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send
2024-08-08 17:18:30 +00:00
where
P: Serialize + ?Sized + Debug,
K: Deserialize<'a> + Send,
V: Deserialize<'a> + Send,
{
2024-11-28 05:54:34 +00:00
self.stream_from_raw(from).map(result_deserialize::<K, V>)
2024-08-08 17:18:30 +00:00
}
/// Iterate key-value entries in the map starting from lower-bound.
///
/// - Query is serialized
/// - Result is raw
#[implement(super::Map)]
#[tracing::instrument(skip(self), level = "trace")]
pub fn stream_from_raw<P>(
self: &Arc<Self>,
from: &P,
) -> impl Stream<Item = Result<KeyVal<'_>>> + Send
2024-08-08 17:18:30 +00:00
where
P: Serialize + ?Sized + Debug,
{
2024-11-28 05:54:34 +00:00
let key = serialize_key(from).expect("failed to serialize query key");
2024-08-08 17:18:30 +00:00
self.raw_stream_from(&key)
}
/// Iterate key-value entries in the map starting from lower-bound.
///
/// - Query is raw
/// - Result is deserialized
#[implement(super::Map)]
pub fn stream_raw_from<'a, K, V, P>(
self: &'a Arc<Self>,
from: &P,
) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send
2024-08-08 17:18:30 +00:00
where
P: AsRef<[u8]> + ?Sized + Debug + Sync,
K: Deserialize<'a> + Send,
V: Deserialize<'a> + Send,
{
2024-11-28 05:54:34 +00:00
self.raw_stream_from(from).map(result_deserialize::<K, V>)
2024-08-08 17:18:30 +00:00
}
/// Iterate key-value entries in the map starting from lower-bound.
///
/// - Query is raw
/// - Result is raw
#[implement(super::Map)]
#[tracing::instrument(skip(self, from), fields(%self), level = "trace")]
pub fn raw_stream_from<P>(
self: &Arc<Self>,
from: &P,
) -> impl Stream<Item = Result<KeyVal<'_>>> + Send
2024-08-08 17:18:30 +00:00
where
P: AsRef<[u8]> + ?Sized + Debug,
{
use crate::pool::Seek;
2024-08-08 17:18:30 +00:00
let opts = super::read_options_default();
let state = stream::State::new(&self.db, &self.cf, opts);
let seek = Seek {
map: self.clone(),
dir: Direction::Forward,
key: Some(from.as_ref().into()),
state: crate::pool::into_send_seek(state),
res: None,
};
self.db
.pool
.execute_iter(seek)
.ok_into::<stream::Items<'_>>()
.into_stream()
.try_flatten()
.boxed()
2024-08-08 17:18:30 +00:00
}