Search in sources :

Example 76 with Index

use of org.h2.index.Index 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 77 with Index

use of org.h2.index.Index in project h2database by h2database.

the class TableDefinition method mapObject.

void mapObject(Object obj) {
    fieldMap.clear();
    initObject(obj, fieldMap);
    if (clazz.isAnnotationPresent(JQSchema.class)) {
        JQSchema schemaAnnotation = clazz.getAnnotation(JQSchema.class);
        // setup schema name mapping, if properly annotated
        if (!StringUtils.isNullOrEmpty(schemaAnnotation.name())) {
            schemaName = schemaAnnotation.name();
        }
    }
    if (clazz.isAnnotationPresent(JQTable.class)) {
        JQTable tableAnnotation = clazz.getAnnotation(JQTable.class);
        // setup table name mapping, if properly annotated
        if (!StringUtils.isNullOrEmpty(tableAnnotation.name())) {
            tableName = tableAnnotation.name();
        }
        // allow control over createTableIfRequired()
        createTableIfRequired = tableAnnotation.createIfRequired();
        // model version
        if (tableAnnotation.version() > 0) {
            tableVersion = tableAnnotation.version();
        }
        // setup the primary index, if properly annotated
        List<String> primaryKey = getColumns(tableAnnotation.primaryKey());
        if (primaryKey != null) {
            setPrimaryKey(primaryKey);
        }
    }
    if (clazz.isAnnotationPresent(JQIndex.class)) {
        JQIndex indexAnnotation = clazz.getAnnotation(JQIndex.class);
        // setup the indexes, if properly annotated
        addIndexes(IndexType.STANDARD, indexAnnotation.standard());
        addIndexes(IndexType.UNIQUE, indexAnnotation.unique());
        addIndexes(IndexType.HASH, indexAnnotation.hash());
        addIndexes(IndexType.UNIQUE_HASH, indexAnnotation.uniqueHash());
    }
}
Also used : JQTable(org.h2.jaqu.Table.JQTable) JQIndex(org.h2.jaqu.Table.JQIndex) JQSchema(org.h2.jaqu.Table.JQSchema)

Example 78 with Index

use of org.h2.index.Index in project h2database by h2database.

the class TestAll method run.

private static void run(String... args) throws Exception {
    SelfDestructor.startCountdown(4 * 60);
    long time = System.nanoTime();
    printSystemInfo();
    // use lower values, to better test those cases,
    // and (for delays) to speed up the tests
    System.setProperty("h2.maxMemoryRows", "100");
    System.setProperty("h2.check2", "true");
    System.setProperty("h2.delayWrongPasswordMin", "0");
    System.setProperty("h2.delayWrongPasswordMax", "0");
    System.setProperty("h2.useThreadContextClassLoader", "true");
    // System.setProperty("h2.modifyOnWrite", "true");
    // speedup
    // System.setProperty("h2.syncMethod", "");
    /*

recovery tests with small freeList pages, page size 64

reopen org.h2.test.unit.TestPageStore
-Xmx1500m -D reopenOffset=3 -D reopenShift=1

power failure test
power failure test: MULTI_THREADED=TRUE
power failure test: larger binaries and additional index.
power failure test with randomly generating / dropping indexes and tables.

drop table test;
create table test(id identity, name varchar(100) default space(100));
@LOOP 10 insert into test select null, null from system_range(1, 100000);
delete from test;

documentation: review package and class level javadocs
documentation: rolling review at main.html

-------------

remove old TODO, move to roadmap

kill a test:
kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`

*/
    TestAll test = new TestAll();
    if (args.length > 0) {
        if ("travis".equals(args[0])) {
            test.travis = true;
            test.testAll();
        } else if ("vmlens".equals(args[0])) {
            test.vmlens = true;
            test.testAll();
        } else if ("reopen".equals(args[0])) {
            System.setProperty("h2.delayWrongPasswordMin", "0");
            System.setProperty("h2.check2", "false");
            System.setProperty("h2.analyzeAuto", "100");
            System.setProperty("h2.pageSize", "64");
            System.setProperty("h2.reopenShift", "5");
            FilePathRec.register();
            test.reopen = true;
            TestReopen reopen = new TestReopen();
            reopen.init();
            FilePathRec.setRecorder(reopen);
            test.runTests();
        } else if ("crash".equals(args[0])) {
            test.endless = true;
            new TestCrashAPI().runTest(test);
        } else if ("synth".equals(args[0])) {
            new TestSynth().runTest(test);
        } else if ("kill".equals(args[0])) {
            new TestKill().runTest(test);
        } else if ("random".equals(args[0])) {
            test.endless = true;
            new TestRandomSQL().runTest(test);
        } else if ("join".equals(args[0])) {
            new TestJoin().runTest(test);
            test.endless = true;
        } else if ("btree".equals(args[0])) {
            new TestBtreeIndex().runTest(test);
        } else if ("all".equals(args[0])) {
            test.testEverything();
        } else if ("codeCoverage".equals(args[0])) {
            test.codeCoverage = true;
            test.runCoverage();
        } else if ("multiThread".equals(args[0])) {
            new TestMulti().runTest(test);
        } else if ("halt".equals(args[0])) {
            new TestHaltApp().runTest(test);
        } else if ("timer".equals(args[0])) {
            new TestTimer().runTest(test);
        }
    } else {
        test.testAll();
    }
    System.out.println(TestBase.formatTime(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time)) + " total");
}
Also used : TestBtreeIndex(org.h2.test.synth.TestBtreeIndex) TestCrashAPI(org.h2.test.synth.TestCrashAPI) TestHaltApp(org.h2.test.synth.TestHaltApp) TestJoin(org.h2.test.synth.TestJoin) TestTimer(org.h2.test.synth.TestTimer) TestMulti(org.h2.test.synth.thread.TestMulti) TestKill(org.h2.test.synth.TestKill) TestReopen(org.h2.test.unit.TestReopen) TestSynth(org.h2.test.synth.sql.TestSynth) TestRandomSQL(org.h2.test.synth.TestRandomSQL)

Example 79 with Index

use of org.h2.index.Index in project h2database by h2database.

the class ValueHashMap method get.

/**
 * Get the value for this key. This method returns null if the key was not
 * found.
 *
 * @param key the key
 * @return the value for the given key
 */
public V get(Value key) {
    int index = getIndex(key);
    int plus = 1;
    do {
        Value k = keys[index];
        if (k == null) {
            // found an empty record
            return null;
        } else if (k == ValueNull.DELETED) {
        // found a deleted record
        } else if (k.equals(key)) {
            // found it
            return values[index];
        }
        index = (index + plus++) & mask;
    } while (plus <= len);
    return null;
}
Also used : Value(org.h2.value.Value)

Example 80 with Index

use of org.h2.index.Index in project h2database by h2database.

the class TestMVTableEngine method testAutoCommit.

private void testAutoCommit() throws SQLException {
    Connection conn;
    Statement stat;
    ResultSet rs;
    deleteDb(getTestName());
    conn = getConnection(getTestName() + ";MV_STORE=TRUE");
    for (int i = 0; i < 2; i++) {
        stat = conn.createStatement();
        stat.execute("create table test(id int primary key, name varchar)");
        stat.execute("create index on test(name)");
        conn.setAutoCommit(false);
        stat.execute("insert into test values(1, 'Hello')");
        stat.execute("insert into test values(2, 'World')");
        rs = stat.executeQuery("select count(*) from test");
        rs.next();
        assertEquals(2, rs.getInt(1));
        conn.rollback();
        rs = stat.executeQuery("select count(*) from test");
        rs.next();
        assertEquals(0, rs.getInt(1));
        stat.execute("insert into test values(1, 'Hello')");
        Savepoint sp = conn.setSavepoint();
        stat.execute("insert into test values(2, 'World')");
        conn.rollback(sp);
        rs = stat.executeQuery("select count(*) from test");
        rs.next();
        assertEquals(1, rs.getInt(1));
        stat.execute("drop table test");
    }
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint) Savepoint(java.sql.Savepoint)

Aggregations

ResultSet (java.sql.ResultSet)77 Index (org.h2.index.Index)75 Statement (java.sql.Statement)64 SimpleResultSet (org.h2.tools.SimpleResultSet)61 SQLException (java.sql.SQLException)60 Value (org.h2.value.Value)58 DbException (org.h2.message.DbException)57 Connection (java.sql.Connection)56 PreparedStatement (java.sql.PreparedStatement)56 Column (org.h2.table.Column)53 IndexColumn (org.h2.table.IndexColumn)45 ArrayList (java.util.ArrayList)31 ValueString (org.h2.value.ValueString)29 Constraint (org.h2.constraint.Constraint)25 Row (org.h2.result.Row)22 MultiVersionIndex (org.h2.index.MultiVersionIndex)19 JdbcConnection (org.h2.jdbc.JdbcConnection)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)18 Table (org.h2.table.Table)18 HashSet (java.util.HashSet)17