Search in sources :

Example 11 with MVStore

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

the class TestConcurrent method testInterruptReopen.

private void testInterruptReopen() throws Exception {
    String fileName = "retry:nio:" + getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    final MVStore s = new MVStore.Builder().fileName(fileName).cacheSize(0).open();
    final Thread mainThread = Thread.currentThread();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                mainThread.interrupt();
                Thread.sleep(10);
            }
        }
    };
    try {
        MVMap<Integer, byte[]> map = s.openMap("data");
        task.execute();
        for (int i = 0; i < 1000 && !task.isFinished(); i++) {
            map.get(i % 1000);
            map.put(i % 1000, new byte[1024]);
            s.commit();
        }
    } finally {
        task.get();
        s.close();
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task)

Example 12 with MVStore

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

the class TestConcurrent method testConcurrentIterate.

private static void testConcurrentIterate() {
    MVStore s = new MVStore.Builder().pageSplitSize(3).open();
    s.setVersionsToKeep(100);
    final MVMap<Integer, Integer> map = s.openMap("test");
    final int len = 10;
    final Random r = new Random();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                int x = r.nextInt(len);
                if (r.nextBoolean()) {
                    map.remove(x);
                } else {
                    map.put(x, r.nextInt(100));
                }
            }
        }
    };
    task.execute();
    try {
        for (int k = 0; k < 10000; k++) {
            Iterator<Integer> it = map.keyIterator(r.nextInt(len));
            long old = s.getCurrentVersion();
            s.commit();
            while (map.getVersion() == old) {
                Thread.yield();
            }
            while (it.hasNext()) {
                it.next();
            }
        }
    } finally {
        task.get();
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) Random(java.util.Random)

Example 13 with MVStore

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

the class TestConcurrent method testConcurrentOnlineBackup.

private void testConcurrentOnlineBackup() throws Exception {
    String fileName = getBaseDir() + "/" + getTestName();
    String fileNameRestore = getBaseDir() + "/" + getTestName() + "2";
    final MVStore s = openStore(fileName);
    final MVMap<Integer, byte[]> map = s.openMap("test");
    final Random r = new Random();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                for (int i = 0; i < 10; i++) {
                    map.put(i, new byte[100 * r.nextInt(100)]);
                }
                s.commit();
                map.clear();
                s.commit();
                long len = s.getFileStore().size();
                if (len > 1024 * 1024) {
                    // slow down writing a lot
                    Thread.sleep(200);
                } else if (len > 20 * 1024) {
                    // slow down writing
                    Thread.sleep(20);
                }
            }
        }
    };
    task.execute();
    try {
        for (int i = 0; i < 10; i++) {
            // System.out.println("test " + i);
            s.setReuseSpace(false);
            OutputStream out = new BufferedOutputStream(new FileOutputStream(fileNameRestore));
            long len = s.getFileStore().size();
            copyFileSlowly(s.getFileStore().getFile(), len, out);
            out.close();
            s.setReuseSpace(true);
            MVStore s2 = openStore(fileNameRestore);
            MVMap<Integer, byte[]> test = s2.openMap("test");
            for (Integer k : test.keySet()) {
                test.get(k);
            }
            s2.close();
            // let it compact
            Thread.sleep(10);
        }
    } finally {
        task.get();
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task) Random(java.util.Random) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 14 with MVStore

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

the class TestConcurrent method testConcurrentSaveCompact.

private void testConcurrentSaveCompact() throws Exception {
    String fileName = "memFS:" + getTestName();
    FileUtils.delete(fileName);
    final MVStore s = new MVStore.Builder().fileName(fileName).cacheSize(0).open();
    try {
        s.setRetentionTime(0);
        final MVMap<Integer, Integer> dataMap = s.openMap("data");
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                int i = 0;
                while (!stop) {
                    s.compact(100, 1024 * 1024);
                    dataMap.put(i % 1000, i * 10);
                    s.commit();
                    i++;
                }
            }
        };
        task.execute();
        for (int i = 0; i < 1000 && !task.isFinished(); i++) {
            s.compact(100, 1024 * 1024);
            dataMap.put(i % 1000, i * 10);
            s.commit();
        }
        task.get();
    } finally {
        s.close();
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task)

Example 15 with MVStore

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

the class TestConcurrent method testConcurrentAutoCommitAndChange.

private void testConcurrentAutoCommitAndChange() throws InterruptedException {
    String fileName = "memFS:" + getTestName();
    FileUtils.delete(fileName);
    final MVStore s = new MVStore.Builder().fileName(fileName).pageSplitSize(1000).open();
    try {
        s.setRetentionTime(1000);
        s.setAutoCommitDelay(1);
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                while (!stop) {
                    s.compact(100, 1024 * 1024);
                }
            }
        };
        final MVMap<Integer, Integer> dataMap = s.openMap("data");
        final MVMap<Integer, Integer> dataSmallMap = s.openMap("dataSmall");
        s.openMap("emptyMap");
        final AtomicInteger counter = new AtomicInteger();
        Task task2 = new Task() {

            @Override
            public void call() throws Exception {
                while (!stop) {
                    int i = counter.getAndIncrement();
                    dataMap.put(i, i * 10);
                    dataSmallMap.put(i % 100, i * 10);
                    if (i % 100 == 0) {
                        dataSmallMap.clear();
                    }
                }
            }
        };
        task.execute();
        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));
            map.put(0, i);
            s.commit();
        }
        task.get();
        task2.get();
        for (int i = 0; i < counter.get(); i++) {
            assertEquals(10 * i, dataMap.get(i).intValue());
        }
    } 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)

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