11//! Range, reverse, and prefix scans.
22
3+ use bytes:: Bytes ;
4+
35use crate :: Result ;
46use crate :: vfs:: Vfs ;
57
@@ -33,13 +35,13 @@ impl<V: Vfs> BTree<V> {
3335 /// lets the write path skip sibling-pointer `CoW` (saves ~2 leaf rewrites
3436 /// per put) at the cost of one extra internal-node lookup per leaf
3537 /// boundary crossed during a scan.
36- pub async fn collect_range ( & self , start : & [ u8 ] , end : & [ u8 ] ) -> Result < Vec < ( Vec < u8 > , Vec < u8 > ) > > {
38+ pub async fn collect_range ( & self , start : & [ u8 ] , end : & [ u8 ] ) -> Result < Vec < ( Bytes , Bytes ) > > {
3739 if self . root_page_id == 0 {
3840 return Ok ( Vec :: new ( ) ) ;
3941 }
4042 let mut path = self . path_to_leaf_for_key ( start) . await ?;
4143 let mut seen_leaves = scan_guard ( ) ;
42- let mut out: Vec < ( Vec < u8 > , Vec < u8 > ) > = Vec :: new ( ) ;
44+ let mut out: Vec < ( Bytes , Bytes ) > = Vec :: new ( ) ;
4345 loop {
4446 let leaf_id = * path. last ( ) . expect ( "non-empty path" ) ;
4547 seen_leaves. insert ( leaf_id) ?;
@@ -50,7 +52,7 @@ impl<V: Vfs> BTree<V> {
5052 }
5153 if k. as_slice ( ) >= start {
5254 let val = self . resolve_leaf_value ( v) . await ?;
53- out. push ( ( k . clone ( ) , val) ) ;
55+ out. push ( ( Bytes :: copy_from_slice ( k ) , val) ) ;
5456 }
5557 }
5658 match self . next_leaf_after ( & path) . await ? {
@@ -68,22 +70,22 @@ impl<V: Vfs> BTree<V> {
6870 /// bound is beyond the valid key domain: a bounded "scan everything" would
6971 /// silently drop records at the top of the keyspace (`[0xFF; N]` and any
7072 /// key extending it). Callers that need the whole tree must use this.
71- pub async fn collect_all ( & self ) -> Result < Vec < ( Vec < u8 > , Vec < u8 > ) > > {
73+ pub async fn collect_all ( & self ) -> Result < Vec < ( Bytes , Bytes ) > > {
7274 if self . root_page_id == 0 {
7375 return Ok ( Vec :: new ( ) ) ;
7476 }
7577 // The empty key sorts below every stored key, so the descent lands on
7678 // the leftmost leaf.
7779 let mut path = self . path_to_leaf_for_key ( & [ ] ) . await ?;
7880 let mut seen_leaves = scan_guard ( ) ;
79- let mut out: Vec < ( Vec < u8 > , Vec < u8 > ) > = Vec :: new ( ) ;
81+ let mut out: Vec < ( Bytes , Bytes ) > = Vec :: new ( ) ;
8082 loop {
8183 let leaf_id = * path. last ( ) . expect ( "non-empty path" ) ;
8284 seen_leaves. insert ( leaf_id) ?;
8385 let leaf = self . read_leaf ( leaf_id) . await ?;
8486 for ( k, v) in & leaf. records {
8587 let val = self . resolve_leaf_value ( v) . await ?;
86- out. push ( ( k . clone ( ) , val) ) ;
88+ out. push ( ( Bytes :: copy_from_slice ( k ) , val) ) ;
8789 }
8890 match self . next_leaf_after ( & path) . await ? {
8991 Some ( next_path) => path = next_path,
@@ -109,13 +111,13 @@ impl<V: Vfs> BTree<V> {
109111 & self ,
110112 start : & [ u8 ] ,
111113 limit : usize ,
112- ) -> Result < Vec < ( Vec < u8 > , Vec < u8 > ) > > {
114+ ) -> Result < Vec < ( Bytes , Bytes ) > > {
113115 if self . root_page_id == 0 || limit == 0 {
114116 return Ok ( Vec :: new ( ) ) ;
115117 }
116118 let mut path = self . path_to_leaf_for_key ( start) . await ?;
117119 let mut seen_leaves = scan_guard ( ) ;
118- let mut out: Vec < ( Vec < u8 > , Vec < u8 > ) > = Vec :: with_capacity ( limit) ;
120+ let mut out: Vec < ( Bytes , Bytes ) > = Vec :: with_capacity ( limit) ;
119121 loop {
120122 let leaf_id = * path. last ( ) . expect ( "non-empty path" ) ;
121123 seen_leaves. insert ( leaf_id) ?;
@@ -128,7 +130,7 @@ impl<V: Vfs> BTree<V> {
128130 return Ok ( out) ;
129131 }
130132 let val = self . resolve_leaf_value ( v) . await ?;
131- out. push ( ( k . clone ( ) , val) ) ;
133+ out. push ( ( Bytes :: copy_from_slice ( k ) , val) ) ;
132134 }
133135 if out. len ( ) == limit {
134136 return Ok ( out) ;
@@ -155,7 +157,7 @@ impl<V: Vfs> BTree<V> {
155157 prefix : & [ u8 ] ,
156158 start : & [ u8 ] ,
157159 limit : usize ,
158- ) -> Result < Vec < ( Vec < u8 > , Vec < u8 > ) > > {
160+ ) -> Result < Vec < ( Bytes , Bytes ) > > {
159161 let mut batch = self . collect_batch_from ( start, limit) . await ?;
160162 if let Some ( end) = batch. iter ( ) . position ( |( key, _) | !key. starts_with ( prefix) ) {
161163 batch. truncate ( end) ;
@@ -179,21 +181,21 @@ impl<V: Vfs> BTree<V> {
179181
180182 /// Reverse range scan: `start` inclusive, `end` exclusive. Returns results
181183 /// in descending key order. Collects matching records forward then reverses.
182- pub async fn scan_rev ( & self , start : & [ u8 ] , end : & [ u8 ] ) -> Result < Vec < ( Vec < u8 > , Vec < u8 > ) > > {
184+ pub async fn scan_rev ( & self , start : & [ u8 ] , end : & [ u8 ] ) -> Result < Vec < ( Bytes , Bytes ) > > {
183185 let mut forward = self . collect_range ( start, end) . await ?;
184186 forward. reverse ( ) ;
185187 Ok ( forward)
186188 }
187189
188190 /// Prefix scan: returns all records whose key starts with `prefix`, in
189191 /// ascending order.
190- pub async fn scan_prefix ( & self , prefix : & [ u8 ] ) -> Result < Vec < ( Vec < u8 > , Vec < u8 > ) > > {
192+ pub async fn scan_prefix ( & self , prefix : & [ u8 ] ) -> Result < Vec < ( Bytes , Bytes ) > > {
191193 if self . root_page_id == 0 {
192194 return Ok ( Vec :: new ( ) ) ;
193195 }
194196 let mut path = self . path_to_leaf_for_key ( prefix) . await ?;
195197 let mut seen_leaves = scan_guard ( ) ;
196- let mut out: Vec < ( Vec < u8 > , Vec < u8 > ) > = Vec :: new ( ) ;
198+ let mut out: Vec < ( Bytes , Bytes ) > = Vec :: new ( ) ;
197199 loop {
198200 let leaf_id = * path. last ( ) . expect ( "non-empty path" ) ;
199201 seen_leaves. insert ( leaf_id) ?;
@@ -208,7 +210,7 @@ impl<V: Vfs> BTree<V> {
208210 break ;
209211 }
210212 let val = self . resolve_leaf_value ( v) . await ?;
211- out. push ( ( k . clone ( ) , val) ) ;
213+ out. push ( ( Bytes :: copy_from_slice ( k ) , val) ) ;
212214 }
213215 if past_prefix {
214216 return Ok ( out) ;
0 commit comments