Fix the bug in Immutable RTree object strategy (#16389)

* Fix the bug in Immutable Node object strategy

* Adding comments in code
This commit is contained in:
Pranav 2024-05-06 02:07:29 -07:00 committed by GitHub
parent 2a638d77d9
commit b713a517f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 2 deletions

View File

@ -63,8 +63,10 @@ public class ImmutableRTreeObjectStrategy implements ObjectStrategy<ImmutableRTr
@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

View File

@ -48,6 +48,7 @@ import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
/**
*
*/
@SuppressWarnings("CheckReturnValue")
public class ImmutableRTreeTest
@ -702,4 +703,37 @@ public class ImmutableRTreeTest
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);
}
}
}