1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- use std:: ops:: Bound ;
15+ use std:: ops:: { Bound , RangeBounds } ;
1616
1717use prost:: Message ;
1818use quickwit_proto:: search:: {
@@ -83,16 +83,16 @@ struct CacheKey {
8383 request : SearchRequest ,
8484 /// The effective time range of the request, that is, the intersection of the timerange
8585 /// requested, and the timerange covered by the split.
86- merged_time_range : Range ,
86+ merged_time_range : HalfOpenRange ,
8787}
8888
8989impl CacheKey {
9090 fn from_split_meta_and_request (
9191 split_info : SplitIdAndFooterOffsets ,
9292 mut search_request : SearchRequest ,
9393 ) -> Self {
94- let split_time_range = Range :: from_bounds ( split_info. time_range ( ) ) ;
95- let request_time_range = Range :: from_bounds ( search_request. time_range ( ) ) ;
94+ let split_time_range = HalfOpenRange :: from_bounds ( split_info. time_range ( ) ) ;
95+ let request_time_range = HalfOpenRange :: from_bounds ( search_request. time_range ( ) ) ;
9696 let merged_time_range = request_time_range. intersect ( & split_time_range) ;
9797
9898 search_request. start_timestamp = None ;
@@ -110,28 +110,30 @@ impl CacheKey {
110110}
111111
112112/// A (half-open) range bounded inclusively below and exclusively above [start..end).
113- #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
114- struct Range {
113+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
114+ struct HalfOpenRange {
115115 start : i64 ,
116116 end : Option < i64 > ,
117117}
118118
119- impl Range {
120- /// Create a Range from bounds.
121- fn from_bounds ( range : impl std:: ops:: RangeBounds < i64 > ) -> Self {
122- let empty_range = Range {
119+ impl HalfOpenRange {
120+ fn empty_range ( ) -> HalfOpenRange {
121+ HalfOpenRange {
123122 start : 0 ,
124123 end : Some ( 0 ) ,
125- } ;
124+ }
125+ }
126126
127+ /// Create a Range from bounds.
128+ fn from_bounds ( range : impl RangeBounds < i64 > ) -> Self {
127129 let start = match range. start_bound ( ) {
128130 Bound :: Included ( start) => * start,
129131 Bound :: Excluded ( start) => {
130132 // if we exclude i64::MAX from the start bound, the range is necessarily empty
131133 if let Some ( start) = start. checked_add ( 1 ) {
132134 start
133135 } else {
134- return empty_range;
136+ return Self :: empty_range ( ) ;
135137 }
136138 }
137139 Bound :: Unbounded => i64:: MIN ,
@@ -143,44 +145,45 @@ impl Range {
143145 Bound :: Unbounded => None ,
144146 } ;
145147
146- Range { start, end }
148+ HalfOpenRange { start, end } . normalize ( )
149+ }
150+
151+ fn is_empty ( self ) -> bool {
152+ !self . contains ( & self . start )
147153 }
148154
149155 /// Normalize empty ranges to be 0..0
150- fn normalize ( self ) -> Range {
151- let empty_range = Range {
152- start : 0 ,
153- end : Some ( 0 ) ,
154- } ;
155- match self {
156- Range {
157- start,
158- end : Some ( end) ,
159- } if start >= end => empty_range,
160- any => any,
156+ fn normalize ( self ) -> HalfOpenRange {
157+ if self . is_empty ( ) {
158+ Self :: empty_range ( )
159+ } else {
160+ self
161161 }
162162 }
163163
164164 /// Return the intersection of self and other.
165- fn intersect ( & self , other : & Range ) -> Range {
165+ fn intersect ( & self , other : & HalfOpenRange ) -> HalfOpenRange {
166166 let start = self . start . max ( other. start ) ;
167-
168167 let end = match ( self . end , other. end ) {
169168 ( Some ( this) , Some ( other) ) => Some ( this. min ( other) ) ,
170169 ( Some ( this) , None ) => Some ( this) ,
171170 ( None , other) => other,
172171 } ;
173- Range { start, end } . normalize ( )
172+ HalfOpenRange { start, end } . normalize ( )
174173 }
175174}
176175
177- impl std :: ops :: RangeBounds < i64 > for Range {
176+ impl RangeBounds < i64 > for HalfOpenRange {
178177 fn start_bound ( & self ) -> Bound < & i64 > {
179178 Bound :: Included ( & self . start )
180179 }
181180
182181 fn end_bound ( & self ) -> Bound < & i64 > {
183- self . end . as_ref ( ) . map_or ( Bound :: Unbounded , Bound :: Excluded )
182+ if let Some ( end_bound) = & self . end {
183+ Bound :: Excluded ( end_bound)
184+ } else {
185+ Bound :: Unbounded
186+ }
184187 }
185188}
186189
0 commit comments