Skip to main content

tuwunel_database/map/
stream.rs

1use std::sync::Arc;
2
3use futures::{Stream, StreamExt};
4use rocksdb::Direction;
5use serde::Deserialize;
6use tuwunel_core::{Result, implement};
7
8use super::seek::{scan_stream, seek_stream};
9use crate::{keyval, keyval::KeyVal, stream};
10
11/// Streams deserialized key-value entries in ascending database order.
12///
13/// Each raw pair is decoded with the database deserializer. Any borrowed key or
14/// value must not be retained across another poll of the stream.
15#[implement(super::Map)]
16pub fn stream<'a, K, V>(
17	self: &'a Arc<Self>,
18) -> impl Stream<Item = Result<KeyVal<'_, K, V>>> + Send
19where
20	K: Deserialize<'a> + Send,
21	V: Deserialize<'a> + Send,
22{
23	self.raw_stream()
24		.map(keyval::result_deserialize::<K, V>)
25}
26
27/// Streams raw key-value entries in ascending database order.
28///
29/// The scan begins at the first key in the column family. Yielded keys and
30/// values borrow cursor storage and must not be retained across another poll.
31#[implement(super::Map)]
32#[tracing::instrument(skip(self), fields(%self), level = "trace")]
33pub fn raw_stream(self: &Arc<Self>) -> impl Stream<Item = Result<KeyVal<'_>>> + Send {
34	seek_stream::<stream::Items<'_>, _>(self, Direction::Forward, None)
35}
36
37/// Streams raw key-value entries for a whole-column integrity walk.
38///
39/// Unlike [`Self::raw_stream`], every block read verifies its checksum and none
40/// enters the block cache, so a walk of a large column on a serving database
41/// evicts nothing. Yielded keys and values borrow cursor storage and must not be
42/// retained across another poll.
43#[implement(super::Map)]
44#[tracing::instrument(
45	level = "trace",
46	skip(self),
47	fields(
48		%self,
49	),
50)]
51pub fn raw_scan(self: &Arc<Self>) -> impl Stream<Item = Result<KeyVal<'_>>> + Send {
52	scan_stream::<stream::Items<'_>, _>(self)
53}