Skip to main content

tuwunel_database/map/
rev_keys_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::{Key, result_deserialize_key, serialize_key};
8
9/// Streams deserialized keys 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 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_keys_prefix<'a, K, P>(
21	self: &'a Arc<Self>,
22	prefix: &P,
23) -> impl Stream<Item = Result<Key<'_, K>>> + Send + use<'a, K, P>
24where
25	P: Serialize + ?Sized + Debug,
26	K: Deserialize<'a> + Send,
27{
28	self.rev_keys_prefix_raw(prefix)
29		.map(result_deserialize_key::<K>)
30}
31
32/// Streams raw keys from a reverse seek at a serialized prefix.
33///
34/// The encoded prefix is both the seek position and the predicate. Under
35/// bytewise ordering, longer keys with the prefix sort above this starting
36/// point, so the scan normally reaches only an exact-key match. Yielded keys
37/// borrow cursor storage and must not be retained 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 rev_keys_prefix_raw<P>(
45	self: &Arc<Self>,
46	prefix: &P,
47) -> impl Stream<Item = Result<Key<'_>>> + Send + use<'_, P>
48where
49	P: Serialize + ?Sized + Debug,
50{
51	let key = serialize_key(prefix).expect("failed to serialize query key");
52	self.rev_raw_keys_from(&key)
53		.try_take_while(move |k: &Key<'_>| future::ok(k.starts_with(&key)))
54}
55
56/// Streams deserialized keys from a reverse seek at a raw prefix.
57///
58/// The supplied bytes are both the seek position and the predicate. Under
59/// bytewise ordering, longer keys with the prefix sort above this starting
60/// point, so the scan normally reaches only an exact-key match. Any borrowed
61/// key must not be retained across another poll.
62#[implement(super::Map)]
63pub fn rev_keys_raw_prefix<'a, K, P>(
64	self: &'a Arc<Self>,
65	prefix: &'a P,
66) -> impl Stream<Item = Result<Key<'_, K>>> + Send + 'a
67where
68	P: AsRef<[u8]> + ?Sized + Debug + Sync + 'a,
69	K: Deserialize<'a> + Send + 'a,
70{
71	self.rev_raw_keys_prefix(prefix)
72		.map(result_deserialize_key::<K>)
73}
74
75/// Streams raw keys from a reverse seek at a raw prefix.
76///
77/// The supplied bytes are both the seek position and the predicate. Under
78/// bytewise ordering, longer keys with the prefix sort above this starting
79/// point, so the scan normally reaches only an exact-key match. Yielded keys
80/// borrow cursor storage and must not be retained across another poll.
81#[implement(super::Map)]
82pub fn rev_raw_keys_prefix<'a, P>(
83	self: &'a Arc<Self>,
84	prefix: &'a P,
85) -> impl Stream<Item = Result<Key<'_>>> + Send + 'a
86where
87	P: AsRef<[u8]> + ?Sized + Debug + Sync + 'a,
88{
89	self.rev_raw_keys_from(prefix)
90		.try_take_while(|k: &Key<'_>| future::ok(k.starts_with(prefix.as_ref())))
91}