12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use std:: ops:: Bound ;
15
+ use std:: ops:: { Bound , RangeBounds } ;
16
16
17
17
use prost:: Message ;
18
18
use quickwit_proto:: search:: {
@@ -83,16 +83,16 @@ struct CacheKey {
83
83
request : SearchRequest ,
84
84
/// The effective time range of the request, that is, the intersection of the timerange
85
85
/// requested, and the timerange covered by the split.
86
- merged_time_range : Range ,
86
+ merged_time_range : HalfOpenRange ,
87
87
}
88
88
89
89
impl CacheKey {
90
90
fn from_split_meta_and_request (
91
91
split_info : SplitIdAndFooterOffsets ,
92
92
mut search_request : SearchRequest ,
93
93
) -> 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 ( ) ) ;
96
96
let merged_time_range = request_time_range. intersect ( & split_time_range) ;
97
97
98
98
search_request. start_timestamp = None ;
@@ -110,28 +110,30 @@ impl CacheKey {
110
110
}
111
111
112
112
/// 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 {
115
115
start : i64 ,
116
116
end : Option < i64 > ,
117
117
}
118
118
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 {
123
122
start : 0 ,
124
123
end : Some ( 0 ) ,
125
- } ;
124
+ }
125
+ }
126
126
127
+ /// Create a Range from bounds.
128
+ fn from_bounds ( range : impl RangeBounds < i64 > ) -> Self {
127
129
let start = match range. start_bound ( ) {
128
130
Bound :: Included ( start) => * start,
129
131
Bound :: Excluded ( start) => {
130
132
// if we exclude i64::MAX from the start bound, the range is necessarily empty
131
133
if let Some ( start) = start. checked_add ( 1 ) {
132
134
start
133
135
} else {
134
- return empty_range;
136
+ return Self :: empty_range ( ) ;
135
137
}
136
138
}
137
139
Bound :: Unbounded => i64:: MIN ,
@@ -143,44 +145,45 @@ impl Range {
143
145
Bound :: Unbounded => None ,
144
146
} ;
145
147
146
- Range { start, end }
148
+ HalfOpenRange { start, end } . normalize ( )
149
+ }
150
+
151
+ fn is_empty ( self ) -> bool {
152
+ !self . contains ( & self . start )
147
153
}
148
154
149
155
/// 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
161
161
}
162
162
}
163
163
164
164
/// Return the intersection of self and other.
165
- fn intersect ( & self , other : & Range ) -> Range {
165
+ fn intersect ( & self , other : & HalfOpenRange ) -> HalfOpenRange {
166
166
let start = self . start . max ( other. start ) ;
167
-
168
167
let end = match ( self . end , other. end ) {
169
168
( Some ( this) , Some ( other) ) => Some ( this. min ( other) ) ,
170
169
( Some ( this) , None ) => Some ( this) ,
171
170
( None , other) => other,
172
171
} ;
173
- Range { start, end } . normalize ( )
172
+ HalfOpenRange { start, end } . normalize ( )
174
173
}
175
174
}
176
175
177
- impl std :: ops :: RangeBounds < i64 > for Range {
176
+ impl RangeBounds < i64 > for HalfOpenRange {
178
177
fn start_bound ( & self ) -> Bound < & i64 > {
179
178
Bound :: Included ( & self . start )
180
179
}
181
180
182
181
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
+ }
184
187
}
185
188
}
186
189
0 commit comments