@@ -272,75 +272,91 @@ pub trait Persistence: Sync + Send + 'static {
272272
273273#[ derive( Debug , Clone , Copy ) ]
274274pub struct TimestampRange {
275- start_bound : Bound < Timestamp > ,
276- end_bound : Bound < Timestamp > ,
275+ start_inclusive : Timestamp ,
276+ end_inclusive : Timestamp ,
277277}
278278
279279impl TimestampRange {
280- pub fn new < T : RangeBounds < Timestamp > > ( range : T ) -> anyhow:: Result < Self > {
281- // Bounds check.
282- Self :: min_inclusive ( & range. start_bound ( ) . cloned ( ) ) ?;
283- Self :: max_exclusive ( & range. end_bound ( ) . cloned ( ) ) ?;
284- Ok ( Self {
285- start_bound : range. start_bound ( ) . cloned ( ) ,
286- end_bound : range. end_bound ( ) . cloned ( ) ,
287- } )
288- }
289-
290- pub fn snapshot ( ts : Timestamp ) -> Self {
280+ #[ inline]
281+ pub fn new < T : RangeBounds < Timestamp > > ( range : T ) -> Self {
282+ let start_inclusive = match range. start_bound ( ) {
283+ Bound :: Included ( t) => * t,
284+ Bound :: Excluded ( t) => {
285+ if let Some ( succ) = t. succ_opt ( ) {
286+ succ
287+ } else {
288+ return Self :: empty ( ) ;
289+ }
290+ } ,
291+ Bound :: Unbounded => Timestamp :: MIN ,
292+ } ;
293+ let end_inclusive = match range. end_bound ( ) {
294+ Bound :: Included ( t) => * t,
295+ Bound :: Excluded ( t) => {
296+ if let Some ( pred) = t. pred_opt ( ) {
297+ pred
298+ } else {
299+ return Self :: empty ( ) ;
300+ }
301+ } ,
302+ Bound :: Unbounded => Timestamp :: MAX ,
303+ } ;
291304 Self {
292- start_bound : Bound :: Unbounded ,
293- end_bound : Bound :: Included ( ts ) ,
305+ start_inclusive ,
306+ end_inclusive ,
294307 }
295308 }
296309
297- pub fn all ( ) -> Self {
310+ #[ inline]
311+ pub fn empty ( ) -> Self {
298312 Self {
299- start_bound : Bound :: Unbounded ,
300- end_bound : Bound :: Unbounded ,
313+ start_inclusive : Timestamp :: MAX ,
314+ end_inclusive : Timestamp :: MIN ,
301315 }
302316 }
303317
304- pub fn at ( ts : Timestamp ) -> Self {
305- Self {
306- start_bound : Bound :: Included ( ts) ,
307- end_bound : Bound :: Included ( ts) ,
308- }
318+ #[ inline]
319+ pub fn snapshot ( ts : Timestamp ) -> Self {
320+ Self :: new ( ..=ts)
309321 }
310322
311- pub fn greater_than ( t : Timestamp ) -> Self {
312- Self {
313- start_bound : Bound :: Excluded ( t) ,
314- end_bound : Bound :: Unbounded ,
315- }
323+ #[ inline]
324+ pub fn all ( ) -> Self {
325+ Self :: new ( ..)
316326 }
317327
318- fn min_inclusive ( start_bound : & Bound < Timestamp > ) -> anyhow:: Result < Timestamp > {
319- Ok ( match start_bound {
320- Bound :: Included ( t) => * t,
321- Bound :: Excluded ( t) => t. succ ( ) ?,
322- Bound :: Unbounded => Timestamp :: MIN ,
323- } )
328+ #[ inline]
329+ pub fn at ( ts : Timestamp ) -> Self {
330+ Self :: new ( ts..=ts)
324331 }
325332
326- pub fn min_timestamp_inclusive ( & self ) -> Timestamp {
327- Self :: min_inclusive ( & self . start_bound ) . unwrap ( )
333+ #[ inline]
334+ pub fn greater_than ( t : Timestamp ) -> Self {
335+ Self :: new ( ( Bound :: Excluded ( t) , Bound :: Unbounded ) )
328336 }
329337
330- fn max_exclusive ( end_bound : & Bound < Timestamp > ) -> anyhow:: Result < Timestamp > {
331- Ok ( match end_bound {
332- Bound :: Included ( t) => t. succ ( ) ?,
333- Bound :: Excluded ( t) => * t,
334- Bound :: Unbounded => Timestamp :: MAX ,
335- } )
338+ #[ inline]
339+ pub fn min_timestamp_inclusive ( & self ) -> Timestamp {
340+ self . start_inclusive
336341 }
337342
343+ #[ inline]
338344 pub fn max_timestamp_exclusive ( & self ) -> Timestamp {
339- Self :: max_exclusive ( & self . end_bound ) . unwrap ( )
345+ // assumes that Timestamp::MAX never actually exists
346+ self . end_inclusive . succ_opt ( ) . unwrap_or ( Timestamp :: MAX )
340347 }
341348
349+ #[ inline]
342350 pub fn contains ( & self , ts : Timestamp ) -> bool {
343- self . min_timestamp_inclusive ( ) <= ts && ts < self . max_timestamp_exclusive ( )
351+ self . start_inclusive <= ts && ts <= self . end_inclusive
352+ }
353+
354+ #[ inline]
355+ pub fn intersect ( & self , other : Self ) -> Self {
356+ Self {
357+ start_inclusive : self . start_inclusive . max ( other. start_inclusive ) ,
358+ end_inclusive : self . end_inclusive . min ( other. end_inclusive ) ,
359+ }
344360 }
345361}
346362
@@ -582,13 +598,12 @@ impl RepeatablePersistence {
582598 /// Same as [`Persistence::load_documents`] but only including documents in
583599 /// the snapshot range.
584600 pub fn load_documents ( & self , range : TimestampRange , order : Order ) -> DocumentStream < ' _ > {
585- let stream = self . reader . load_documents (
586- range,
601+ self . reader . load_documents (
602+ range. intersect ( TimestampRange :: snapshot ( * self . upper_bound ) ) ,
587603 order,
588604 * DEFAULT_DOCUMENTS_PAGE_SIZE ,
589605 self . retention_validator . clone ( ) ,
590- ) ;
591- Box :: pin ( stream. try_filter ( |entry| future:: ready ( entry. ts <= * self . upper_bound ) ) )
606+ )
592607 }
593608
594609 /// Same as [`Persistence::load_documents_from_table`] but only including
@@ -599,14 +614,13 @@ impl RepeatablePersistence {
599614 range : TimestampRange ,
600615 order : Order ,
601616 ) -> DocumentStream < ' _ > {
602- let stream = self . reader . load_documents_from_table (
617+ self . reader . load_documents_from_table (
603618 tablet_id,
604- range,
619+ range. intersect ( TimestampRange :: snapshot ( * self . upper_bound ) ) ,
605620 order,
606621 * DEFAULT_DOCUMENTS_PAGE_SIZE ,
607622 self . retention_validator . clone ( ) ,
608- ) ;
609- Box :: pin ( stream. try_filter ( |entry| future:: ready ( entry. ts <= * self . upper_bound ) ) )
623+ )
610624 }
611625
612626 /// Same as `load_documents` but doesn't use the `RetentionValidator` from
@@ -618,13 +632,12 @@ impl RepeatablePersistence {
618632 order : Order ,
619633 retention_validator : Arc < dyn RetentionValidator > ,
620634 ) -> DocumentStream < ' _ > {
621- let stream = self . reader . load_documents (
622- range,
635+ self . reader . load_documents (
636+ range. intersect ( TimestampRange :: snapshot ( * self . upper_bound ) ) ,
623637 order,
624638 * DEFAULT_DOCUMENTS_PAGE_SIZE ,
625639 retention_validator,
626- ) ;
627- Box :: pin ( stream. try_filter ( |entry| future:: ready ( entry. ts <= * self . upper_bound ) ) )
640+ )
628641 }
629642
630643 pub async fn previous_revisions (
0 commit comments