Search in sources :

Example 16 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class TestFileLockSerialized method testPendingWrite.

private void testPendingWrite() throws Exception {
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
    String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;WRITE_DELAY=0";
    Connection conn = getConnection(writeUrl, "sa", "sa");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key)");
    Thread.sleep(100);
    String propFile = getBaseDir() + "/fileLockSerialized.lock.db";
    SortedProperties p = SortedProperties.loadProperties(propFile);
    p.setProperty("changePending", "true");
    p.setProperty("modificationDataId", "1000");
    try (OutputStream out = FileUtils.newOutputStream(propFile, false)) {
        p.store(out, "test");
    }
    Thread.sleep(100);
    stat.execute("select * from test");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) OutputStream(java.io.OutputStream) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) SortedProperties(org.h2.util.SortedProperties)

Example 17 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class TestAll method addTest.

private void addTest(TestBase test) {
    // tests.add(test);
    // run directly for now, because concurrently running tests
    // fails on Raspberry Pi quite often (seems to be a JVM problem)
    // event queue watchdog for tests that get stuck when running in Jenkins
    final java.util.Timer watchdog = new java.util.Timer();
    // 5 minutes
    watchdog.schedule(new TimerTask() {

        @Override
        public void run() {
            ThreadDeadlockDetector.dumpAllThreadsAndLocks("test watchdog timed out");
        }
    }, 5 * 60 * 1000);
    try {
        test.runTest(this);
    } finally {
        watchdog.cancel();
    }
}
Also used : TestTimer(org.h2.test.synth.TestTimer) TimerTask(java.util.TimerTask)

Example 18 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out 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 19 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class TestBinaryArithmeticStream method testHuffmanRandomized.

private void testHuffmanRandomized() throws IOException {
    Random r = new Random(1);
    int[] freq = new int[r.nextInt(200) + 1];
    for (int i = 0; i < freq.length; i++) {
        freq[i] = r.nextInt(1000) + 1;
    }
    int seed = r.nextInt();
    r.setSeed(seed);
    Huffman huff = new Huffman(freq);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    Out out = new Out(byteOut);
    for (int i = 0; i < 10000; i++) {
        huff.write(out, r.nextInt(freq.length));
    }
    out.flush();
    In in = new In(new ByteArrayInputStream(byteOut.toByteArray()));
    r.setSeed(seed);
    for (int i = 0; i < 10000; i++) {
        int expected = r.nextInt(freq.length);
        int got = huff.read(in);
        assertEquals(expected, got);
    }
}
Also used : Random(java.util.Random) In(org.h2.dev.util.BinaryArithmeticStream.In) ByteArrayInputStream(java.io.ByteArrayInputStream) Huffman(org.h2.dev.util.BinaryArithmeticStream.Huffman) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Out(org.h2.dev.util.BinaryArithmeticStream.Out)

Example 20 with Out

use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.

the class TestBinaryArithmeticStream method testCompressionRatio.

private void testCompressionRatio() throws IOException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    Out out = new Out(byteOut);
    int prob = 1000;
    int len = 1024;
    for (int i = 0; i < len; i++) {
        out.writeBit(true, prob);
    }
    out.flush();
    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    In in = new In(byteIn);
    for (int i = 0; i < len; i++) {
        assertTrue(in.readBit(prob));
    }
// System.out.println(len / 8 + " comp: " +
// byteOut.toByteArray().length);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) In(org.h2.dev.util.BinaryArithmeticStream.In) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Out(org.h2.dev.util.BinaryArithmeticStream.Out)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SQLException (java.sql.SQLException)17 DbException (org.h2.message.DbException)17 Random (java.util.Random)11 ResultSet (java.sql.ResultSet)10 InputStream (java.io.InputStream)9 Statement (java.sql.Statement)9 Connection (java.sql.Connection)7 PrintStream (java.io.PrintStream)6 Properties (java.util.Properties)6 Task (org.h2.util.Task)6 BufferedOutputStream (java.io.BufferedOutputStream)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 PipedInputStream (java.io.PipedInputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)4