Skip to main content

tuwunel_database/map/
rev_keys_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::{Key, result_deserialize_key, serialize_key},
11	stream,
12};
13
14/// Streams deserialized keys backward from a serialized upper bound.
15///
16/// The scan begins at the greatest key not greater than the encoded bound.
17/// Any borrowed key must not be retained across another poll.
18///
19/// # Panics
20///
21/// Panics if the upper bound cannot be serialized.
22#[implement(super::Map)]
23pub fn rev_keys_from<'a, K, P>(
24	self: &'a Arc<Self>,
25	from: &P,
26) -> impl Stream<Item = Result<Key<'_, K>>> + Send + use<'a, K, P>
27where
28	P: Serialize + ?Sized + Debug,
29	K: Deserialize<'a> + Send,
30{
31	self.rev_keys_from_raw(from)
32		.map(result_deserialize_key::<K>)
33}
34
35/// Streams raw keys backward from a serialized upper bound.
36///
37/// The scan begins at the greatest key not greater than the encoded bound.
38/// Yielded keys borrow cursor storage and must not be retained across another
39/// poll.
40///
41/// # Panics
42///
43/// Panics if the upper bound cannot be serialized.
44#[implement(super::Map)]
45#[tracing::instrument(skip(self), level = "trace")]
46pub fn rev_keys_from_raw<P>(
47	self: &Arc<Self>,
48	from: &P,
49) -> impl Stream<Item = Result<Key<'_>>> + Send + use<'_, P>
50where
51	P: Serialize + ?Sized + Debug,
52{
53	let key = serialize_key(from).expect("failed to serialize query key");
54	self.rev_raw_keys_from(&key)
55}
56
57/// Streams deserialized keys backward from a raw upper bound.
58///
59/// The supplied bytes are used directly as the reverse seek position. Any
60/// borrowed key must not be retained across another poll.
61#[implement(super::Map)]
62pub fn rev_keys_raw_from<'a, K, P>(
63	self: &'a Arc<Self>,
64	from: &P,
65) -> impl Stream<Item = Result<Key<'_, K>>> + Send + use<'a, K, P>
66where
67	P: AsRef<[u8]> + ?Sized + Debug + Sync,
68	K: Deserialize<'a> + Send,
69{
70	self.rev_raw_keys_from(from)
71		.map(result_deserialize_key::<K>)
72}
73
74/// Streams raw keys backward from a raw upper bound.
75///
76/// The supplied bytes are used directly as the reverse seek position. Yielded
77/// keys borrow cursor storage and must not be retained across another poll.
78#[implement(super::Map)]
79#[tracing::instrument(skip(self, from), fields(%self), level = "trace")]
80pub fn rev_raw_keys_from<P>(
81	self: &Arc<Self>,
82	from: &P,
83) -> impl Stream<Item = Result<Key<'_>>> + Send + use<'_, P>
84where
85	P: AsRef<[u8]> + ?Sized + Debug,
86{
87	seek_stream::<stream::KeysRev<'_>, _>(self, Direction::Reverse, Some(from.as_ref()))
88}