Search in sources :

Example 6 with IndexColumn

use of org.h2.table.IndexColumn in project ignite by apache.

the class GridSqlSortColumn method toIndexColumns.

/**
     * @param tbl Table.
     * @param sortCols Sort columns.
     * @return Index columns.
     */
public static IndexColumn[] toIndexColumns(Table tbl, List<GridSqlSortColumn> sortCols) {
    assert !F.isEmpty(sortCols);
    IndexColumn[] res = new IndexColumn[sortCols.size()];
    for (int i = 0; i < res.length; i++) {
        GridSqlSortColumn sc = sortCols.get(i);
        Column col = tbl.getColumn(sc.column());
        IndexColumn c = new IndexColumn();
        c.column = col;
        c.columnName = col.getName();
        c.sortType = sc.asc ? SortOrder.ASCENDING : SortOrder.DESCENDING;
        if (sc.nullsFirst)
            c.sortType |= SortOrder.NULLS_FIRST;
        if (sc.nullsLast)
            c.sortType |= SortOrder.NULLS_LAST;
        res[i] = c;
    }
    return res;
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) IndexColumn(org.h2.table.IndexColumn)

Example 7 with IndexColumn

use of org.h2.table.IndexColumn in project ignite by apache.

the class H2TableDescriptor method createSystemIndexes.

/** {@inheritDoc} */
@Override
public ArrayList<Index> createSystemIndexes(GridH2Table tbl) {
    ArrayList<Index> idxs = new ArrayList<>();
    IndexColumn keyCol = tbl.indexColumn(KEY_COL, SortOrder.ASCENDING);
    IndexColumn affCol = tbl.getAffinityKeyColumn();
    if (affCol != null && H2Utils.equals(affCol, keyCol))
        affCol = null;
    GridH2RowDescriptor desc = tbl.rowDescriptor();
    Index hashIdx = createHashIndex(tbl, "_key_PK_hash", H2Utils.treeIndexColumns(desc, new ArrayList<IndexColumn>(2), keyCol, affCol));
    if (hashIdx != null)
        idxs.add(hashIdx);
    // Add primary key index.
    Index pkIdx = idx.createSortedIndex(schema, "_key_PK", tbl, true, H2Utils.treeIndexColumns(desc, new ArrayList<IndexColumn>(2), keyCol, affCol), -1);
    idxs.add(pkIdx);
    if (type().valueClass() == String.class) {
        try {
            luceneIdx = new GridLuceneIndex(idx.kernalContext(), schema.offheap(), tbl.cacheName(), type);
        } catch (IgniteCheckedException e1) {
            throw new IgniteException(e1);
        }
    }
    boolean affIdxFound = false;
    GridQueryIndexDescriptor textIdx = type.textIndex();
    if (textIdx != null) {
        try {
            luceneIdx = new GridLuceneIndex(idx.kernalContext(), schema.offheap(), tbl.cacheName(), type);
        } catch (IgniteCheckedException e1) {
            throw new IgniteException(e1);
        }
    }
    // Locate index where affinity column is first (if any).
    if (affCol != null) {
        for (GridQueryIndexDescriptor idxDesc : type.indexes().values()) {
            if (idxDesc.type() != QueryIndexType.SORTED)
                continue;
            String firstField = idxDesc.fields().iterator().next();
            Column col = tbl.getColumn(firstField);
            IndexColumn idxCol = tbl.indexColumn(col.getColumnId(), idxDesc.descending(firstField) ? SortOrder.DESCENDING : SortOrder.ASCENDING);
            affIdxFound |= H2Utils.equals(idxCol, affCol);
        }
    }
    // Add explicit affinity key index if nothing alike was found.
    if (affCol != null && !affIdxFound) {
        idxs.add(idx.createSortedIndex(schema, "AFFINITY_KEY", tbl, false, H2Utils.treeIndexColumns(desc, new ArrayList<IndexColumn>(2), affCol, keyCol), -1));
    }
    return idxs;
}
Also used : ArrayList(java.util.ArrayList) GridLuceneIndex(org.apache.ignite.internal.processors.query.h2.opt.GridLuceneIndex) GridLuceneIndex(org.apache.ignite.internal.processors.query.h2.opt.GridLuceneIndex) H2PkHashIndex(org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex) Index(org.h2.index.Index) IndexColumn(org.h2.table.IndexColumn) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) IgniteException(org.apache.ignite.IgniteException) GridQueryIndexDescriptor(org.apache.ignite.internal.processors.query.GridQueryIndexDescriptor)

Example 8 with IndexColumn

use of org.h2.table.IndexColumn in project ignite by apache.

the class H2Utils method indexCreateSql.

/**
     * Generate {@code CREATE INDEX} SQL statement for given params.
     * @param fullTblName Fully qualified table name.
     * @param h2Idx H2 index.
     * @param ifNotExists Quietly skip index creation if it exists.
     * @return Statement string.
     */
public static String indexCreateSql(String fullTblName, GridH2IndexBase h2Idx, boolean ifNotExists) {
    boolean spatial = F.eq(SPATIAL_IDX_CLS, h2Idx.getClass().getName());
    GridStringBuilder sb = new SB("CREATE ").a(spatial ? "SPATIAL " : "").a("INDEX ").a(ifNotExists ? "IF NOT EXISTS " : "").a(withQuotes(h2Idx.getName())).a(" ON ").a(fullTblName).a(" (");
    boolean first = true;
    for (IndexColumn col : h2Idx.getIndexColumns()) {
        if (first)
            first = false;
        else
            sb.a(", ");
        sb.a(withQuotes(col.columnName)).a(" ").a(col.sortType == SortOrder.ASCENDING ? "ASC" : "DESC");
    }
    sb.a(')');
    return sb.toString();
}
Also used : GridStringBuilder(org.apache.ignite.internal.util.GridStringBuilder) SB(org.apache.ignite.internal.util.typedef.internal.SB) IndexColumn(org.h2.table.IndexColumn)

Example 9 with IndexColumn

use of org.h2.table.IndexColumn in project ignite by apache.

the class H2Tree method compare.

/** {@inheritDoc} */
@Override
protected int compare(BPlusIO<SearchRow> io, long pageAddr, int idx, SearchRow row) throws IgniteCheckedException {
    if (inlineSize() == 0)
        return compareRows(getRow(io, pageAddr, idx), row);
    else {
        int off = io.offset(idx);
        int fieldOff = 0;
        int lastIdxUsed = 0;
        for (int i = 0; i < inlineIdxs.size(); i++) {
            InlineIndexHelper inlineIdx = inlineIdxs.get(i);
            Value v2 = row.getValue(inlineIdx.columnIndex());
            if (v2 == null)
                return 0;
            int c = inlineIdx.compare(pageAddr, off + fieldOff, inlineSize() - fieldOff, v2, comp);
            if (c == -2)
                break;
            lastIdxUsed++;
            if (c != 0)
                return c;
            fieldOff += inlineIdx.fullSize(pageAddr, off + fieldOff);
            if (fieldOff > inlineSize())
                break;
        }
        if (lastIdxUsed == cols.length)
            return 0;
        SearchRow rowData = getRow(io, pageAddr, idx);
        for (int i = lastIdxUsed, len = cols.length; i < len; i++) {
            IndexColumn col = cols[i];
            int idx0 = col.column.getColumnId();
            Value v2 = row.getValue(idx0);
            if (v2 == null) {
                // Can't compare further.
                return 0;
            }
            Value v1 = rowData.getValue(idx0);
            int c = compareValues(v1, v2);
            if (c != 0)
                return InlineIndexHelper.fixSort(c, col.sortType);
        }
        return 0;
    }
}
Also used : Value(org.h2.value.Value) SearchRow(org.h2.result.SearchRow) IndexColumn(org.h2.table.IndexColumn)

Example 10 with IndexColumn

use of org.h2.table.IndexColumn in project ignite by apache.

the class H2TreeIndex method getAvailableInlineColumns.

/**
     * @param cols Columns array.
     * @return List of {@link InlineIndexHelper} objects.
     */
private List<InlineIndexHelper> getAvailableInlineColumns(IndexColumn[] cols) {
    List<InlineIndexHelper> res = new ArrayList<>();
    for (IndexColumn col : cols) {
        if (!InlineIndexHelper.AVAILABLE_TYPES.contains(col.column.getType()))
            break;
        InlineIndexHelper idx = new InlineIndexHelper(col.column.getType(), col.column.getColumnId(), col.sortType);
        res.add(idx);
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) IndexColumn(org.h2.table.IndexColumn)

Aggregations

IndexColumn (org.h2.table.IndexColumn)17 Column (org.h2.table.Column)5 ArrayList (java.util.ArrayList)4 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)3 ExpressionColumn (org.h2.expression.ExpressionColumn)3 LinkedHashMap (java.util.LinkedHashMap)2 CacheException (javax.cache.CacheException)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)2 SB (org.apache.ignite.internal.util.typedef.internal.SB)2 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)2 Query (org.h2.command.dml.Query)2 Schema (org.h2.schema.Schema)2 Value (org.h2.value.Value)2 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1