Search in sources :

Example 36 with Call

use of org.h2.command.dml.Call 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 37 with Call

use of org.h2.command.dml.Call 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)

Example 38 with Call

use of org.h2.command.dml.Call 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 39 with Call

use of org.h2.command.dml.Call 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 40 with Call

use of org.h2.command.dml.Call 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)

Aggregations

Task (org.h2.util.Task)69 Connection (java.sql.Connection)68 Statement (java.sql.Statement)64 PreparedStatement (java.sql.PreparedStatement)60 ResultSet (java.sql.ResultSet)48 SQLException (java.sql.SQLException)42 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 SimpleResultSet (org.h2.tools.SimpleResultSet)24 MVStore (org.h2.mvstore.MVStore)20 Random (java.util.Random)19 JdbcConnection (org.h2.jdbc.JdbcConnection)19 CallableStatement (java.sql.CallableStatement)14 DbException (org.h2.message.DbException)13 IOException (java.io.IOException)10 JdbcSQLException (org.h2.jdbc.JdbcSQLException)7 ArrayList (java.util.ArrayList)6 Expression (org.h2.expression.Expression)6 ValueString (org.h2.value.ValueString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStream (java.io.OutputStream)4