Skip to main content

tuwunel_database/map/
qry.rs

1use std::{convert::AsRef, fmt::Debug, io::Write, sync::Arc};
2
3use futures::Future;
4use serde::Serialize;
5use tuwunel_core::{Result, arrayvec::ArrayVec, implement};
6
7use crate::{Handle, keyval::KeyBuf, ser};
8
9/// Fetch a value from the database into cache, returning a reference-handle
10/// asynchronously. The key is serialized into an allocated buffer to perform
11/// the query.
12#[implement(super::Map)]
13#[inline]
14pub fn qry<K>(
15	self: &Arc<Self>,
16	key: &K,
17) -> impl Future<Output = Result<Handle<'_>>> + Send + use<'_, K>
18where
19	K: Serialize + ?Sized + Debug,
20{
21	let mut buf = KeyBuf::new();
22	self.bqry(key, &mut buf)
23}
24
25/// Fetch a value from the database into cache, returning a reference-handle
26/// asynchronously. The key is serialized into a fixed-sized buffer to perform
27/// the query. The maximum size is supplied as const generic parameter.
28#[implement(super::Map)]
29#[inline]
30pub fn aqry<const MAX: usize, K>(
31	self: &Arc<Self>,
32	key: &K,
33) -> impl Future<Output = Result<Handle<'_>>> + Send + use<'_, MAX, K>
34where
35	K: Serialize + ?Sized + Debug,
36{
37	let mut buf = ArrayVec::<u8, MAX>::new();
38	self.bqry(key, &mut buf)
39}
40
41/// Fetch a value from the database into cache, returning a reference-handle
42/// asynchronously. The key is serialized into a user-supplied Writer.
43#[implement(super::Map)]
44#[tracing::instrument(skip(self, buf), level = "trace")]
45pub fn bqry<K, B>(
46	self: &Arc<Self>,
47	key: &K,
48	buf: &mut B,
49) -> impl Future<Output = Result<Handle<'_>>> + Send + use<'_, K, B>
50where
51	K: Serialize + ?Sized + Debug,
52	B: Write + AsRef<[u8]>,
53{
54	let key = ser::serialize(buf, key).expect("failed to serialize query key");
55	self.get(key)
56}