Search in sources :

Example 71 with Index

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

the class ResultTempTable method createIndex.

private void createIndex() {
    IndexColumn[] indexCols = null;
    // sort columns. So we need to disregard the sort. Not ideal.
    if (sort != null && !distinct) {
        int[] colIndex = sort.getQueryColumnIndexes();
        indexCols = new IndexColumn[colIndex.length];
        for (int i = 0; i < colIndex.length; i++) {
            IndexColumn indexColumn = new IndexColumn();
            indexColumn.column = table.getColumn(colIndex[i]);
            indexColumn.sortType = sort.getSortTypes()[i];
            indexColumn.columnName = COLUMN_NAME + i;
            indexCols[i] = indexColumn;
        }
    } else {
        indexCols = new IndexColumn[columnCount];
        for (int i = 0; i < columnCount; i++) {
            IndexColumn indexColumn = new IndexColumn();
            indexColumn.column = table.getColumn(i);
            indexColumn.columnName = COLUMN_NAME + i;
            indexCols[i] = indexColumn;
        }
    }
    String indexName = table.getSchema().getUniqueIndexName(session, table, Constants.PREFIX_INDEX);
    int indexId = session.getDatabase().allocateObjectId();
    IndexType indexType = IndexType.createNonUnique(true);
    index = table.addIndex(session, indexName, indexId, indexCols, indexType, true, null);
}
Also used : IndexType(org.h2.index.IndexType) IndexColumn(org.h2.table.IndexColumn)

Example 72 with Index

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

the class SortOrder method getColumn.

/**
 * Get the column for the given table filter, if the sort column is for this
 * filter.
 *
 * @param index the column index (0, 1,..)
 * @param filter the table filter
 * @return the column, or null
 */
public Column getColumn(int index, TableFilter filter) {
    if (orderList == null) {
        return null;
    }
    SelectOrderBy order = orderList.get(index);
    Expression expr = order.expression;
    if (expr == null) {
        return null;
    }
    expr = expr.getNonAliasExpression();
    if (expr.isConstant()) {
        return null;
    }
    if (!(expr instanceof ExpressionColumn)) {
        return null;
    }
    ExpressionColumn exprCol = (ExpressionColumn) expr;
    if (exprCol.getTableFilter() != filter) {
        return null;
    }
    return exprCol.getColumn();
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy) Expression(org.h2.expression.Expression) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 73 with Index

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

the class Page method setValue.

/**
 * Replace the value at an index in this page.
 *
 * @param index the index
 * @param value the new value
 * @return the old value
 */
public Object setValue(int index, Object value) {
    Object old = values[index];
    // this is slightly slower:
    // values = Arrays.copyOf(values, values.length);
    values = values.clone();
    DataType valueType = map.getValueType();
    if (isPersistent()) {
        addMemory(valueType.getMemory(value) - valueType.getMemory(old));
    }
    values[index] = value;
    return old;
}
Also used : DataType(org.h2.mvstore.type.DataType)

Example 74 with Index

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

the class Page method setKey.

/**
 * Replace the key at an index in this page.
 *
 * @param index the index
 * @param key the new key
 */
public void setKey(int index, Object key) {
    // this is slightly slower:
    // keys = Arrays.copyOf(keys, keys.length);
    keys = keys.clone();
    if (isPersistent()) {
        Object old = keys[index];
        DataType keyType = map.getKeyType();
        int mem = keyType.getMemory(key);
        if (old != null) {
            mem -= keyType.getMemory(old);
        }
        addMemory(mem);
    }
    keys[index] = key;
}
Also used : DataType(org.h2.mvstore.type.DataType)

Example 75 with Index

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

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