Skip to content

Commit

Permalink
Never expand a range in a way that leaves a hole
Browse files Browse the repository at this point in the history
  • Loading branch information
jleibs committed May 10, 2024
1 parent 98e9254 commit b5807be
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions crates/re_query/src/range/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,20 @@ impl RangeComponentResultsInner {
front.into_iter().chain(back)
}

/// Given a `query`, returns a reduced query that is sufficient to fill the missing data
/// Given a `query`, returns an augmented query that is sufficient to fill the missing data
/// on the front side of the cache, or `None` if all the necessary data is already
/// cached.
///
/// This query may be larger than the requested data range, as it may need to fill in gaps.
pub fn compute_front_query(&self, query: &RangeQuery) -> Option<RangeQuery> {
let mut reduced_query = query.clone();
let mut augmented_query = query.clone();

// If nothing has been cached already, then we just want to query everything.
if self.indices.is_empty()
&& self.promises_front.is_empty()
&& self.promises_back.is_empty()
{
return Some(reduced_query);
return Some(augmented_query);
}

// If the cache contains static data, then there's no point in querying anything else since
Expand Down Expand Up @@ -375,31 +377,29 @@ impl RangeComponentResultsInner {

if let Some(time_range) = self.time_range() {
let time_range_min = i64::min(time_range.min().as_i64().saturating_sub(1), pending_min);
reduced_query
.range
.set_max(i64::min(reduced_query.range.max().as_i64(), time_range_min));
augmented_query.range.set_max(time_range_min);
} else {
reduced_query
.range
.set_max(i64::min(reduced_query.range.max().as_i64(), pending_min));
augmented_query.range.set_max(pending_min);
}

if reduced_query.range.max() < reduced_query.range.min() {
if augmented_query.range.max() < augmented_query.range.min() {
return None;
}

Some(reduced_query)
Some(augmented_query)
}

/// Given a `query`, returns a reduced query that is sufficient to fill the missing data
/// Given a `query`, returns a augmented query that is sufficient to fill the missing data
/// on the back side of the cache, or `None` if all the necessary data is already
/// cached.
///
/// This query may be larger than the requested data range, as it may need to fill in gaps.
pub fn compute_back_query(
&self,
query: &RangeQuery,
query_front: Option<&RangeQuery>,
) -> Option<RangeQuery> {
let mut reduced_query = query.clone();
let mut augmented_query = query.clone();

// If nothing has been cached already, then the front query is already going to take care
// of everything.
Expand Down Expand Up @@ -456,29 +456,25 @@ impl RangeComponentResultsInner {

if let Some(time_range) = self.time_range() {
let time_range_max = i64::max(time_range.max().as_i64().saturating_add(1), pending_max);
reduced_query
.range
.set_min(i64::max(reduced_query.range.min().as_i64(), time_range_max));
augmented_query.range.set_min(time_range_max);
} else {
reduced_query
.range
.set_min(i64::max(reduced_query.range.min().as_i64(), pending_max));
augmented_query.range.set_min(pending_max);
}

// Back query should never overlap with the front query.
// Reminder: time ranges are all inclusive.
if let Some(query_front) = query_front {
let front_max_plus_one = query_front.range().max().as_i64().saturating_add(1);
let back_min = reduced_query.range().min().as_i64();
reduced_query
let back_min = augmented_query.range().min().as_i64();
augmented_query
.range
.set_min(i64::max(back_min, front_max_plus_one));
}

if reduced_query.range.max() < reduced_query.range.min() {
if augmented_query.range.max() < augmented_query.range.min() {
return None;
}

Some(reduced_query)
Some(augmented_query)
}
}

0 comments on commit b5807be

Please sign in to comment.