Skip to main content

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