Skip to main content

tuwunel_database/map/
stream_from.rs

1use std::{fmt::Debug, sync::Arc};
2
3use futures::{Stream, StreamExt};
4use rocksdb::Direction;
5use serde::{Deserialize, Serialize};
6use tuwunel_core::{Result, implement};
7
8use super::seek::seek_stream;
9use crate::{
10	keyval::{KeyVal, result_deserialize, serialize_key},
11	stream,
12};
13
14/// Streams deserialized entries forward from a serialized lower bound.
15///
16/// The scan begins at the first key not less than the encoded bound. Any
17/// borrowed key or value must not be retained across another poll of the
18/// stream.
19///
20/// # Panics
21///
22/// Panics if the lower bound cannot be serialized.
23#[implement(super::Map)]
24pub fn stream_from<'a, K, V, P>(
25	self: &'a Arc<Self>,
26	from: &P,
27) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send + use<'a, K, V, P>
28where
29	P: Serialize + ?Sized + Debug,
30	K: Deserialize<'a> + Send,
31	V: Deserialize<'a> + Send,
32{
33	self.stream_from_raw(from)
34		.map(result_deserialize::<K, V>)
35}
36
37/// Streams raw entries forward from a serialized lower bound.
38///
39/// The scan begins at the first key not less than the encoded bound. Yielded
40/// keys and values borrow cursor storage and must not be retained across
41/// another poll.
42///
43/// # Panics
44///
45/// Panics if the lower bound cannot be serialized.
46#[implement(super::Map)]
47#[tracing::instrument(skip(self), level = "trace")]
48pub fn stream_from_raw<P>(
49	self: &Arc<Self>,
50	from: &P,
51) -> impl Stream<Item = Result<KeyVal<'_>>> + Send + use<'_, P>
52where
53	P: Serialize + ?Sized + Debug,
54{
55	let key = serialize_key(from).expect("failed to serialize query key");
56	self.raw_stream_from(&key)
57}
58
59/// Streams deserialized entries forward from a raw lower bound.
60///
61/// The supplied bytes are used directly as the seek position. Any borrowed key
62/// or value must not be retained across another poll of the stream.
63#[implement(super::Map)]
64pub fn stream_raw_from<'a, K, V, P>(
65	self: &'a Arc<Self>,
66	from: &P,
67) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send + use<'a, K, V, P>
68where
69	P: AsRef<[u8]> + ?Sized + Debug + Sync,
70	K: Deserialize<'a> + Send,
71	V: Deserialize<'a> + Send,
72{
73	self.raw_stream_from(from)
74		.map(result_deserialize::<K, V>)
75}
76
77/// Streams raw entries forward from a raw lower bound.
78///
79/// The supplied bytes are used directly as the seek position. Yielded keys and
80/// values borrow cursor storage and must not be retained across another poll.
81#[implement(super::Map)]
82#[tracing::instrument(skip(self, from), fields(%self), level = "trace")]
83pub fn raw_stream_from<P>(
84	self: &Arc<Self>,
85	from: &P,
86) -> impl Stream<Item = Result<KeyVal<'_>>> + Send + use<'_, P>
87where
88	P: AsRef<[u8]> + ?Sized + Debug,
89{
90	seek_stream::<stream::Items<'_>, _>(self, Direction::Forward, Some(from.as_ref()))
91}