Search in sources :

Example 31 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestConcurrent method testConcurrentDataType.

private void testConcurrentDataType() throws InterruptedException {
    final ObjectDataType type = new ObjectDataType();
    final Object[] data = new Object[] { null, -1, 1, 10, "Hello", new Object[] { new byte[] { (byte) -1, (byte) 1 }, null }, new Object[] { new byte[] { (byte) 1, (byte) -1 }, 10 }, new Object[] { new byte[] { (byte) -1, (byte) 1 }, 20L }, new Object[] { new byte[] { (byte) 1, (byte) -1 }, 5 } };
    Arrays.sort(data, new Comparator<Object>() {

        @Override
        public int compare(Object o1, Object o2) {
            return type.compare(o1, o2);
        }
    });
    Task[] tasks = new Task[2];
    for (int i = 0; i < tasks.length; i++) {
        tasks[i] = new Task() {

            @Override
            public void call() throws Exception {
                Random r = new Random();
                WriteBuffer buff = new WriteBuffer();
                while (!stop) {
                    int a = r.nextInt(data.length);
                    int b = r.nextInt(data.length);
                    int comp;
                    if (r.nextBoolean()) {
                        comp = type.compare(a, b);
                    } else {
                        comp = -type.compare(b, a);
                    }
                    buff.clear();
                    type.write(buff, a);
                    buff.clear();
                    type.write(buff, b);
                    if (a == b) {
                        assertEquals(0, comp);
                    } else {
                        assertEquals(a > b ? 1 : -1, comp);
                    }
                }
            }
        };
        tasks[i].execute();
    }
    try {
        Thread.sleep(100);
    } finally {
        for (Task t : tasks) {
            t.get();
        }
    }
}
Also used : Task(org.h2.util.Task) WriteBuffer(org.h2.mvstore.WriteBuffer) Random(java.util.Random) ObjectDataType(org.h2.mvstore.type.ObjectDataType) ConcurrentModificationException(java.util.ConcurrentModificationException)

Example 32 with Call

use of org.h2.command.dml.Call in project h2database by h2database.

the class TestConcurrent method testConcurrentChangeAndGetVersion.

private static void testConcurrentChangeAndGetVersion() throws InterruptedException {
    for (int test = 0; test < 10; test++) {
        final MVStore s = new MVStore.Builder().autoCommitDisabled().open();
        try {
            s.setVersionsToKeep(10);
            final MVMap<Integer, Integer> m = s.openMap("data");
            m.put(1, 1);
            Task task = new Task() {

                @Override
                public void call() throws Exception {
                    while (!stop) {
                        m.put(1, 1);
                        s.commit();
                    }
                }
            };
            task.execute();
            Thread.sleep(1);
            for (int i = 0; i < 10000; i++) {
                if (task.isFinished()) {
                    break;
                }
                for (int j = 0; j < 20; j++) {
                    m.put(1, 1);
                    s.commit();
                }
                s.setVersionsToKeep(15);
                long version = s.getCurrentVersion() - 1;
                try {
                    m.openVersion(version);
                } catch (IllegalArgumentException e) {
                // ignore
                }
                s.setVersionsToKeep(20);
            }
            task.get();
            s.commit();
        } finally {
            s.close();
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Task(org.h2.util.Task)

Example 33 with Call

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

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

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

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