Search in sources :

Example 6 with MVRTreeMap

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

the class TestMVRTree method testRemoveAll.

private void testRemoveAll() {
    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++) {
        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);
    }
    s.commit();
    map.clear();
    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 7 with MVRTreeMap

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

the class TestMVRTree method testRandomFind.

private void testRandomFind() {
    MVStore s = openStore(null);
    MVRTreeMap<Integer> m = s.openMap("data", new MVRTreeMap.Builder<Integer>());
    int max = 100;
    for (int x = 0; x < max; x++) {
        for (int y = 0; y < max; y++) {
            int id = x * max + y;
            SpatialKey k = new SpatialKey(id, x, x, y, y);
            m.put(k, id);
        }
    }
    Random rand = new Random(1);
    int operationCount = 1000;
    for (int i = 0; i < operationCount; i++) {
        int x1 = rand.nextInt(max), y1 = rand.nextInt(10);
        int x2 = rand.nextInt(10), y2 = rand.nextInt(10);
        int intersecting = Math.max(0, x2 - x1 + 1) * Math.max(0, y2 - y1 + 1);
        int contained = Math.max(0, x2 - x1 - 1) * Math.max(0, y2 - y1 - 1);
        SpatialKey k = new SpatialKey(0, x1, x2, y1, y2);
        Iterator<SpatialKey> it = m.findContainedKeys(k);
        int count = 0;
        while (it.hasNext()) {
            SpatialKey t = it.next();
            assertTrue(t.min(0) > x1);
            assertTrue(t.min(1) > y1);
            assertTrue(t.max(0) < x2);
            assertTrue(t.max(1) < y2);
            count++;
        }
        assertEquals(contained, count);
        it = m.findIntersectingKeys(k);
        count = 0;
        while (it.hasNext()) {
            SpatialKey t = it.next();
            assertTrue(t.min(0) >= x1);
            assertTrue(t.min(1) >= y1);
            assertTrue(t.max(0) <= x2);
            assertTrue(t.max(1) <= y2);
            count++;
        }
        assertEquals(intersecting, count);
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) SpatialKey(org.h2.mvstore.rtree.SpatialKey) MVRTreeMap(org.h2.mvstore.rtree.MVRTreeMap) Random(java.util.Random)

Example 8 with MVRTreeMap

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

the class TestMVRTree method testMany.

private void testMany() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s;
    s = openStore(fileName);
    // s.setMaxPageSize(50);
    MVRTreeMap<String> r = s.openMap("data", new MVRTreeMap.Builder<String>().dimensions(2).valueType(StringDataType.INSTANCE));
    // r.setQuadraticSplit(true);
    Random rand = new Random(1);
    int len = 1000;
    // prof.startCollecting();
    for (int i = 0; i < len; i++) {
        float x = rand.nextFloat(), y = rand.nextFloat();
        float p = (float) (rand.nextFloat() * 0.000001);
        SpatialKey k = new SpatialKey(i, x - p, x + p, y - p, y + p);
        r.add(k, "" + i);
        if (i > 0 && (i % len / 10) == 0) {
            s.commit();
        }
        if (i > 0 && (i % 10000) == 0) {
            render(r, getBaseDir() + "/test.png");
        }
    }
    s.close();
    s = openStore(fileName);
    r = s.openMap("data", new MVRTreeMap.Builder<String>().dimensions(2).valueType(StringDataType.INSTANCE));
    rand = new Random(1);
    for (int i = 0; i < len; i++) {
        float x = rand.nextFloat(), y = rand.nextFloat();
        float p = (float) (rand.nextFloat() * 0.000001);
        SpatialKey k = new SpatialKey(i, x - p, x + p, y - p, y + p);
        assertEquals("" + i, r.get(k));
    }
    assertEquals(len, r.size());
    int count = 0;
    for (SpatialKey k : r.keySet()) {
        assertNotNull(r.get(k));
        count++;
    }
    assertEquals(len, count);
    rand = new Random(1);
    for (int i = 0; i < len; i++) {
        float x = rand.nextFloat(), y = rand.nextFloat();
        float p = (float) (rand.nextFloat() * 0.000001);
        SpatialKey k = new SpatialKey(i, x - p, x + p, y - p, y + p);
        r.remove(k);
    }
    assertEquals(0, r.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)

Example 9 with MVRTreeMap

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

the class TestMVRTree method render.

private static void render(MVRTreeMap<String> r, String fileName) {
    int width = 1000, height = 500;
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = (Graphics2D) img.getGraphics();
    g2d.setBackground(Color.WHITE);
    g2d.setColor(Color.WHITE);
    g2d.fillRect(0, 0, width, height);
    g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(Color.BLACK);
    SpatialKey b = new SpatialKey(0, Float.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE, Float.MIN_VALUE);
    for (SpatialKey x : r.keySet()) {
        b.setMin(0, Math.min(b.min(0), x.min(0)));
        b.setMin(1, Math.min(b.min(1), x.min(1)));
        b.setMax(0, Math.max(b.max(0), x.max(0)));
        b.setMax(1, Math.max(b.max(1), x.max(1)));
    }
    // System.out.println(b);
    for (SpatialKey x : r.keySet()) {
        int[] rect = scale(b, x, width, height);
        g2d.drawRect(rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1]);
        String s = r.get(x);
        g2d.drawChars(s.toCharArray(), 0, s.length(), rect[0], rect[1] - 4);
    }
    g2d.setColor(Color.red);
    ArrayList<SpatialKey> list = New.arrayList();
    r.addNodeKeys(list, r.getRoot());
    for (SpatialKey x : list) {
        int[] rect = scale(b, x, width, height);
        g2d.drawRect(rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1]);
    }
    ImageWriter out = ImageIO.getImageWritersByFormatName("png").next();
    try {
        out.setOutput(new FileImageOutputStream(new File(fileName)));
        out.write(img);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SpatialKey(org.h2.mvstore.rtree.SpatialKey) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 10 with MVRTreeMap

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

the class TestMVRTree method testSimple.

private void testSimple() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s;
    s = openStore(fileName);
    MVRTreeMap<String> r = s.openMap("data", new MVRTreeMap.Builder<String>().dimensions(2).valueType(StringDataType.INSTANCE));
    add(r, "Bern", key(0, 46.57, 7.27, 124381));
    add(r, "Basel", key(1, 47.34, 7.36, 170903));
    add(r, "Zurich", key(2, 47.22, 8.33, 376008));
    add(r, "Lucerne", key(3, 47.03, 8.18, 77491));
    add(r, "Geneva", key(4, 46.12, 6.09, 191803));
    add(r, "Lausanne", key(5, 46.31, 6.38, 127821));
    add(r, "Winterthur", key(6, 47.30, 8.45, 102966));
    add(r, "St. Gallen", key(7, 47.25, 9.22, 73500));
    add(r, "Biel/Bienne", key(8, 47.08, 7.15, 51203));
    add(r, "Lugano", key(9, 46.00, 8.57, 54667));
    add(r, "Thun", key(10, 46.46, 7.38, 42623));
    add(r, "Bellinzona", key(11, 46.12, 9.01, 17373));
    add(r, "Chur", key(12, 46.51, 9.32, 33756));
    // render(r, getBaseDir() + "/test.png");
    ArrayList<String> list = New.arrayList();
    for (SpatialKey x : r.keySet()) {
        list.add(r.get(x));
    }
    Collections.sort(list);
    assertEquals("[Basel, Bellinzona, Bern, Biel/Bienne, Chur, Geneva, " + "Lausanne, Lucerne, Lugano, St. Gallen, Thun, Winterthur, Zurich]", list.toString());
    SpatialKey k;
    // intersection
    list.clear();
    k = key(0, 47.34, 7.36, 0);
    for (Iterator<SpatialKey> it = r.findIntersectingKeys(k); it.hasNext(); ) {
        list.add(r.get(it.next()));
    }
    Collections.sort(list);
    assertEquals("[Basel]", list.toString());
    // contains
    list.clear();
    k = key(0, 47.34, 7.36, 0);
    for (Iterator<SpatialKey> it = r.findContainedKeys(k); it.hasNext(); ) {
        list.add(r.get(it.next()));
    }
    assertEquals(0, list.size());
    k = key(0, 47.34, 7.36, 171000);
    for (Iterator<SpatialKey> it = r.findContainedKeys(k); it.hasNext(); ) {
        list.add(r.get(it.next()));
    }
    assertEquals("[Basel]", list.toString());
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) SpatialKey(org.h2.mvstore.rtree.SpatialKey) MVRTreeMap(org.h2.mvstore.rtree.MVRTreeMap)

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