use of org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry in project bboxdb by jnidzwetzki.
the class TestRTreeMemoryDeserializer method testSerializeIndex3D.
/**
* Test the encoding and the decoding of the index
* @throws StorageManagerException
* @throws IOException
* @throws InterruptedException
*/
@Test(timeout = 60000)
public void testSerializeIndex3D() throws StorageManagerException, IOException, InterruptedException {
final List<SpatialIndexEntry> tupleList = RTreeTestHelper.generateRandomTupleList(3);
final SpatialIndexBuilder index = new RTreeBuilder();
index.bulkInsert(tupleList);
RTreeTestHelper.queryIndex(tupleList, index);
final File tempFile = File.createTempFile("rtree-", "-test");
tempFile.deleteOnExit();
final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
index.writeToFile(raf);
raf.close();
final AbstractRTreeReader indexRead = getRTreeReader();
final RandomAccessFile rafRead = new RandomAccessFile(tempFile, "r");
indexRead.readFromFile(rafRead);
rafRead.close();
RTreeTestHelper.queryIndex(tupleList, indexRead);
}
use of org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry in project bboxdb by jnidzwetzki.
the class TestRTreeMemoryDeserializer method testSerializeIndex1D.
/**
* Test the encoding and the decoding of the index
* @throws StorageManagerException
* @throws IOException
* @throws InterruptedException
*/
@Test(timeout = 60000)
public void testSerializeIndex1D() throws StorageManagerException, IOException, InterruptedException {
final List<SpatialIndexEntry> tupleList = RTreeTestHelper.generateRandomTupleList(1);
final SpatialIndexBuilder index = new RTreeBuilder();
index.bulkInsert(tupleList);
RTreeTestHelper.queryIndex(tupleList, index);
final File tempFile = File.createTempFile("rtree-", "-test");
tempFile.deleteOnExit();
final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
index.writeToFile(raf);
raf.close();
final AbstractRTreeReader indexRead = getRTreeReader();
final RandomAccessFile rafRead = new RandomAccessFile(tempFile, "r");
indexRead.readFromFile(rafRead);
rafRead.close();
RTreeTestHelper.queryIndex(tupleList, indexRead);
}
use of org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry in project bboxdb by jnidzwetzki.
the class DirectoryNode method initFromByteBuffer.
/**
* Read the node from byte buffer
* @param memory
* @param maxNodeSize
* @throws IOException
*/
public void initFromByteBuffer(final MappedByteBuffer memory, final int maxNodeSize) throws IOException {
nodeId = memory.getInt();
// Bounding box data
final int boundingBoxLength = memory.getInt();
final byte[] boundingBoxBytes = new byte[boundingBoxLength];
memory.get(boundingBoxBytes, 0, boundingBoxBytes.length);
boundingBox = BoundingBox.fromByteArray(boundingBoxBytes);
final byte[] followingByte = new byte[RTreeBuilder.MAGIC_VALUE_SIZE];
// Read index entries
for (int i = 0; i < maxNodeSize; i++) {
memory.get(followingByte, 0, followingByte.length);
if (Arrays.equals(followingByte, RTreeBuilder.MAGIC_CHILD_NODE_FOLLOWING)) {
final SpatialIndexEntry spatialIndexEntry = SpatialIndexEntry.readFromByteBuffer(memory);
indexEntries.add(spatialIndexEntry);
} else if (!Arrays.equals(followingByte, RTreeBuilder.MAGIC_CHILD_NODE_NOT_EXISTING)) {
throw new IllegalArgumentException("Unknown node type following: " + followingByte);
}
}
// Read pointer positions
for (int i = 0; i < maxNodeSize; i++) {
memory.get(followingByte, 0, followingByte.length);
if (!Arrays.equals(followingByte, RTreeBuilder.MAGIC_CHILD_NODE_NOT_EXISTING)) {
final int childPointer = DataEncoderHelper.readIntFromByte(followingByte);
assert (childPointer > 0) : "Child pointer needs to be > 0 " + childPointer;
childNodes.add(childPointer);
}
}
}
use of org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry in project bboxdb by jnidzwetzki.
the class RTreeBuilder method distributeLeafData.
/**
* Distribute the index data
* @param nodeToSplit
* @param newNode1
* @param newNode2
*/
protected void distributeLeafData(final RTreeDirectoryNode nodeToSplit, final RTreeDirectoryNode newNode1, final RTreeDirectoryNode newNode2) {
final List<SpatialIndexEntry> dataToDistribute = nodeToSplit.getIndexEntries();
final QuadraticSeedPicker<SpatialIndexEntry> seedPicker = new QuadraticSeedPicker<>();
final Pair<SpatialIndexEntry, SpatialIndexEntry> seeds = seedPicker.quadraticPickSeeds(dataToDistribute);
insert(newNode1, seeds.getElement1());
insert(newNode2, seeds.getElement2());
for (int i = 0; i < dataToDistribute.size(); i++) {
newNode1.updateBoundingBox();
newNode2.updateBoundingBox();
final int remainingObjects = dataToDistribute.size() - i;
final SpatialIndexEntry entry = dataToDistribute.get(i);
if (newNode1.getIndexEntries().size() + remainingObjects <= maxNodeSize / 2) {
insert(newNode1, entry);
continue;
}
if (newNode2.getIndexEntries().size() + remainingObjects <= maxNodeSize / 2) {
insert(newNode2, entry);
continue;
}
final double node1Enlargement = newNode1.getBoundingBox().calculateEnlargement(entry.getBoundingBox());
final double node2Enlargement = newNode2.getBoundingBox().calculateEnlargement(entry.getBoundingBox());
if (node1Enlargement == node2Enlargement) {
if (newNode1.getIndexEntries().size() < newNode2.getIndexEntries().size()) {
insert(newNode1, entry);
continue;
} else {
insert(newNode2, entry);
continue;
}
}
if (node1Enlargement < node2Enlargement) {
insert(newNode1, entry);
continue;
} else {
insert(newNode2, entry);
continue;
}
}
}
use of org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry in project bboxdb by jnidzwetzki.
the class RTreeDirectoryNode method testCovering.
/**
* Test the bounding box covering (useful for test purposes)
*/
public void testCovering() {
boolean success = true;
for (final SpatialIndexEntry entry : indexEntries) {
if (!boundingBox.isCovering(entry.getBoundingBox())) {
System.err.println("Error 1");
success = false;
}
if (!boundingBox.overlaps(entry.getBoundingBox())) {
System.err.println("Error 2");
success = false;
}
}
for (final RTreeDirectoryNode entry : directoryNodeChilds) {
assert boundingBox != null;
assert entry.getBoundingBox() != null : "Null BBox: " + entry;
if (!boundingBox.isCovering(entry.getBoundingBox())) {
System.err.println("Error 3a: " + boundingBox + " does not cover" + entry.getBoundingBox());
entry.updateBoundingBox();
updateBoundingBox();
if (!boundingBox.isCovering(entry.getBoundingBox())) {
System.err.println("Error 3b: " + boundingBox + " does not cover" + entry.getBoundingBox());
System.err.println(getAllChildBoundingBoxes());
success = false;
}
}
if (!boundingBox.overlaps(entry.getBoundingBox())) {
System.err.println("Error 4");
success = false;
}
entry.testCovering();
}
if (!success) {
throw new RuntimeException();
}
}
Aggregations