@@ -25,7 +25,7 @@ use crate::{
2525 batch:: { Batch , WriteOperation } ,
2626 common:: {
2727 from_bytes_option, from_bytes_option_or_default, get_key_range_for_prefix, get_upper_bound,
28- key_matches_interval , DeletionSet , HasherOutput , SuffixClosedSetIterator , Update ,
28+ DeletionSet , HasherOutput , SuffixClosedSetIterator , Update ,
2929 } ,
3030 context:: Context ,
3131 hashable_wrapper:: WrappedHashableContainerView ,
@@ -1012,42 +1012,63 @@ impl<C: Context> KeyValueStoreView<C> {
10121012 & self ,
10131013 key_interval : KeyInterval ,
10141014 ) -> Result < ( Vec < Vec < u8 > > , bool ) , ViewError > {
1015- let key_values = self . find_key_values_by_prefix ( & [ ] ) . await ?;
1015+ if key_interval. is_empty ( ) {
1016+ return Ok ( ( Vec :: new ( ) , true ) ) ;
1017+ }
1018+ // Any key in the interval starts with the longest common prefix of
1019+ // its bounds, so the prefix-scan path (which already merges in-memory
1020+ // updates with the underlying store and respects the deletion set)
1021+ // is a sufficient superset. We then trim it to the interval and limit.
1022+ let prefix = key_interval. common_prefix ( ) ;
1023+ let prefix_keys = self . find_keys_by_prefix ( & prefix) . await ?;
10161024 let mut keys = Vec :: new ( ) ;
1017- for ( key, _value) in key_values {
1018- if key_matches_interval ( & key, key_interval. start_bound ( ) , key_interval. end_bound ( ) ) {
1019- keys. push ( key) ;
1020- if key_interval. limit . is_some_and ( |limit| keys. len ( ) >= limit) {
1021- break ;
1022- }
1025+ let mut more_after_limit = false ;
1026+ for stripped in prefix_keys {
1027+ let mut full = prefix. clone ( ) ;
1028+ full. extend ( & stripped) ;
1029+ if !key_interval. contains ( & full) {
1030+ continue ;
1031+ }
1032+ if key_interval
1033+ . limit
1034+ . is_some_and ( |limit| keys. len ( ) >= limit)
1035+ {
1036+ more_after_limit = true ;
1037+ break ;
10231038 }
1039+ keys. push ( full) ;
10241040 }
1025- let is_finished = key_interval. limit . is_none_or ( |limit| keys. len ( ) < limit) ;
1026- Ok ( ( keys, is_finished) )
1041+ Ok ( ( keys, !more_after_limit) )
10271042 }
10281043
10291044 /// Iterates over all key-value pairs matching the given interval.
10301045 pub async fn find_key_values_in_interval (
10311046 & self ,
10321047 key_interval : KeyInterval ,
10331048 ) -> Result < ( Vec < ( Vec < u8 > , Vec < u8 > ) > , bool ) , ViewError > {
1034- let entries = self . find_key_values_by_prefix ( & [ ] ) . await ?;
1049+ if key_interval. is_empty ( ) {
1050+ return Ok ( ( Vec :: new ( ) , true ) ) ;
1051+ }
1052+ let prefix = key_interval. common_prefix ( ) ;
1053+ let prefix_entries = self . find_key_values_by_prefix ( & prefix) . await ?;
10351054 let mut key_values = Vec :: new ( ) ;
1036- for ( key, value) in entries {
1037- if key_matches_interval ( & key, key_interval. start_bound ( ) , key_interval. end_bound ( ) ) {
1038- key_values. push ( ( key, value) ) ;
1039- if key_interval
1040- . limit
1041- . is_some_and ( |limit| key_values. len ( ) >= limit)
1042- {
1043- break ;
1044- }
1055+ let mut more_after_limit = false ;
1056+ for ( stripped, value) in prefix_entries {
1057+ let mut full = prefix. clone ( ) ;
1058+ full. extend ( & stripped) ;
1059+ if !key_interval. contains ( & full) {
1060+ continue ;
1061+ }
1062+ if key_interval
1063+ . limit
1064+ . is_some_and ( |limit| key_values. len ( ) >= limit)
1065+ {
1066+ more_after_limit = true ;
1067+ break ;
10451068 }
1069+ key_values. push ( ( full, value) ) ;
10461070 }
1047- let is_finished = key_interval
1048- . limit
1049- . is_none_or ( |limit| key_values. len ( ) < limit) ;
1050- Ok ( ( key_values, is_finished) )
1071+ Ok ( ( key_values, !more_after_limit) )
10511072 }
10521073
10531074 /// Iterates over all the keys matching the given prefix. The prefix is not included in the returned keys.
@@ -1330,43 +1351,60 @@ impl<C: Context> ReadableKeyValueStore for ViewContainer<C> {
13301351 & self ,
13311352 key_interval : KeyInterval ,
13321353 ) -> Result < ( Vec < Vec < u8 > > , bool ) , ViewContainerError > {
1354+ if key_interval. is_empty ( ) {
1355+ return Ok ( ( Vec :: new ( ) , true ) ) ;
1356+ }
13331357 let view = self . view . read ( ) . await ;
1334- let key_values = view. find_key_values_by_prefix ( & [ ] ) . await ?;
1358+ let prefix = key_interval. common_prefix ( ) ;
1359+ let prefix_keys = view. find_keys_by_prefix ( & prefix) . await ?;
13351360 let mut keys = Vec :: new ( ) ;
1336- for ( key, _value) in key_values {
1337- if key_matches_interval ( & key, key_interval. start_bound ( ) , key_interval. end_bound ( ) ) {
1338- keys. push ( key) ;
1339- if key_interval. limit . is_some_and ( |limit| keys. len ( ) >= limit) {
1340- break ;
1341- }
1361+ let mut more_after_limit = false ;
1362+ for stripped in prefix_keys {
1363+ let mut full = prefix. clone ( ) ;
1364+ full. extend ( & stripped) ;
1365+ if !key_interval. contains ( & full) {
1366+ continue ;
1367+ }
1368+ if key_interval
1369+ . limit
1370+ . is_some_and ( |limit| keys. len ( ) >= limit)
1371+ {
1372+ more_after_limit = true ;
1373+ break ;
13421374 }
1375+ keys. push ( full) ;
13431376 }
1344- let is_finished = key_interval. limit . is_none_or ( |limit| keys. len ( ) < limit) ;
1345- Ok ( ( keys, is_finished) )
1377+ Ok ( ( keys, !more_after_limit) )
13461378 }
13471379
13481380 async fn find_key_values_in_interval (
13491381 & self ,
13501382 key_interval : KeyInterval ,
13511383 ) -> Result < ( Vec < ( Vec < u8 > , Vec < u8 > ) > , bool ) , ViewContainerError > {
1384+ if key_interval. is_empty ( ) {
1385+ return Ok ( ( Vec :: new ( ) , true ) ) ;
1386+ }
13521387 let view = self . view . read ( ) . await ;
1353- let entries = view. find_key_values_by_prefix ( & [ ] ) . await ?;
1388+ let prefix = key_interval. common_prefix ( ) ;
1389+ let prefix_entries = view. find_key_values_by_prefix ( & prefix) . await ?;
13541390 let mut key_values = Vec :: new ( ) ;
1355- for ( key, value) in entries {
1356- if key_matches_interval ( & key, key_interval. start_bound ( ) , key_interval. end_bound ( ) ) {
1357- key_values. push ( ( key, value) ) ;
1358- if key_interval
1359- . limit
1360- . is_some_and ( |limit| key_values. len ( ) >= limit)
1361- {
1362- break ;
1363- }
1391+ let mut more_after_limit = false ;
1392+ for ( stripped, value) in prefix_entries {
1393+ let mut full = prefix. clone ( ) ;
1394+ full. extend ( & stripped) ;
1395+ if !key_interval. contains ( & full) {
1396+ continue ;
1397+ }
1398+ if key_interval
1399+ . limit
1400+ . is_some_and ( |limit| key_values. len ( ) >= limit)
1401+ {
1402+ more_after_limit = true ;
1403+ break ;
13641404 }
1405+ key_values. push ( ( full, value) ) ;
13651406 }
1366- let is_finished = key_interval
1367- . limit
1368- . is_none_or ( |limit| key_values. len ( ) < limit) ;
1369- Ok ( ( key_values, is_finished) )
1407+ Ok ( ( key_values, !more_after_limit) )
13701408 }
13711409}
13721410
0 commit comments