Search in sources :

Example 1 with MVRTreeMap

use of org.h2.mvstore.rtree.MVRTreeMap in project h2database by h2database.

the class TestMVRTree method testExample.

private void testExample() {
    // create an in-memory store
    MVStore s = MVStore.open(null);
    // open an R-tree map
    MVRTreeMap<String> r = s.openMap("data", new MVRTreeMap.Builder<String>());
    // add two key-value pairs
    // the first value is the key id (to make the key unique)
    // then the min x, max x, min y, max y
    r.add(new SpatialKey(0, -3f, -2f, 2f, 3f), "left");
    r.add(new SpatialKey(1, 3f, 4f, 4f, 5f), "right");
    // iterate over the intersecting keys
    Iterator<SpatialKey> it = r.findIntersectingKeys(new SpatialKey(0, 0f, 9f, 3f, 6f));
    for (SpatialKey k; it.hasNext(); ) {
        k = it.next();
        // System.out.println(k + ": " + r.get(k));
        assertNotNull(k);
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) SpatialKey(org.h2.mvstore.rtree.SpatialKey) MVRTreeMap(org.h2.mvstore.rtree.MVRTreeMap)

Example 2 with MVRTreeMap

use of org.h2.mvstore.rtree.MVRTreeMap in project h2database by h2database.

the class TestMVRTree method testRandom.

private void testRandom(boolean quadraticSplit) {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s = openStore(fileName);
    MVRTreeMap<String> m = s.openMap("data", new MVRTreeMap.Builder<String>());
    m.setQuadraticSplit(quadraticSplit);
    HashMap<SpatialKey, String> map = new HashMap<>();
    Random rand = new Random(1);
    int operationCount = 10000;
    int maxValue = 300;
    for (int i = 0; i < operationCount; i++) {
        int key = rand.nextInt(maxValue);
        Random rk = new Random(key);
        float x = rk.nextFloat(), y = rk.nextFloat();
        float p = (float) (rk.nextFloat() * 0.000001);
        SpatialKey k = new SpatialKey(key, x - p, x + p, y - p, y + p);
        String v = "" + rand.nextInt();
        Iterator<SpatialKey> it;
        switch(rand.nextInt(5)) {
            case 0:
                log(i + ": put " + k + " = " + v + " " + m.size());
                m.put(k, v);
                map.put(k, v);
                break;
            case 1:
                log(i + ": remove " + k + " " + m.size());
                m.remove(k);
                map.remove(k);
                break;
            case 2:
                {
                    p = (float) (rk.nextFloat() * 0.01);
                    k = new SpatialKey(key, x - p, x + p, y - p, y + p);
                    it = m.findIntersectingKeys(k);
                    while (it.hasNext()) {
                        SpatialKey n = it.next();
                        String a = map.get(n);
                        assertNotNull(a);
                    }
                    break;
                }
            case 3:
                {
                    p = (float) (rk.nextFloat() * 0.01);
                    k = new SpatialKey(key, x - p, x + p, y - p, y + p);
                    it = m.findContainedKeys(k);
                    while (it.hasNext()) {
                        SpatialKey n = it.next();
                        String a = map.get(n);
                        assertNotNull(a);
                    }
                    break;
                }
            default:
                String a = map.get(k);
                String b = m.get(k);
                if (a == null || b == null) {
                    assertTrue(a == b);
                } else {
                    assertEquals(a, b);
                }
                break;
        }
        assertEquals(map.size(), m.size());
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) SpatialKey(org.h2.mvstore.rtree.SpatialKey) MVRTreeMap(org.h2.mvstore.rtree.MVRTreeMap) Random(java.util.Random) HashMap(java.util.HashMap)

Example 3 with MVRTreeMap

use of org.h2.mvstore.rtree.MVRTreeMap in project h2database by h2database.

the class TestMVRTree method testRandomInsert.

private void testRandomInsert() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s;
    s = new MVStore.Builder().fileName(fileName).pageSplitSize(100).open();
    MVRTreeMap<String> map = s.openMap("data", new MVRTreeMap.Builder<String>());
    Random r = new Random(1);
    for (int i = 0; i < 1000; i++) {
        if (i % 100 == 0) {
            r.setSeed(1);
        }
        float x = r.nextFloat() * 50, y = r.nextFloat() * 50;
        SpatialKey k = new SpatialKey(i % 100, x, x + 2, y, y + 1);
        map.put(k, "i:" + i);
        if (i % 10 == 0) {
            s.commit();
        }
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) SpatialKey(org.h2.mvstore.rtree.SpatialKey) MVRTreeMap(org.h2.mvstore.rtree.MVRTreeMap) Random(java.util.Random)

Example 4 with MVRTreeMap

use of org.h2.mvstore.rtree.MVRTreeMap in project h2database by h2database.

the class TestMVStoreTool method testCompact.

private void testCompact() {
    String fileName = getBaseDir() + "/testCompact.h3";
    FileUtils.createDirectories(getBaseDir());
    FileUtils.delete(fileName);
    // store with a very small page size, to make sure
    // there are many leaf pages
    MVStore s = new MVStore.Builder().pageSplitSize(1000).fileName(fileName).autoCommitDisabled().open();
    MVMap<Integer, String> map = s.openMap("data");
    for (int i = 0; i < 10; i++) {
        map.put(i, "Hello World " + i * 10);
        if (i % 3 == 0) {
            s.commit();
        }
    }
    for (int i = 0; i < 20; i++) {
        map = s.openMap("data" + i);
        for (int j = 0; j < i * i; j++) {
            map.put(j, "Hello World " + j * 10);
        }
        s.commit();
    }
    MVRTreeMap<String> rTreeMap = s.openMap("rtree", new MVRTreeMap.Builder<String>());
    Random r = new Random(1);
    for (int i = 0; i < 10; i++) {
        float x = r.nextFloat();
        float y = r.nextFloat();
        float width = r.nextFloat() / 10;
        float height = r.nextFloat() / 10;
        SpatialKey k = new SpatialKey(i, x, x + width, y, y + height);
        rTreeMap.put(k, "Hello World " + i * 10);
        if (i % 3 == 0) {
            s.commit();
        }
    }
    s.close();
    MVStoreTool.compact(fileName, fileName + ".new", false);
    MVStoreTool.compact(fileName, fileName + ".new.compress", true);
    MVStore s1 = new MVStore.Builder().fileName(fileName).readOnly().open();
    MVStore s2 = new MVStore.Builder().fileName(fileName + ".new").readOnly().open();
    MVStore s3 = new MVStore.Builder().fileName(fileName + ".new.compress").readOnly().open();
    assertEquals(s1, s2);
    assertEquals(s1, s3);
    s1.close();
    s2.close();
    s3.close();
    long size1 = FileUtils.size(fileName);
    long size2 = FileUtils.size(fileName + ".new");
    long size3 = FileUtils.size(fileName + ".new.compress");
    assertTrue("size1: " + size1 + " size2: " + size2 + " size3: " + size3, size2 < size1 && size3 < size2);
    MVStoreTool.compact(fileName, false);
    assertEquals(size2, FileUtils.size(fileName));
    MVStoreTool.compact(fileName, true);
    assertEquals(size3, FileUtils.size(fileName));
}
Also used : SpatialKey(org.h2.mvstore.rtree.SpatialKey) MVStore(org.h2.mvstore.MVStore) MVRTreeMap(org.h2.mvstore.rtree.MVRTreeMap) Random(java.util.Random)

Example 5 with MVRTreeMap

use of org.h2.mvstore.rtree.MVRTreeMap in project ignite by apache.

the class GridH2SpatialIndex method findFirstOrLast.

/**
 * {@inheritDoc}
 */
@Override
public Cursor findFirstOrLast(Session ses, boolean first) {
    Lock l = lock.readLock();
    l.lock();
    try {
        checkClosed();
        if (!first)
            throw DbException.throwInternalError("Spatial Index can only be fetch by ascending order");
        final int seg = threadLocalSegment();
        final MVRTreeMap<Long> segment = segments[seg];
        GridCursor<GridH2Row> iter = rowIterator(segment.keySet().iterator(), null);
        return new SingleRowCursor(iter.next() ? iter.get() : null);
    } catch (IgniteCheckedException e) {
        throw DbException.convert(e);
    } finally {
        l.unlock();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SingleRowCursor(org.h2.index.SingleRowCursor) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

SpatialKey (org.h2.mvstore.rtree.SpatialKey)9 MVStore (org.h2.mvstore.MVStore)8 MVRTreeMap (org.h2.mvstore.rtree.MVRTreeMap)8 Random (java.util.Random)6 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 ImageWriter (javax.imageio.ImageWriter)1 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 SingleRowCursor (org.h2.index.SingleRowCursor)1