Skip to main content

tuwunel_database/map/
count.rs

1use std::{fmt::Debug, sync::Arc};
2
3use futures::stream::StreamExt;
4use serde::Serialize;
5use tuwunel_core::implement;
6
7/// Counts items yielded by a complete forward key scan.
8///
9/// Each database entry normally contributes one item. Iterator failures are
10/// counted as yielded items rather than returned to the caller.
11#[implement(super::Map)]
12#[inline]
13pub fn count(self: &Arc<Self>) -> impl Future<Output = usize> + Send + '_ {
14	self.raw_keys().count()
15}
16
17/// Counts forward scan items at or after a serialized lower bound.
18///
19/// The bound is encoded with the database serializer before seeking. Iterator
20/// failures are counted as yielded items rather than returned to the caller.
21///
22/// # Panics
23///
24/// Panics if the lower bound cannot be serialized.
25#[implement(super::Map)]
26#[inline]
27pub fn count_from<'a, P>(
28	self: &'a Arc<Self>,
29	from: &P,
30) -> impl Future<Output = usize> + Send + 'a + use<'a, P>
31where
32	P: Serialize + ?Sized + Debug + 'a,
33{
34	self.keys_from_raw(from).count()
35}
36
37/// Counts forward scan items at or after a raw lower bound.
38///
39/// The supplied bytes are used directly as the seek key. Iterator failures are
40/// counted as yielded items rather than returned to the caller.
41#[implement(super::Map)]
42#[inline]
43pub fn raw_count_from<'a, P>(
44	self: &'a Arc<Self>,
45	from: &'a P,
46) -> impl Future<Output = usize> + Send + 'a
47where
48	P: AsRef<[u8]> + ?Sized + Debug + Sync + 'a,
49{
50	self.raw_keys_from(from).count()
51}
52
53/// Counts forward scan items matching a serialized prefix.
54///
55/// The prefix is encoded with the database serializer before seeking. Iterator
56/// failures are counted as yielded items rather than returned to the caller.
57///
58/// # Panics
59///
60/// Panics if the prefix cannot be serialized.
61#[implement(super::Map)]
62#[inline]
63pub fn count_prefix<'a, P>(
64	self: &'a Arc<Self>,
65	prefix: &P,
66) -> impl Future<Output = usize> + Send + 'a + use<'a, P>
67where
68	P: Serialize + ?Sized + Debug + 'a,
69{
70	self.keys_prefix_raw(prefix).count()
71}
72
73/// Counts forward scan items matching a raw prefix.
74///
75/// The supplied bytes are used directly for the seek and prefix test. Iterator
76/// failures are counted as yielded items rather than returned to the caller.
77#[implement(super::Map)]
78#[inline]
79pub fn raw_count_prefix<'a, P>(
80	self: &'a Arc<Self>,
81	prefix: &'a P,
82) -> impl Future<Output = usize> + Send + 'a
83where
84	P: AsRef<[u8]> + ?Sized + Debug + Sync + 'a,
85{
86	self.raw_keys_prefix(prefix).count()
87}