Skip to main content

tuwunel_database/map/
stream_prefix.rs

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