Skip to main content

tuwunel_database/map/
count.rs

1use std::{fmt::Debug, future::Future, sync::Arc};
2
3use futures::stream::StreamExt;
4use serde::Serialize;
5use tuwunel_core::implement;
6
7/// Count the total number of entries in the map.
8#[implement(super::Map)]
9#[inline]
10pub fn count(self: &Arc<Self>) -> impl Future<Output = usize> + Send + '_ {
11	self.raw_keys().count()
12}
13
14/// Count the number of entries in the map starting from a lower-bound.
15///
16/// - From is a structured key
17#[implement(super::Map)]
18#[inline]
19pub fn count_from<'a, P>(
20	self: &'a Arc<Self>,
21	from: &P,
22) -> impl Future<Output = usize> + Send + 'a + use<'a, P>
23where
24	P: Serialize + ?Sized + Debug + 'a,
25{
26	self.keys_from_raw(from).count()
27}
28
29/// Count the number of entries in the map starting from a lower-bound.
30///
31/// - From is a raw
32#[implement(super::Map)]
33#[inline]
34pub fn raw_count_from<'a, P>(
35	self: &'a Arc<Self>,
36	from: &'a P,
37) -> impl Future<Output = usize> + Send + 'a
38where
39	P: AsRef<[u8]> + ?Sized + Debug + Sync + 'a,
40{
41	self.raw_keys_from(from).count()
42}
43
44/// Count the number of entries in the map matching a prefix.
45///
46/// - Prefix is structured key
47#[implement(super::Map)]
48#[inline]
49pub fn count_prefix<'a, P>(
50	self: &'a Arc<Self>,
51	prefix: &P,
52) -> impl Future<Output = usize> + Send + 'a + use<'a, P>
53where
54	P: Serialize + ?Sized + Debug + 'a,
55{
56	self.keys_prefix_raw(prefix).count()
57}
58
59/// Count the number of entries in the map matching a prefix.
60///
61/// - Prefix is raw
62#[implement(super::Map)]
63#[inline]
64pub fn raw_count_prefix<'a, P>(
65	self: &'a Arc<Self>,
66	prefix: &'a P,
67) -> impl Future<Output = usize> + Send + 'a
68where
69	P: AsRef<[u8]> + ?Sized + Debug + Sync + 'a,
70{
71	self.raw_keys_prefix(prefix).count()
72}