Search in sources :

Example 46 with Set

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

the class TestPerfectHash method testMinimal.

private int testMinimal(int size) {
    Random r = new Random(size);
    HashSet<Long> set = new HashSet<>(size);
    while (set.size() < size) {
        set.add((long) r.nextInt());
    }
    LongHash hf = new LongHash();
    byte[] desc = MinimalPerfectHash.generate(set, hf);
    int max = testMinimal(desc, set, hf);
    assertEquals(size - 1, max);
    return desc.length * 8;
}
Also used : Random(java.util.Random) LongHash(org.h2.dev.hash.MinimalPerfectHash.LongHash) HashSet(java.util.HashSet)

Example 47 with Set

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

the class TestPerfectHash method testMinimalWithString.

private int testMinimalWithString(int size) {
    Random r = new Random(size);
    HashSet<String> set = new HashSet<>(size);
    while (set.size() < size) {
        set.add("x " + r.nextDouble());
    }
    StringHash hf = new StringHash();
    byte[] desc = MinimalPerfectHash.generate(set, hf);
    int max = testMinimal(desc, set, hf);
    assertEquals(size - 1, max);
    return desc.length * 8;
}
Also used : Random(java.util.Random) StringHash(org.h2.dev.hash.MinimalPerfectHash.StringHash) HashSet(java.util.HashSet)

Example 48 with Set

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

the class TestPerfectHash method testBrokenHashFunction.

private void testBrokenHashFunction() {
    int size = 10000;
    Random r = new Random(10000);
    HashSet<String> set = new HashSet<>(size);
    while (set.size() < size) {
        set.add("x " + r.nextDouble());
    }
    for (int test = 1; test < 10; test++) {
        final int badUntilLevel = test;
        UniversalHash<String> badHash = new UniversalHash<String>() {

            @Override
            public int hashCode(String o, int index, int seed) {
                if (index < badUntilLevel) {
                    return 0;
                }
                return StringHash.getFastHash(o, index, seed);
            }
        };
        byte[] desc = MinimalPerfectHash.generate(set, badHash);
        testMinimal(desc, set, badHash);
    }
}
Also used : UniversalHash(org.h2.dev.hash.MinimalPerfectHash.UniversalHash) Random(java.util.Random) HashSet(java.util.HashSet)

Example 49 with Set

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

the class TestPerfectHash method testMinimal.

private <K> int testMinimal(byte[] desc, Set<K> set, UniversalHash<K> hf) {
    int max = -1;
    BitSet test = new BitSet();
    MinimalPerfectHash<K> hash = new MinimalPerfectHash<>(desc, hf);
    for (K x : set) {
        int h = hash.get(x);
        assertTrue(h >= 0);
        assertTrue(h <= set.size() * 3);
        max = Math.max(max, h);
        assertFalse(test.get(h));
        test.set(h);
    }
    return max;
}
Also used : MinimalPerfectHash(org.h2.dev.hash.MinimalPerfectHash) BitSet(java.util.BitSet)

Example 50 with Set

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

the class TestRecovery method testCompressedAndUncompressed.

private void testCompressedAndUncompressed() 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 test(id int primary key, data clob)");
    stat.execute("insert into test values(1, space(10000))");
    stat.execute("set compress_lob lzf");
    stat.execute("insert into test values(2, space(10000))");
    conn.close();
    Recover rec = new Recover();
    rec.runTool("-dir", getBaseDir(), "-db", "recovery");
    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();
    DeleteDbFiles.execute(getBaseDir(), "recovery", true);
    DeleteDbFiles.execute(getBaseDir(), "recovery2", true);
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) Recover(org.h2.tools.Recover)

Aggregations

SQLException (java.sql.SQLException)107 DbException (org.h2.message.DbException)80 PreparedStatement (java.sql.PreparedStatement)78 Connection (java.sql.Connection)70 Statement (java.sql.Statement)63 ResultSet (java.sql.ResultSet)62 Value (org.h2.value.Value)51 JdbcConnection (org.h2.jdbc.JdbcConnection)48 SimpleResultSet (org.h2.tools.SimpleResultSet)36 HashSet (java.util.HashSet)28 Column (org.h2.table.Column)24 Savepoint (java.sql.Savepoint)23 ValueString (org.h2.value.ValueString)21 IOException (java.io.IOException)20 Random (java.util.Random)17 Expression (org.h2.expression.Expression)16 Task (org.h2.util.Task)16 ExpressionColumn (org.h2.expression.ExpressionColumn)14 ValueExpression (org.h2.expression.ValueExpression)13 IndexColumn (org.h2.table.IndexColumn)13