Search in sources :

Example 21 with SpatialIndexEntry

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);
}
Also used : RTreeBuilder(org.bboxdb.storage.sstable.spatialindex.rtree.RTreeBuilder) RandomAccessFile(java.io.RandomAccessFile) AbstractRTreeReader(org.bboxdb.storage.sstable.spatialindex.rtree.AbstractRTreeReader) SpatialIndexBuilder(org.bboxdb.storage.sstable.spatialindex.SpatialIndexBuilder) SpatialIndexEntry(org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 22 with SpatialIndexEntry

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);
}
Also used : RTreeBuilder(org.bboxdb.storage.sstable.spatialindex.rtree.RTreeBuilder) RandomAccessFile(java.io.RandomAccessFile) AbstractRTreeReader(org.bboxdb.storage.sstable.spatialindex.rtree.AbstractRTreeReader) SpatialIndexBuilder(org.bboxdb.storage.sstable.spatialindex.SpatialIndexBuilder) SpatialIndexEntry(org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 23 with SpatialIndexEntry

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);
        }
    }
}
Also used : SpatialIndexEntry(org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry)

Example 24 with SpatialIndexEntry

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;
        }
    }
}
Also used : SpatialIndexEntry(org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry)

Example 25 with SpatialIndexEntry

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();
    }
}
Also used : SpatialIndexEntry(org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry)

Aggregations

SpatialIndexEntry (org.bboxdb.storage.sstable.spatialindex.SpatialIndexEntry)27 Test (org.junit.Test)14 SpatialIndexBuilder (org.bboxdb.storage.sstable.spatialindex.SpatialIndexBuilder)13 RTreeBuilder (org.bboxdb.storage.sstable.spatialindex.rtree.RTreeBuilder)12 BoundingBox (org.bboxdb.commons.math.BoundingBox)8 RandomAccessFile (java.io.RandomAccessFile)6 ArrayList (java.util.ArrayList)6 StorageManagerException (org.bboxdb.storage.StorageManagerException)6 File (java.io.File)5 AbstractRTreeReader (org.bboxdb.storage.sstable.spatialindex.rtree.AbstractRTreeReader)4 List (java.util.List)3 Random (java.util.Random)3 Collectors (java.util.stream.Collectors)3 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 SpatialIndexReader (org.bboxdb.storage.sstable.spatialindex.SpatialIndexReader)2 Assert (org.junit.Assert)2 ByteBuffer (java.nio.ByteBuffer)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1