Skip to content

Commit

Permalink
Fix the bug in Immutable RTree object strategy (apache#16389)
Browse files Browse the repository at this point in the history
* Fix the bug in Immutable Node object strategy

* Adding comments in code
  • Loading branch information
pranavbhole authored May 6, 2024
1 parent 2a638d7 commit b713a51
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ public Class<ImmutableRTree> getClazz()
@Override
public ImmutableRTree fromByteBuffer(ByteBuffer buffer, int numBytes)
{
buffer.limit(buffer.position() + numBytes);
return new ImmutableRTree(buffer, bitmapFactory);
// always create the duplicate buffer for creating the objects as original buffer may have mutations somewhere else which can corrupt objects
ByteBuffer duplicateBuf = buffer.duplicate();
duplicateBuf.limit(duplicateBuf.position() + numBytes);
return new ImmutableRTree(duplicateBuf, bitmapFactory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.concurrent.TimeUnit;

/**
*
*/
@SuppressWarnings("CheckReturnValue")
public class ImmutableRTreeTest
Expand Down Expand Up @@ -702,4 +703,37 @@ public void testPreciseRadiusBoundFilter()
ImmutableBitmap finalSet = bf.union(points);
Assert.assertTrue(finalSet.size() == 100);
}

@Test
public void testForImmutableRTreeImmutability()
{
BitmapFactory bf = new RoaringBitmapFactory();
RTree tree = new RTree(2, new LinearGutmanSplitStrategy(0, 50, bf), bf);
float centerLat = 37.4133961f;
float centerLong = -122.1224665f;
float[][] insidePoints = SpatialUtils.generateGeoCoordinatesAroundCircle(centerLat, centerLong, 100, 100, true);
for (int i = 0; i < insidePoints.length; i++) {
tree.insert(insidePoints[i], i);
}
float[][] outsidePoints = SpatialUtils.generateGeoCoordinatesAroundCircle(centerLat, centerLong, 100, 100, false);
for (int i = 0; i < outsidePoints.length; i++) {
tree.insert(outsidePoints[i], i);
}
final ImmutableRTree searchTree = ImmutableRTree.newImmutableFromMutable(tree);
ByteBuffer buffer = ByteBuffer.wrap(searchTree.toBytes());
ImmutableRTreeObjectStrategy objectStrategy = new ImmutableRTreeObjectStrategy(bf);
ImmutableRTree builtTree = objectStrategy.fromByteBuffer(buffer, searchTree.toBytes().length);
buffer.position(10);
buffer.limit(100);

for (float[] insidePoint : insidePoints) {
Iterable<ImmutableBitmap> points = builtTree.search(new RadiusBound(
new float[]{insidePoint[0], insidePoint[1]},
100,
2,
RadiusBound.RadiusUnit.meters
));
bf.union(points);
}
}
}

0 comments on commit b713a51

Please sign in to comment.