Skip to main content

tuwunel_database/map/
rev_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 from a reverse seek at a serialized prefix.
10///
11/// The encoded prefix is both the seek position and the predicate. Under
12/// bytewise ordering, longer keys with the prefix sort above this starting
13/// point, so the scan normally reaches only an exact-key match. Any borrowed
14/// key or value must not be retained across another poll.
15///
16/// # Panics
17///
18/// Panics if the prefix cannot be serialized.
19#[implement(super::Map)]
20pub fn rev_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.rev_stream_prefix_raw(prefix)
30		.map(result_deserialize::<K, V>)
31}
32
33/// Streams raw entries from a reverse seek at a serialized prefix.
34///
35/// The encoded prefix is both the seek position and the predicate. Under
36/// bytewise ordering, longer keys with the prefix sort above this starting
37/// point, so the scan normally reaches only an exact-key match. Yielded keys
38/// and values borrow cursor storage and must not be retained across another
39/// poll.
40///
41/// # Panics
42///
43/// Panics if the prefix cannot be serialized.
44#[implement(super::Map)]
45#[tracing::instrument(skip(self), level = "trace")]
46pub fn rev_stream_prefix_raw<P>(
47	self: &Arc<Self>,
48	prefix: &P,
49) -> impl Stream<Item = Result<KeyVal<'_>>> + Send + use<'_, P>
50where
51	P: Serialize + ?Sized + Debug,
52{
53	let key = serialize_key(prefix).expect("failed to serialize query key");
54	self.rev_raw_stream_from(&key)
55		.try_take_while(move |(k, _): &KeyVal<'_>| future::ok(k.starts_with(&key)))
56}
57
58/// Streams deserialized entries from a reverse seek at a raw prefix.
59///
60/// The supplied bytes are both the seek position and the predicate. Under
61/// bytewise ordering, longer keys with the prefix sort above this starting
62/// point, so the scan normally reaches only an exact-key match. Any borrowed
63/// key or value must not be retained across another poll.
64#[implement(super::Map)]
65pub fn rev_stream_raw_prefix<'a, K, V, P>(
66	self: &'a Arc<Self>,
67	prefix: &'a P,
68) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send + 'a
69where
70	P: AsRef<[u8]> + ?Sized + Debug + Sync + 'a,
71	K: Deserialize<'a> + Send + 'a,
72	V: Deserialize<'a> + Send + 'a,
73{
74	self.rev_raw_stream_prefix(prefix)
75		.map(result_deserialize::<K, V>)
76}
77
78/// Streams raw entries from a reverse seek at a raw prefix.
79///
80/// The supplied bytes are both the seek position and the predicate. Under
81/// bytewise ordering, longer keys with the prefix sort above this starting
82/// point, so the scan normally reaches only an exact-key match. Yielded keys
83/// and values borrow cursor storage and must not be retained across another
84/// poll.
85#[implement(super::Map)]
86pub fn rev_raw_stream_prefix<'a, P>(
87	self: &'a Arc<Self>,
88	prefix: &'a P,
89) -> impl Stream<Item = Result<KeyVal<'_>>> + Send + 'a
90where
91	P: AsRef<[u8]> + ?Sized + Debug + Sync + 'a,
92{
93	self.rev_raw_stream_from(prefix)
94		.try_take_while(|(k, _): &KeyVal<'_>| future::ok(k.starts_with(prefix.as_ref())))
95}