Skip to main content

tuwunel_database/map/
rev_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 backward from a serialized upper bound.
15///
16/// The scan begins at the greatest key not greater 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 upper bound cannot be serialized.
23#[implement(super::Map)]
24pub fn rev_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.rev_stream_from_raw(from)
34		.map(result_deserialize::<K, V>)
35}
36
37/// Streams raw entries backward from a serialized upper bound.
38///
39/// The scan begins at the greatest key not greater than the encoded bound.
40/// Yielded keys and values borrow cursor storage and must not be retained
41/// across another poll.
42///
43/// # Panics
44///
45/// Panics if the upper bound cannot be serialized.
46#[implement(super::Map)]
47#[tracing::instrument(skip(self), level = "trace")]
48pub fn rev_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.rev_raw_stream_from(&key)
57}
58
59/// Streams deserialized entries backward from a raw upper bound.
60///
61/// The supplied bytes are used directly as the reverse seek position. Any
62/// borrowed key or value must not be retained across another poll of the
63/// stream.
64#[implement(super::Map)]
65pub fn rev_stream_raw_from<'a, K, V, P>(
66	self: &'a Arc<Self>,
67	from: &P,
68) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send + use<'a, K, V, P>
69where
70	P: AsRef<[u8]> + ?Sized + Debug + Sync,
71	K: Deserialize<'a> + Send,
72	V: Deserialize<'a> + Send,
73{
74	self.rev_raw_stream_from(from)
75		.map(result_deserialize::<K, V>)
76}
77
78/// Streams raw entries backward from a raw upper bound.
79///
80/// The supplied bytes are used directly as the reverse seek position. Yielded
81/// keys and values borrow cursor storage and must not be retained across
82/// another poll.
83#[implement(super::Map)]
84#[tracing::instrument(skip(self, from), fields(%self), level = "trace")]
85pub fn rev_raw_stream_from<P>(
86	self: &Arc<Self>,
87	from: &P,
88) -> impl Stream<Item = Result<KeyVal<'_>>> + Send + use<'_, P>
89where
90	P: AsRef<[u8]> + ?Sized + Debug,
91{
92	seek_stream::<stream::ItemsRev<'_>, _>(self, Direction::Reverse, Some(from.as_ref()))
93}