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,8 +61,7 @@ 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,
@ -66,7 +74,6 @@ impl<'t> Criterion for Geo<'t> {
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)?, (None, None) => self.index.documents_ids(self.rtxn)?,
}; };
@ -75,41 +82,34 @@ impl<'t> Criterion for Geo<'t> {
} }
match bucket_candidates { match bucket_candidates {
// why not are we keeping elements from the previous bucket? Some(bucket_candidates) => self.bucket_candidates |= bucket_candidates,
Some(bucket_candidates) => {
self.bucket_candidates |= bucket_candidates
}
None => self.bucket_candidates |= &candidates, None => self.bucket_candidates |= &candidates,
} }
if candidates.is_empty() { if candidates.is_empty() {
continue; continue;
} }
let rtree = Box::new(rtree.clone());
let rtree = Box::leak(rtree);
self.allowed_candidates = &candidates - params.excluded_candidates; self.allowed_candidates = &candidates - params.excluded_candidates;
self.candidates = geo_point(rtree, self.allowed_candidates.clone(), self.point)?; self.candidates =
geo_point(rtree, self.allowed_candidates.clone(), self.point);
} }
None => return Ok(None), 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)| { .filter_map(move |(point, _distance)| candidates.contains(point.data).then(|| point.data))
candidates.contains(point.data).then(|| point.data)
})
.map(|id| std::iter::once(id).collect::<RoaringBitmap>()) .map(|id| std::iter::once(id).collect::<RoaringBitmap>())
)) .collect::<Vec<_>>();
Box::new(results.into_iter())
} }