Search in sources :

Example 16 with MVStore

use of org.h2.mvstore.MVStore in project h2database by h2database.

the class TestConcurrent method testConcurrentReplaceAndRead.

private void testConcurrentReplaceAndRead() throws InterruptedException {
    final MVStore s = new MVStore.Builder().open();
    final MVMap<Integer, Integer> map = s.openMap("data");
    for (int i = 0; i < 100; i++) {
        map.put(i, i % 100);
    }
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            int i = 0;
            while (!stop) {
                map.put(i % 100, i % 100);
                i++;
                if (i % 1000 == 0) {
                    s.commit();
                }
            }
        }
    };
    task.execute();
    try {
        Thread.sleep(1);
        for (int i = 0; !task.isFinished() && i < 1000000; i++) {
            assertEquals(i % 100, map.get(i % 100).intValue());
        }
    } finally {
        task.get();
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task)

Example 17 with MVStore

use of org.h2.mvstore.MVStore in project h2database by h2database.

the class TestConcurrent method testConcurrentChangeAndCompact.

private void testConcurrentChangeAndCompact() throws InterruptedException {
    String fileName = "memFS:" + getTestName();
    FileUtils.delete(fileName);
    final MVStore s = new MVStore.Builder().fileName(fileName).pageSplitSize(10).autoCommitDisabled().open();
    s.setRetentionTime(10000);
    try {
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                while (!stop) {
                    s.compact(100, 1024 * 1024);
                }
            }
        };
        task.execute();
        Task task2 = new Task() {

            @Override
            public void call() throws Exception {
                while (!stop) {
                    s.compact(100, 1024 * 1024);
                }
            }
        };
        task2.execute();
        Thread.sleep(1);
        for (int i = 0; !task.isFinished() && !task2.isFinished() && i < 1000; i++) {
            MVMap<Integer, Integer> map = s.openMap("d" + (i % 3));
            // MVMap<Integer, Integer> map = s.openMap("d" + (i % 3),
            // new MVMapConcurrent.Builder<Integer, Integer>());
            map.put(0, i);
            map.get(0);
            s.commit();
        }
        task.get();
        task2.get();
    } finally {
        s.close();
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task)

Example 18 with MVStore

use of org.h2.mvstore.MVStore in project h2database by h2database.

the class TestConcurrent method testConcurrentFree.

private void testConcurrentFree() throws InterruptedException {
    String fileName = "memFS:" + getTestName();
    for (int test = 0; test < 10; test++) {
        FileUtils.delete(fileName);
        final MVStore s1 = new MVStore.Builder().fileName(fileName).autoCommitDisabled().open();
        s1.setRetentionTime(0);
        final int count = 200;
        for (int i = 0; i < count; i++) {
            MVMap<Integer, Integer> m = s1.openMap("d" + i);
            m.put(1, 1);
            if (i % 2 == 0) {
                s1.commit();
            }
        }
        s1.close();
        final MVStore s = new MVStore.Builder().fileName(fileName).autoCommitDisabled().open();
        try {
            s.setRetentionTime(0);
            final ArrayList<MVMap<Integer, Integer>> list = New.arrayList();
            for (int i = 0; i < count; i++) {
                MVMap<Integer, Integer> m = s.openMap("d" + i);
                list.add(m);
            }
            final AtomicInteger counter = new AtomicInteger();
            Task task = new Task() {

                @Override
                public void call() throws Exception {
                    while (!stop) {
                        int x = counter.getAndIncrement();
                        if (x >= count) {
                            break;
                        }
                        MVMap<Integer, Integer> m = list.get(x);
                        m.clear();
                        s.removeMap(m);
                    }
                }
            };
            task.execute();
            Thread.sleep(1);
            while (true) {
                int x = counter.getAndIncrement();
                if (x >= count) {
                    break;
                }
                MVMap<Integer, Integer> m = list.get(x);
                m.clear();
                s.removeMap(m);
                if (x % 5 == 0) {
                    s.commit();
                }
            }
            task.get();
            // this will mark old chunks as unused,
            // but not remove (and overwrite) them yet
            s.commit();
            // this will remove them, so we end up with
            // one unused one, and one active one
            MVMap<Integer, Integer> m = s.openMap("dummy");
            m.put(1, 1);
            s.commit();
            m.put(2, 2);
            s.commit();
            MVMap<String, String> meta = s.getMetaMap();
            int chunkCount = 0;
            for (String k : meta.keyList()) {
                if (k.startsWith("chunk.")) {
                    chunkCount++;
                }
            }
            assertTrue("" + chunkCount, chunkCount < 3);
        } finally {
            s.close();
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MVMap(org.h2.mvstore.MVMap)

Example 19 with MVStore

use of org.h2.mvstore.MVStore 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 20 with MVStore

use of org.h2.mvstore.MVStore 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)

Aggregations

MVStore (org.h2.mvstore.MVStore)123 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)69 Random (java.util.Random)23 Task (org.h2.util.Task)20 TransactionStore (org.h2.mvstore.db.TransactionStore)19 Transaction (org.h2.mvstore.db.TransactionStore.Transaction)17 MVRTreeMap (org.h2.mvstore.rtree.MVRTreeMap)8 SpatialKey (org.h2.mvstore.rtree.SpatialKey)8 IOException (java.io.IOException)6 Connection (java.sql.Connection)5 Statement (java.sql.Statement)5 FileStore (org.h2.mvstore.FileStore)5 StreamStore (org.h2.mvstore.StreamStore)5 ArrayList (java.util.ArrayList)4 TreeMap (java.util.TreeMap)4 Store (org.h2.mvstore.db.MVTableEngine.Store)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 PreparedStatement (java.sql.PreparedStatement)3