Search in sources :

Example 16 with Trace

use of org.h2.message.Trace in project h2database by h2database.

the class TestRecovery method testRunScript.

private void testRunScript() throws SQLException {
    DeleteDbFiles.execute(getBaseDir(), "recovery", true);
    DeleteDbFiles.execute(getBaseDir(), "recovery2", true);
    org.h2.Driver.load();
    Connection conn = getConnection("recovery");
    Statement stat = conn.createStatement();
    stat.execute("create table \"Joe\"\"s Table\" as " + "select 1");
    stat.execute("create table test as " + "select * from system_range(1, 100)");
    stat.execute("create view \"TEST VIEW OF TABLE TEST\" as " + "select * from test");
    stat.execute("create table a(id int primary key) as " + "select * from system_range(1, 100)");
    stat.execute("create table b(id int references a(id)) as " + "select * from system_range(1, 100)");
    stat.execute("create table lob(c clob, b blob) as " + "select space(10000) || 'end', SECURE_RAND(10000)");
    stat.execute("create table d(d varchar) as " + "select space(10000) || 'end'");
    stat.execute("alter table a add foreign key(id) references b(id)");
    // all rows have the same value - so that SCRIPT can't re-order the rows
    stat.execute("create table e(id varchar) as " + "select space(10) from system_range(1, 1000)");
    stat.execute("create index idx_e_id on e(id)");
    conn.close();
    Recover rec = new Recover();
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    rec.setOut(new PrintStream(buff));
    rec.runTool("-dir", getBaseDir(), "-db", "recovery", "-trace");
    String out = new String(buff.toByteArray());
    assertContains(out, "Created file");
    Connection conn2 = getConnection("recovery2");
    Statement stat2 = conn2.createStatement();
    String name = "recovery.h2.sql";
    stat2.execute("runscript from '" + getBaseDir() + "/" + name + "'");
    stat2.execute("select * from test");
    conn2.close();
    conn = getConnection("recovery");
    stat = conn.createStatement();
    conn2 = getConnection("recovery2");
    stat2 = conn2.createStatement();
    assertEqualDatabases(stat, stat2);
    conn.close();
    conn2.close();
    Recover.execute(getBaseDir(), "recovery");
    deleteDb("recovery");
    deleteDb("recovery2");
    FileUtils.delete(getBaseDir() + "/recovery.h2.sql");
    String dir = getBaseDir() + "/recovery.lobs.db";
    FileUtils.deleteRecursive(dir, false);
}
Also used : PrintStream(java.io.PrintStream) Statement(java.sql.Statement) Connection(java.sql.Connection) Recover(org.h2.tools.Recover) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 17 with Trace

use of org.h2.message.Trace in project h2database by h2database.

the class TestFileSystem method testZipFileSystem.

private void testZipFileSystem(String prefix, Random r) throws IOException {
    byte[] data = new byte[r.nextInt(16 * 1024)];
    long x = r.nextLong();
    FilePath file = FilePath.get(getBaseDir() + "/fs/readonly" + x + ".zip");
    r.nextBytes(data);
    OutputStream out = file.newOutputStream(false);
    ZipOutputStream zipOut = new ZipOutputStream(out);
    ZipEntry entry = new ZipEntry("data");
    zipOut.putNextEntry(entry);
    zipOut.write(data);
    zipOut.closeEntry();
    zipOut.close();
    out.close();
    FilePath fp = FilePath.get(prefix + getBaseDir() + "/fs/readonly" + x + ".zip!data");
    FileChannel fc = fp.open("r");
    StringBuilder buff = new StringBuilder();
    try {
        int pos = 0;
        for (int i = 0; i < 100; i++) {
            trace("op " + i);
            switch(r.nextInt(5)) {
                case 0:
                    {
                        int p = r.nextInt(data.length);
                        trace("seek " + p);
                        buff.append("seek " + p + "\n");
                        fc.position(p);
                        pos = p;
                        break;
                    }
                case 1:
                    {
                        int len = r.nextInt(1000);
                        int offset = r.nextInt(100);
                        int arrayLen = len + offset;
                        len = Math.min(len, data.length - pos);
                        byte[] b1 = new byte[arrayLen];
                        byte[] b2 = new byte[arrayLen];
                        trace("readFully " + len);
                        buff.append("readFully " + len + "\n");
                        System.arraycopy(data, pos, b1, offset, len);
                        ByteBuffer byteBuff = createSlicedBuffer(b2, offset, len);
                        FileUtils.readFully(fc, byteBuff);
                        assertEquals(b1, b2);
                        pos += len;
                        break;
                    }
                case 2:
                    {
                        int len = r.nextInt(1000);
                        int offset = r.nextInt(100);
                        int arrayLen = len + offset;
                        int p = r.nextInt(data.length);
                        len = Math.min(len, data.length - p);
                        byte[] b1 = new byte[arrayLen];
                        byte[] b2 = new byte[arrayLen];
                        trace("readFully " + p + " " + len);
                        buff.append("readFully " + p + " " + len + "\n");
                        System.arraycopy(data, p, b1, offset, len);
                        ByteBuffer byteBuff = createSlicedBuffer(b2, offset, len);
                        DataUtils.readFully(fc, p, byteBuff);
                        assertEquals(b1, b2);
                        break;
                    }
                case 3:
                    {
                        trace("getFilePointer");
                        buff.append("getFilePointer\n");
                        assertEquals(pos, fc.position());
                        break;
                    }
                case 4:
                    {
                        trace("length " + data.length);
                        buff.append("length " + data.length + "\n");
                        assertEquals(data.length, fc.size());
                        break;
                    }
                default:
            }
        }
        fc.close();
        file.delete();
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Exception: " + e + "\n" + buff.toString());
    }
}
Also used : FilePath(org.h2.store.fs.FilePath) ZipOutputStream(java.util.zip.ZipOutputStream) FileChannel(java.nio.channels.FileChannel) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) ByteBuffer(java.nio.ByteBuffer)

Example 18 with Trace

use of org.h2.message.Trace in project h2database by h2database.

the class TestDataPage method testAll.

private void testAll() {
    Data page = Data.create(this, 128);
    char[] data = new char[0x10000];
    for (int i = 0; i < data.length; i++) {
        data[i] = (char) i;
    }
    String s = new String(data);
    page.checkCapacity(s.length() * 4);
    page.writeString(s);
    int len = page.length();
    assertEquals(len, Data.getStringLen(s));
    page.reset();
    assertEquals(s, page.readString());
    page.reset();
    page.writeString("H\u1111!");
    page.writeString("John\tBrack's \"how are you\" M\u1111ller");
    page.writeValue(ValueInt.get(10));
    page.writeValue(ValueString.get("test"));
    page.writeValue(ValueFloat.get(-2.25f));
    page.writeValue(ValueDouble.get(10.40));
    page.writeValue(ValueNull.INSTANCE);
    trace(new String(page.getBytes()));
    page.reset();
    trace(page.readString());
    trace(page.readString());
    trace(page.readValue().getInt());
    trace(page.readValue().getString());
    trace("" + page.readValue().getFloat());
    trace("" + page.readValue().getDouble());
    trace(page.readValue().toString());
    page.reset();
    page.writeInt(0);
    page.writeInt(Integer.MAX_VALUE);
    page.writeInt(Integer.MIN_VALUE);
    page.writeInt(1);
    page.writeInt(-1);
    page.writeInt(1234567890);
    page.writeInt(54321);
    trace(new String(page.getBytes()));
    page.reset();
    trace(page.readInt());
    trace(page.readInt());
    trace(page.readInt());
    trace(page.readInt());
    trace(page.readInt());
    trace(page.readInt());
    trace(page.readInt());
    page = null;
}
Also used : Data(org.h2.store.Data) ValueString(org.h2.value.ValueString)

Example 19 with Trace

use of org.h2.message.Trace in project h2database by h2database.

the class TestFile method doTest.

private void doTest(boolean nioMem, boolean compress) {
    int len = getSize(1000, 10000);
    Random random = new Random();
    FileStore mem = null, file = null;
    byte[] buffMem = null;
    byte[] buffFile = null;
    String prefix = nioMem ? (compress ? "nioMemLZF:" : "nioMemFS:") : (compress ? "memLZF:" : "memFS:");
    FileUtils.delete(prefix + "test");
    FileUtils.delete("~/testFile");
    for (int i = 0; i < len; i++) {
        if (buffMem == null) {
            int l = 1 + random.nextInt(1000);
            buffMem = new byte[l];
            buffFile = new byte[l];
        }
        if (file == null) {
            mem = FileStore.open(this, prefix + "test", "rw");
            file = FileStore.open(this, "~/testFile", "rw");
        }
        assertEquals(file.getFilePointer(), mem.getFilePointer());
        assertEquals(file.length(), mem.length());
        int x = random.nextInt(100);
        if ((x -= 20) < 0) {
            if (file.length() > 0) {
                long pos = random.nextInt((int) (file.length() / 16)) * 16;
                trace("seek " + pos);
                mem.seek(pos);
                file.seek(pos);
            }
        } else if ((x -= 20) < 0) {
            trace("close");
            mem.close();
            file.close();
            mem = null;
            file = null;
        } else if ((x -= 20) < 0) {
            if (buffFile.length > 16) {
                random.nextBytes(buffFile);
                System.arraycopy(buffFile, 0, buffMem, 0, buffFile.length);
                int off = random.nextInt(buffFile.length - 16);
                int l = random.nextInt((buffFile.length - off) / 16) * 16;
                trace("write " + off + " " + l);
                mem.write(buffMem, off, l);
                file.write(buffFile, off, l);
            }
        } else if ((x -= 20) < 0) {
            if (buffFile.length > 16) {
                int off = random.nextInt(buffFile.length - 16);
                int l = random.nextInt((buffFile.length - off) / 16) * 16;
                l = (int) Math.min(l, file.length() - file.getFilePointer());
                trace("read " + off + " " + l);
                Exception a = null, b = null;
                try {
                    file.readFully(buffFile, off, l);
                } catch (Exception e) {
                    a = e;
                }
                try {
                    mem.readFully(buffMem, off, l);
                } catch (Exception e) {
                    b = e;
                }
                if (a != b) {
                    if (a == null || b == null) {
                        fail("only one threw an exception");
                    }
                }
                assertEquals(buffMem, buffFile);
            }
        } else if ((x -= 10) < 0) {
            trace("reset buffers");
            buffMem = null;
            buffFile = null;
        } else {
            int l = random.nextInt(10000) * 16;
            long p = file.getFilePointer();
            file.setLength(l);
            mem.setLength(l);
            trace("setLength " + l);
            if (p > l) {
                file.seek(l);
                mem.seek(l);
            }
        }
    }
    if (mem != null) {
        mem.close();
        file.close();
    }
    FileUtils.delete(prefix + "test");
    FileUtils.delete("~/testFile");
}
Also used : FileStore(org.h2.store.FileStore) Random(java.util.Random)

Example 20 with Trace

use of org.h2.message.Trace in project h2database by h2database.

the class TestFileLockSerialized method testConcurrentReadWrite.

private void testConcurrentReadWrite() throws Exception {
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized";
    String writeUrl = url + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
    // ;TRACE_LEVEL_SYSTEM_OUT=3
    // String readUrl = writeUrl + ";ACCESS_MODE_DATA=R";
    trace(" create database");
    Class.forName("org.h2.Driver");
    Connection conn = getConnection(writeUrl, "sa", "sa");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key)");
    Connection conn3 = getConnection(writeUrl, "sa", "sa");
    PreparedStatement prep3 = conn3.prepareStatement("insert into test values(?)");
    Connection conn2 = getConnection(writeUrl, "sa", "sa");
    Statement stat2 = conn2.createStatement();
    printResult(stat2, "select * from test");
    stat2.execute("create local temporary table temp(name varchar) not persistent");
    printResult(stat2, "select * from temp");
    trace(" insert row 1");
    stat.execute("insert into test values(1)");
    trace(" insert row 2");
    prep3.setInt(1, 2);
    prep3.execute();
    printResult(stat2, "select * from test");
    printResult(stat2, "select * from temp");
    conn.close();
    conn2.close();
    conn3.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

SQLException (java.sql.SQLException)16 DbException (org.h2.message.DbException)14 Connection (java.sql.Connection)11 ResultSet (java.sql.ResultSet)10 PreparedStatement (java.sql.PreparedStatement)9 Statement (java.sql.Statement)8 IOException (java.io.IOException)7 Savepoint (java.sql.Savepoint)7 Random (java.util.Random)7 ArrayList (java.util.ArrayList)5 Properties (java.util.Properties)5 TraceSystem (org.h2.message.TraceSystem)5 Server (org.h2.tools.Server)5 ValueString (org.h2.value.ValueString)5 SQLClientInfoException (java.sql.SQLClientInfoException)4 SysProperties (org.h2.engine.SysProperties)4 JdbcConnection (org.h2.jdbc.JdbcConnection)4 SortedProperties (org.h2.util.SortedProperties)4 Value (org.h2.value.Value)4 Socket (java.net.Socket)3