Search in sources :

Example 96 with In

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

the class TestMVTableEngine method testBlob.

private void testBlob() throws SQLException, IOException {
    if (config.memory) {
        return;
    }
    deleteDb(getTestName());
    String dbName = getTestName() + ";MV_STORE=TRUE";
    Connection conn;
    Statement stat;
    conn = getConnection(dbName);
    stat = conn.createStatement();
    stat.execute("create table test(id int, name blob)");
    PreparedStatement prep = conn.prepareStatement("insert into test values(1, ?)");
    prep.setBinaryStream(1, new ByteArrayInputStream(new byte[129]));
    prep.execute();
    conn.close();
    conn = getConnection(dbName);
    stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("select * from test");
    while (rs.next()) {
        InputStream in = rs.getBinaryStream(2);
        int len = 0;
        while (in.read() >= 0) {
            len++;
        }
        assertEquals(129, len);
    }
    conn.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Savepoint(java.sql.Savepoint)

Example 97 with In

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

the class TestTransactionStore method testStopWhileCommitting.

private void testStopWhileCommitting() throws Exception {
    String fileName = getBaseDir() + "/testStopWhileCommitting.h3";
    FileUtils.delete(fileName);
    Random r = new Random(0);
    for (int i = 0; i < 10; ) {
        MVStore s;
        TransactionStore ts;
        Transaction tx;
        TransactionMap<Integer, String> m;
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        ts.init();
        tx = ts.begin();
        s.setReuseSpace(false);
        m = tx.openMap("test");
        final String value = "x" + i;
        for (int j = 0; j < 1000; j++) {
            m.put(j, value);
        }
        final AtomicInteger state = new AtomicInteger();
        final MVStore store = s;
        final MVMap<Integer, String> other = s.openMap("other");
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                for (int i = 0; !stop; i++) {
                    state.set(i);
                    other.put(i, value);
                    store.commit();
                }
            }
        };
        task.execute();
        // wait for the task to start
        while (state.get() < 1) {
            Thread.yield();
        }
        // commit while writing in the task
        tx.commit();
        // wait for the task to stop
        task.get();
        store.close();
        s = MVStore.open(fileName);
        // roll back a bit, until we have some undo log entries
        assertTrue(s.hasMap("undoLog"));
        for (int back = 0; back < 100; back++) {
            int minus = r.nextInt(10);
            s.rollbackTo(Math.max(0, s.getCurrentVersion() - minus));
            MVMap<?, ?> undo = s.openMap("undoLog");
            if (undo.size() > 0) {
                break;
            }
        }
        // re-open the store, because we have opened
        // the undoLog map with the wrong data type
        s.close();
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        List<Transaction> list = ts.getOpenTransactions();
        if (list.size() != 0) {
            tx = list.get(0);
            if (tx.getStatus() == Transaction.STATUS_COMMITTING) {
                i++;
            }
        }
        s.close();
        FileUtils.delete(fileName);
        assertFalse(FileUtils.exists(fileName));
    }
}
Also used : Task(org.h2.util.Task) MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Random(java.util.Random) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 98 with In

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

the class TestStreamStore method testTreeStructure.

private void testTreeStructure() throws IOException {
    final AtomicInteger reads = new AtomicInteger();
    Map<Long, byte[]> map = new HashMap<Long, byte[]>() {

        private static final long serialVersionUID = 1L;

        @Override
        public byte[] get(Object k) {
            reads.incrementAndGet();
            return super.get(k);
        }
    };
    StreamStore store = new StreamStore(map);
    store.setMinBlockSize(10);
    store.setMaxBlockSize(100);
    byte[] id = store.put(new ByteArrayInputStream(new byte[10000]));
    InputStream in = store.get(id);
    assertEquals(0, in.read(new byte[0]));
    assertEquals(0, in.read());
    assertEquals(3, reads.get());
}
Also used : StreamStore(org.h2.mvstore.StreamStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 99 with In

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

the class TestStreamStore method testFormat.

private void testFormat() throws IOException {
    Map<Long, byte[]> map = new HashMap<>();
    StreamStore store = new StreamStore(map);
    store.setMinBlockSize(10);
    store.setMaxBlockSize(20);
    store.setNextKey(123);
    byte[] id;
    id = store.put(new ByteArrayInputStream(new byte[200]));
    assertEquals(200, store.length(id));
    assertEquals("02c8018801", StringUtils.convertBytesToHex(id));
    id = store.put(new ByteArrayInputStream(new byte[0]));
    assertEquals("", StringUtils.convertBytesToHex(id));
    id = store.put(new ByteArrayInputStream(new byte[1]));
    assertEquals("000100", StringUtils.convertBytesToHex(id));
    id = store.put(new ByteArrayInputStream(new byte[3]));
    assertEquals("0003000000", StringUtils.convertBytesToHex(id));
    id = store.put(new ByteArrayInputStream(new byte[10]));
    assertEquals("010a8901", StringUtils.convertBytesToHex(id));
    byte[] combined = StringUtils.convertHexToBytes("0001aa0002bbcc");
    assertEquals(3, store.length(combined));
    InputStream in = store.get(combined);
    assertEquals(1, in.skip(1));
    assertEquals(0xbb, in.read());
    assertEquals(1, in.skip(1));
}
Also used : StreamStore(org.h2.mvstore.StreamStore) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 100 with In

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

the class TestCrashAPI method getConnection.

private Connection getConnection(int seed, boolean delete) throws SQLException {
    openCount++;
    if (delete) {
        deleteDb();
    }
    // can not use FILE_LOCK=NO, otherwise something could be written into
    // the database in the finalize method
    String add = ";MAX_QUERY_TIMEOUT=10000";
    // int testing;
    // if(openCount >= 32) {
    // int test;
    // Runtime.getRuntime().halt(0);
    // System.exit(1);
    // }
    // System.out.println("now open " + openCount);
    // add += ";TRACE_LEVEL_FILE=3";
    // config.logMode = 2;
    // }
    String dbName = "crashApi" + seed;
    String url = getURL(DIR + "/" + dbName, true) + add;
    // int test;
    // url += ";DB_CLOSE_ON_EXIT=FALSE";
    // int test;
    // url += ";TRACE_LEVEL_FILE=3";
    Connection conn = null;
    String fileName = "temp/backup/db-" + uniqueId++ + ".zip";
    Backup.execute(fileName, getBaseDir() + "/" + DIR, dbName, true);
    // close databases earlier
    System.gc();
    try {
        conn = DriverManager.getConnection(url, "sa", getPassword(""));
        // delete the backup if opening was successful
        FileUtils.delete(fileName);
    } catch (SQLException e) {
        if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) {
            // delete if the password changed
            FileUtils.delete(fileName);
        }
        throw e;
    }
    int len = random.getInt(50);
    int first = random.getInt(statements.size() - len);
    int end = first + len;
    Statement stat = conn.createStatement();
    stat.execute("SET LOCK_TIMEOUT 10");
    stat.execute("SET WRITE_DELAY 0");
    if (random.nextBoolean()) {
        if (random.nextBoolean()) {
            double g = random.nextGaussian();
            int size = (int) Math.abs(10000 * g * g);
            stat.execute("SET CACHE_SIZE " + size);
        } else {
            stat.execute("SET CACHE_SIZE 0");
        }
    }
    stat.execute("SCRIPT NOPASSWORDS NOSETTINGS");
    for (int i = first; i < end && i < statements.size() && !stopped; i++) {
        try {
            stat.execute("SELECT * FROM TEST WHERE ID=1");
        } catch (Throwable t) {
            printIfBad(seed, -i, -1, t);
        }
        try {
            stat.execute("SELECT * FROM TEST WHERE ID=1 OR ID=1");
        } catch (Throwable t) {
            printIfBad(seed, -i, -1, t);
        }
        String sql = statements.get(i);
        try {
            // if(openCount == 32) {
            // int test;
            // System.out.println("stop!");
            // }
            stat.execute(sql);
        } catch (Throwable t) {
            printIfBad(seed, -i, -1, t);
        }
    }
    if (random.nextBoolean()) {
        try {
            conn.commit();
        } catch (Throwable t) {
            printIfBad(seed, 0, -1, t);
        }
    }
    return conn;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) Savepoint(java.sql.Savepoint)

Aggregations

SQLException (java.sql.SQLException)63 Connection (java.sql.Connection)59 DbException (org.h2.message.DbException)56 PreparedStatement (java.sql.PreparedStatement)54 ResultSet (java.sql.ResultSet)47 Statement (java.sql.Statement)44 Value (org.h2.value.Value)40 IOException (java.io.IOException)39 ByteArrayInputStream (java.io.ByteArrayInputStream)30 InputStream (java.io.InputStream)29 Column (org.h2.table.Column)24 ArrayList (java.util.ArrayList)23 SimpleResultSet (org.h2.tools.SimpleResultSet)23 Random (java.util.Random)19 Expression (org.h2.expression.Expression)18 JdbcConnection (org.h2.jdbc.JdbcConnection)18 Index (org.h2.index.Index)16 ValueString (org.h2.value.ValueString)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)15