Search in sources :

Example 21 with IndexColumn

use of org.h2.table.IndexColumn in project h2database by h2database.

the class NonUniqueHashIndex method find.

@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
    if (first == null || last == null) {
        throw DbException.throwInternalError(first + " " + last);
    }
    if (first != last) {
        if (compareKeys(first, last) != 0) {
            throw DbException.throwInternalError();
        }
    }
    Value v = first.getValue(indexColumn);
    /*
         * Sometimes the incoming search is a similar, but not the same type
         * e.g. the search value is INT, but the index column is LONG. In which
         * case we need to convert, otherwise the ValueHashMap will not find the
         * result.
         */
    v = v.convertTo(tableData.getColumn(indexColumn).getType());
    ArrayList<Long> positions = rows.get(v);
    return new NonUniqueHashCursor(session, tableData, positions);
}
Also used : Value(org.h2.value.Value)

Example 22 with IndexColumn

use of org.h2.table.IndexColumn in project h2database by h2database.

the class NonUniqueHashIndex method add.

@Override
public void add(Session session, Row row) {
    Value key = row.getValue(indexColumn);
    ArrayList<Long> positions = rows.get(key);
    if (positions == null) {
        positions = New.arrayList();
        rows.put(key, positions);
    }
    positions.add(row.getKey());
    rowCount++;
}
Also used : Value(org.h2.value.Value)

Example 23 with IndexColumn

use of org.h2.table.IndexColumn in project h2database by h2database.

the class PageStore method addMeta.

private void addMeta(Row row, Session session, boolean redo) {
    int id = row.getValue(0).getInt();
    int type = row.getValue(1).getInt();
    int parent = row.getValue(2).getInt();
    int rootPageId = row.getValue(3).getInt();
    String[] options = StringUtils.arraySplit(row.getValue(4).getString(), ',', false);
    String columnList = row.getValue(5).getString();
    String[] columns = StringUtils.arraySplit(columnList, ',', false);
    Index meta;
    if (trace.isDebugEnabled()) {
        trace.debug("addMeta id=" + id + " type=" + type + " root=" + rootPageId + " parent=" + parent + " columns=" + columnList);
    }
    if (redo && rootPageId != 0) {
        // ensure the page is empty, but not used by regular data
        writePage(rootPageId, createData());
        allocatePage(rootPageId);
    }
    metaRootPageId.put(id, rootPageId);
    if (type == META_TYPE_DATA_INDEX) {
        CreateTableData data = new CreateTableData();
        if (SysProperties.CHECK) {
            if (columns == null) {
                throw DbException.throwInternalError(row.toString());
            }
        }
        for (int i = 0, len = columns.length; i < len; i++) {
            Column col = new Column("C" + i, Value.INT);
            data.columns.add(col);
        }
        data.schema = metaSchema;
        data.tableName = "T" + id;
        data.id = id;
        data.temporary = options[2].equals("temp");
        data.persistData = true;
        data.persistIndexes = true;
        data.create = false;
        data.session = session;
        RegularTable table = new RegularTable(data);
        boolean binaryUnsigned = SysProperties.SORT_BINARY_UNSIGNED;
        if (options.length > 3) {
            binaryUnsigned = Boolean.parseBoolean(options[3]);
        }
        CompareMode mode = CompareMode.getInstance(options[0], Integer.parseInt(options[1]), binaryUnsigned);
        table.setCompareMode(mode);
        meta = table.getScanIndex(session);
    } else {
        Index p = metaObjects.get(parent);
        if (p == null) {
            throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "Table not found:" + parent + " for " + row + " meta:" + metaObjects);
        }
        RegularTable table = (RegularTable) p.getTable();
        Column[] tableCols = table.getColumns();
        int len = columns.length;
        IndexColumn[] cols = new IndexColumn[len];
        for (int i = 0; i < len; i++) {
            String c = columns[i];
            IndexColumn ic = new IndexColumn();
            int idx = c.indexOf('/');
            if (idx >= 0) {
                String s = c.substring(idx + 1);
                ic.sortType = Integer.parseInt(s);
                c = c.substring(0, idx);
            }
            ic.column = tableCols[Integer.parseInt(c)];
            cols[i] = ic;
        }
        IndexType indexType;
        if (options[3].equals("d")) {
            indexType = IndexType.createPrimaryKey(true, false);
            Column[] tableColumns = table.getColumns();
            for (IndexColumn indexColumn : cols) {
                tableColumns[indexColumn.column.getColumnId()].setNullable(false);
            }
        } else {
            indexType = IndexType.createNonUnique(true);
        }
        meta = table.addIndex(session, "I" + id, id, cols, indexType, false, null);
    }
    PageIndex index;
    if (meta instanceof MultiVersionIndex) {
        index = (PageIndex) ((MultiVersionIndex) meta).getBaseIndex();
    } else {
        index = (PageIndex) meta;
    }
    metaObjects.put(id, index);
}
Also used : Index(org.h2.index.Index) PageIndex(org.h2.index.PageIndex) PageDelegateIndex(org.h2.index.PageDelegateIndex) MultiVersionIndex(org.h2.index.MultiVersionIndex) PageBtreeIndex(org.h2.index.PageBtreeIndex) PageDataIndex(org.h2.index.PageDataIndex) ValueString(org.h2.value.ValueString) PageIndex(org.h2.index.PageIndex) CreateTableData(org.h2.command.ddl.CreateTableData) IndexColumn(org.h2.table.IndexColumn) IndexColumn(org.h2.table.IndexColumn) Column(org.h2.table.Column) MultiVersionIndex(org.h2.index.MultiVersionIndex) RegularTable(org.h2.table.RegularTable) CompareMode(org.h2.value.CompareMode) IndexType(org.h2.index.IndexType)

Example 24 with IndexColumn

use of org.h2.table.IndexColumn in project h2database by h2database.

the class PageStore method addMeta.

/**
 * Add the meta data of an index.
 *
 * @param index the index to add
 * @param session the session
 */
public void addMeta(PageIndex index, Session session) {
    Table table = index.getTable();
    if (SysProperties.CHECK) {
        if (!table.isTemporary()) {
            // the Database lock before we take the PageStore lock
            synchronized (database) {
                synchronized (this) {
                    database.verifyMetaLocked(session);
                }
            }
        }
    }
    synchronized (this) {
        int type = index instanceof PageDataIndex ? META_TYPE_DATA_INDEX : META_TYPE_BTREE_INDEX;
        IndexColumn[] columns = index.getIndexColumns();
        StatementBuilder buff = new StatementBuilder();
        for (IndexColumn col : columns) {
            buff.appendExceptFirst(",");
            int id = col.column.getColumnId();
            buff.append(id);
            int sortType = col.sortType;
            if (sortType != 0) {
                buff.append('/');
                buff.append(sortType);
            }
        }
        String columnList = buff.toString();
        CompareMode mode = table.getCompareMode();
        String options = mode.getName() + "," + mode.getStrength() + ",";
        if (table.isTemporary()) {
            options += "temp";
        }
        options += ",";
        if (index instanceof PageDelegateIndex) {
            options += "d";
        }
        options += "," + mode.isBinaryUnsigned();
        Row row = metaTable.getTemplateRow();
        row.setValue(0, ValueInt.get(index.getId()));
        row.setValue(1, ValueInt.get(type));
        row.setValue(2, ValueInt.get(table.getId()));
        row.setValue(3, ValueInt.get(index.getRootPageId()));
        row.setValue(4, ValueString.get(options));
        row.setValue(5, ValueString.get(columnList));
        row.setKey(index.getId() + 1);
        metaIndex.add(session, row);
    }
}
Also used : RegularTable(org.h2.table.RegularTable) Table(org.h2.table.Table) PageDelegateIndex(org.h2.index.PageDelegateIndex) PageDataIndex(org.h2.index.PageDataIndex) StatementBuilder(org.h2.util.StatementBuilder) CompareMode(org.h2.value.CompareMode) ValueString(org.h2.value.ValueString) Row(org.h2.result.Row) IndexColumn(org.h2.table.IndexColumn)

Example 25 with IndexColumn

use of org.h2.table.IndexColumn 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)

Aggregations

IndexColumn (org.h2.table.IndexColumn)53 Column (org.h2.table.Column)23 ArrayList (java.util.ArrayList)14 Index (org.h2.index.Index)12 Value (org.h2.value.Value)12 IndexType (org.h2.index.IndexType)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 Constraint (org.h2.constraint.Constraint)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 LinkedHashMap (java.util.LinkedHashMap)5 CacheException (javax.cache.CacheException)5 TableFilter (org.h2.table.TableFilter)5 StatementBuilder (org.h2.util.StatementBuilder)5 ValueString (org.h2.value.ValueString)5 HashMap (java.util.HashMap)4 H2TreeIndex (org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex)4 H2TreeIndexBase (org.apache.ignite.internal.processors.query.h2.database.H2TreeIndexBase)4 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)4 Row (org.h2.result.Row)4 ResultSet (java.sql.ResultSet)3