Skip to main content

tuwunel_database/map/
qry.rs

1use std::{fmt::Debug, io::Write, sync::Arc};
2
3use serde::Serialize;
4use tuwunel_core::{Result, arrayvec::ArrayVec, implement};
5
6use crate::{Handle, keyval::KeyBuf, ser};
7
8/// Fetches a serialized key asynchronously using an owned buffer.
9///
10/// The key is encoded with the database serializer before raw lookup. The
11/// returned handle keeps its RocksDB value storage pinned for the handle's
12/// lifetime.
13///
14/// # Panics
15///
16/// Panics if the key cannot be serialized.
17#[implement(super::Map)]
18#[inline]
19pub fn qry<K>(
20	self: &Arc<Self>,
21	key: &K,
22) -> impl Future<Output = Result<Handle<'_>>> + Send + use<'_, K>
23where
24	K: Serialize + ?Sized + Debug,
25{
26	let mut buf = KeyBuf::new();
27	self.bqry(key, &mut buf)
28}
29
30/// Fetches a serialized key asynchronously using fixed-capacity storage.
31///
32/// `MAX` bounds the complete encoded key without a heap fallback. The returned
33/// handle keeps its RocksDB value storage pinned for the handle's lifetime.
34///
35/// # Panics
36///
37/// Panics if the encoded key exceeds `MAX` or serialization otherwise fails.
38#[implement(super::Map)]
39#[inline]
40pub fn aqry<const MAX: usize, K>(
41	self: &Arc<Self>,
42	key: &K,
43) -> impl Future<Output = Result<Handle<'_>>> + Send + use<'_, MAX, K>
44where
45	K: Serialize + ?Sized + Debug,
46{
47	let mut buf = ArrayVec::<u8, MAX>::new();
48	self.bqry(key, &mut buf)
49}
50
51/// Fetches a serialized key asynchronously using a caller-supplied buffer.
52///
53/// Serialization appends to the supplied buffer, and lookup uses its full
54/// resulting contents. The returned handle keeps its RocksDB value storage
55/// pinned for the handle's lifetime.
56///
57/// # Panics
58///
59/// Panics if the key cannot be serialized.
60#[implement(super::Map)]
61#[tracing::instrument(skip(self, buf), level = "trace")]
62pub fn bqry<K, B>(
63	self: &Arc<Self>,
64	key: &K,
65	buf: &mut B,
66) -> impl Future<Output = Result<Handle<'_>>> + Send + use<'_, K, B>
67where
68	K: Serialize + ?Sized + Debug,
69	B: Write + AsRef<[u8]>,
70{
71	let key = ser::serialize(buf, key).expect("failed to serialize query key");
72	self.get(key)
73}