rostra_client_db/
paginate.rs1use std::{cmp, ops};
2
3use crate::{Database, DbResult};
4
5impl Database {
6 pub fn paginate_table<K, V, R>(
7 table: &impl redb_bincode::ReadableTable<K, V>,
8 cursor: Option<K>,
9 limit: usize,
10 filter_fn: impl Fn(K, V) -> DbResult<Option<R>> + Send + 'static,
11 ) -> DbResult<(Vec<R>, Option<K>)>
12 where
13 K: bincode::Decode<()> + bincode::Encode,
14 V: bincode::Decode<()> + bincode::Encode,
15 {
16 let mut ret = vec![];
17
18 for event in if let Some(cursor) = cursor {
19 table.range(&cursor..)?
20 } else {
21 table.range(..)?
22 } {
23 let (k, v) = event?;
24
25 let k = k.value();
26 if limit <= ret.len() {
27 return Ok((ret, Some(k)));
28 }
29
30 if let Some(r) = filter_fn(k, v.value())? {
31 ret.push(r);
32 }
33 }
34
35 Ok((ret, None))
36 }
37
38 pub fn paginate_table_rev<K, V, R>(
39 table: &impl redb_bincode::ReadableTable<K, V>,
40 cursor: Option<K>,
41 limit: usize,
42 filter_fn: impl Fn(K, V) -> DbResult<Option<R>> + Send + 'static,
43 ) -> DbResult<(Vec<R>, Option<K>)>
44 where
45 K: bincode::Decode<()> + bincode::Encode,
46 V: bincode::Decode<()> + bincode::Encode,
47 {
48 let mut ret = vec![];
49
50 for event in if let Some(cursor) = cursor {
51 table.range(..=&cursor)?
52 } else {
53 table.range(..)?
54 }
55 .rev()
56 {
57 let (k, v) = event?;
58
59 let k = k.value();
60 if limit <= ret.len() {
61 return Ok((ret, Some(k)));
62 }
63
64 if let Some(r) = filter_fn(k, v.value())? {
65 ret.push(r);
66 }
67 }
68
69 Ok((ret, None))
70 }
71
72 pub fn paginate_table_partition<K, V, C, R>(
73 table: &impl redb_bincode::ReadableTable<K, V>,
74 prefix: ops::RangeInclusive<K>,
75 cursor_to_key: impl Fn(C) -> K,
76 cursor: Option<C>,
77 limit: usize,
78 filter_fn: impl Fn(K, V) -> DbResult<Option<R>> + Send + 'static,
79 ) -> DbResult<(Vec<R>, Option<K>)>
80 where
81 K: bincode::Decode<()> + bincode::Encode + cmp::Ord,
82 V: bincode::Decode<()> + bincode::Encode,
83 {
84 let mut ret = vec![];
85
86 let (prefix_start, prefix_end) = prefix.into_inner();
87
88 let start = if let Some(cursor) = cursor {
89 cursor_to_key(cursor).max(prefix_start)
90 } else {
91 prefix_start
92 };
93
94 for event in table.range(&start..=&prefix_end)? {
95 let (k, v) = event?;
96
97 let k = k.value();
98 if limit <= ret.len() {
99 return Ok((ret, Some(k)));
100 }
101
102 if let Some(r) = filter_fn(k, v.value())? {
103 ret.push(r);
104 }
105 }
106
107 Ok((ret, None))
108 }
109
110 pub fn paginate_table_partition_rev<K, V, C, R>(
111 table: &impl redb_bincode::ReadableTable<K, V>,
112 prefix: ops::RangeInclusive<K>,
113 cursor_to_key: impl Fn(C) -> K,
114 cursor: Option<C>,
115 limit: usize,
116 filter_fn: impl Fn(K, V) -> DbResult<Option<R>> + Send + 'static,
117 ) -> DbResult<(Vec<R>, Option<K>)>
118 where
119 K: bincode::Decode<()> + bincode::Encode + cmp::Ord,
120 V: bincode::Decode<()> + bincode::Encode,
121 {
122 let mut ret = vec![];
123
124 let (prefix_start, prefix_end) = prefix.into_inner();
125
126 let end = if let Some(cursor) = cursor {
127 cursor_to_key(cursor).min(prefix_end)
128 } else {
129 prefix_end
130 };
131 for event in table.range(&prefix_start..&end)?.rev() {
132 let (k, v) = event?;
133
134 let k = k.value();
135 if limit <= ret.len() {
136 return Ok((ret, Some(k)));
137 }
138
139 if let Some(r) = filter_fn(k, v.value())? {
140 ret.push(r);
141 }
142 }
143
144 Ok((ret, None))
145 }
146}
147
148#[cfg(test)]
149mod tests;