remove the memory leak

This commit is contained in:
Tamo 2021-09-01 17:02:51 +02:00
parent a8a1f5bd55
commit aca707413c
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69

View File

@ -28,7 +28,16 @@ impl<'t> Geo<'t> {
let bucket_candidates = RoaringBitmap::new(); let bucket_candidates = RoaringBitmap::new();
let rtree = index.geo_rtree(rtxn)?; let rtree = index.geo_rtree(rtxn)?;
Ok(Self { index, rtxn, parent, candidates, allowed_candidates, bucket_candidates, rtree, point }) Ok(Self {
index,
rtxn,
parent,
candidates,
allowed_candidates,
bucket_candidates,
rtree,
point,
})
} }
} }
@ -52,64 +61,55 @@ impl<'t> Criterion for Geo<'t> {
bucket_candidates: Some(self.bucket_candidates.clone()), bucket_candidates: Some(self.bucket_candidates.clone()),
})); }));
} }
None => { None => match self.parent.next(params)? {
match self.parent.next(params)? { Some(CriterionResult {
Some(CriterionResult { query_tree,
query_tree, candidates,
candidates, filtered_candidates,
filtered_candidates, bucket_candidates,
bucket_candidates, }) => {
}) => { let mut candidates = match (&query_tree, candidates) {
let mut candidates = match (&query_tree, candidates) { (_, Some(candidates)) => candidates,
(_, Some(candidates)) => candidates, (Some(qt), None) => {
(Some(qt), None) => { let context = CriteriaBuilder::new(&self.rtxn, &self.index)?;
let context = CriteriaBuilder::new(&self.rtxn, &self.index)?; resolve_query_tree(&context, qt, params.wdcache)?
resolve_query_tree(&context, qt, params.wdcache)?
}
// TODO: TAMO: why are we doing this?
(None, None) => self.index.documents_ids(self.rtxn)?,
};
if let Some(filtered_candidates) = filtered_candidates {
candidates &= filtered_candidates;
} }
(None, None) => self.index.documents_ids(self.rtxn)?,
};
match bucket_candidates { if let Some(filtered_candidates) = filtered_candidates {
// why not are we keeping elements from the previous bucket? candidates &= filtered_candidates;
Some(bucket_candidates) => {
self.bucket_candidates |= bucket_candidates
}
None => self.bucket_candidates |= &candidates,
}
if candidates.is_empty() {
continue;
}
let rtree = Box::new(rtree.clone());
let rtree = Box::leak(rtree);
self.allowed_candidates = &candidates - params.excluded_candidates;
self.candidates = geo_point(rtree, self.allowed_candidates.clone(), self.point)?;
} }
None => return Ok(None),
match bucket_candidates {
Some(bucket_candidates) => self.bucket_candidates |= bucket_candidates,
None => self.bucket_candidates |= &candidates,
}
if candidates.is_empty() {
continue;
}
self.allowed_candidates = &candidates - params.excluded_candidates;
self.candidates =
geo_point(rtree, self.allowed_candidates.clone(), self.point);
} }
} None => return Ok(None),
},
} }
} }
} }
} }
fn geo_point<'t>( fn geo_point(
rtree: &'t RTree<GeoPoint>, rtree: &RTree<GeoPoint>,
candidates: RoaringBitmap, candidates: RoaringBitmap,
point: [f64; 2], point: [f64; 2],
) -> Result<Box<dyn Iterator<Item = RoaringBitmap> + 't>> { ) -> Box<dyn Iterator<Item = RoaringBitmap>> {
Ok(Box::new( let results = rtree
rtree .nearest_neighbor_iter_with_distance_2(&point)
.nearest_neighbor_iter_with_distance_2(&point) .filter_map(move |(point, _distance)| candidates.contains(point.data).then(|| point.data))
.filter_map(move |(point, _distance)| { .map(|id| std::iter::once(id).collect::<RoaringBitmap>())
candidates.contains(point.data).then(|| point.data) .collect::<Vec<_>>();
})
.map(|id| std::iter::once(id).collect::<RoaringBitmap>()) Box::new(results.into_iter())
))
} }