Search in sources :

Example 6 with Index

use of org.hsqldb_voltpatches.index.Index in project voltdb by VoltDB.

the class TableWorks method addUniqueExprConstraint.

/**
     * A VoltDB extended variant of addUniqueConstraint that supports indexed generalized non-column expressions.
     * @param cols
     * @param indexExprs
     * @param name HsqlName
     */
public void addUniqueExprConstraint(int[] cols, Expression[] indexExprs, HsqlName name, boolean isAutogeneratedName, boolean assumeUnique) {
    database.schemaManager.checkSchemaObjectNotExists(name);
    if (table.getUniqueConstraintForExprs(indexExprs) != null) {
        throw Error.error(ErrorCode.X_42522);
    }
    // create an autonamed index
    HsqlName indexname = database.nameManager.newAutoName("IDX", name.name, table.getSchemaName(), table.getName(), SchemaObject.INDEX);
    Index exprIndex = table.createIndexStructure(indexname, cols, null, null, true, true, false);
    exprIndex = exprIndex.withExpressions(indexExprs);
    Constraint constraint = new Constraint(name, isAutogeneratedName, table, exprIndex, Constraint.UNIQUE).setAssumeUnique(assumeUnique);
    Table tn = table.moveDefinition(session, table.tableType, null, constraint, exprIndex, -1, 0, emptySet, emptySet);
    tn.moveData(session, table, -1, 0);
    database.persistentStoreCollection.releaseStore(table);
    table = tn;
    database.schemaManager.addSchemaObject(constraint);
    setNewTableInSchema(table);
    updateConstraints(table, emptySet);
    database.schemaManager.recompileDependentObjects(table);
}
Also used : HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) Index(org.hsqldb_voltpatches.index.Index)

Example 7 with Index

use of org.hsqldb_voltpatches.index.Index in project voltdb by VoltDB.

the class Table method voltGetTableXML.

/**
     * VoltDB added method to get a non-catalog-dependent
     * representation of this HSQLDB object.
     * @param session The current Session object may be needed to resolve
     * some names.
     * @return XML, correctly indented, representing this object.
     * @throws HSQLParseException
     */
VoltXMLElement voltGetTableXML(Session session) throws org.hsqldb_voltpatches.HSQLInterface.HSQLParseException {
    VoltXMLElement table = new VoltXMLElement("table");
    // add table metadata
    String tableName = getName().name;
    table.attributes.put("name", tableName);
    // read all the columns
    VoltXMLElement columns = new VoltXMLElement("columns");
    columns.attributes.put("name", "columns");
    table.children.add(columns);
    int[] columnIndices = getColumnMap();
    for (int i : columnIndices) {
        ColumnSchema column = getColumn(i);
        VoltXMLElement colChild = column.voltGetColumnXML(session);
        colChild.attributes.put("index", Integer.toString(i));
        columns.children.add(colChild);
        assert (colChild != null);
    }
    // read all the constraints
    VoltXMLElement constraints = new VoltXMLElement("constraints");
    constraints.attributes.put("name", "constraints");
    Map<String, VoltXMLElement> indexConstraintMap = new HashMap<>();
    for (Constraint constraint : getConstraints()) {
        VoltXMLElement constraintChild = constraint.voltGetConstraintXML();
        constraints.children.add(constraintChild);
        if (constraintChild.attributes.containsKey("index")) {
            indexConstraintMap.put(constraintChild.attributes.get("index"), constraintChild);
        }
    }
    // read all the indexes
    VoltXMLElement indexes = new VoltXMLElement("indexes");
    indexes.attributes.put("name", "indexes");
    for (Index index : indexList) {
        VoltXMLElement indexChild = index.voltGetIndexXML(session, tableName, indexConstraintMap);
        indexes.children.add(indexChild);
        assert (indexChild != null);
    }
    // Indexes must come before constraints when converting to the catalog.
    table.children.add(indexes);
    table.children.add(constraints);
    assert (indexConstraintMap.isEmpty());
    return table;
}
Also used : HashMap(java.util.HashMap) Index(org.hsqldb_voltpatches.index.Index)

Example 8 with Index

use of org.hsqldb_voltpatches.index.Index in project voltdb by VoltDB.

the class TableBase method setBestRowIdentifiers.

/**
     * This method is called whenever there is a change to table structure and
     * serves two porposes: (a) to reset the best set of columns that identify
     * the rows of the table (b) to reset the best index that can be used
     * to find rows of the table given a column value.
     *
     * (a) gives most weight to a primary key index, followed by a unique
     * address with the lowest count of nullable columns. Otherwise there is
     * no best row identifier.
     *
     * (b) finds for each column an index with a corresponding first column.
     * It uses any type of visible index and accepts the one with the largest
     * column count.
     *
     * bestIndex is the user defined, primary key, the first unique index, or
     * the first non-unique index. NULL if there is no user-defined index.
     *
     */
public final void setBestRowIdentifiers() {
    int[] briCols = null;
    int briColsCount = 0;
    boolean isStrict = false;
    int nNullCount = 0;
    // ignore if called prior to completion of primary key construction
    if (colNotNull == null) {
        return;
    }
    bestIndex = null;
    bestIndexForColumn = new int[colTypes.length];
    ArrayUtil.fillArray(bestIndexForColumn, -1);
    for (int i = 0; i < indexList.length; i++) {
        Index index = indexList[i];
        // Expression-based indexes are not suitable for row identification.
        if (index.getExpressions() != null) {
            continue;
        }
        // End of VoltDB extension
        int[] cols = index.getColumns();
        int colsCount = index.getVisibleColumns();
        if (colsCount == 0) {
            continue;
        }
        if (i == 0) {
            isStrict = true;
        }
        if (bestIndexForColumn[cols[0]] == -1) {
            bestIndexForColumn[cols[0]] = i;
        } else {
            Index existing = indexList[bestIndexForColumn[cols[0]]];
            if (colsCount > existing.getColumns().length) {
                bestIndexForColumn[cols[0]] = i;
            }
        }
        if (!index.isUnique()) {
            if (bestIndex == null) {
                bestIndex = index;
            }
            continue;
        }
        int nnullc = 0;
        for (int j = 0; j < colsCount; j++) {
            if (colNotNull[cols[j]]) {
                nnullc++;
            }
        }
        if (bestIndex != null) {
            bestIndex = index;
        }
        if (nnullc == colsCount) {
            if (briCols == null || briColsCount != nNullCount || colsCount < briColsCount) {
                //  nothing found before ||
                //  found but has null columns ||
                //  found but has more columns than this index
                briCols = cols;
                briColsCount = colsCount;
                nNullCount = colsCount;
                isStrict = true;
            }
            continue;
        } else if (isStrict) {
            continue;
        } else if (briCols == null || colsCount < briColsCount || nnullc > nNullCount) {
            //  nothing found before ||
            //  found but has more columns than this index||
            //  found but has fewer not null columns than this index
            briCols = cols;
            briColsCount = colsCount;
            nNullCount = nnullc;
        }
    }
    // remove rowID column from bestRowIdentiferCols
    bestRowIdentifierCols = briCols == null || briColsCount == briCols.length ? briCols : ArrayUtil.arraySlice(briCols, 0, briColsCount);
    bestRowIdentifierStrict = isStrict;
    // don't try to evaluate bestIndex for that case
    if (!(indexList.length == 0 && isView) && (indexList[0].getColumnCount() > 0)) {
        // End of VoltDB extension
        bestIndex = indexList[0];
    }
}
Also used : Index(org.hsqldb_voltpatches.index.Index)

Example 9 with Index

use of org.hsqldb_voltpatches.index.Index in project voltdb by VoltDB.

the class TableBase method createAndAddIndexStructure.

public final Index createAndAddIndexStructure(HsqlName name, int[] columns, boolean[] descending, boolean[] nullsLast, boolean unique, boolean constraint, boolean forward) {
    Index newindex = createIndexStructure(name, columns, descending, nullsLast, unique, constraint, forward);
    addIndex(newindex);
    return newindex;
}
Also used : Index(org.hsqldb_voltpatches.index.Index)

Example 10 with Index

use of org.hsqldb_voltpatches.index.Index in project voltdb by VoltDB.

the class TableBase method addIndex.

final void addIndex(Index index) {
    int i = 0;
    for (; i < indexList.length; i++) {
        Index current = indexList[i];
        int order = index.getIndexOrderValue() - current.getIndexOrderValue();
        if (order < 0) {
            break;
        }
    }
    indexList = (Index[]) ArrayUtil.toAdjustedArray(indexList, index, i, 1);
    for (i = 0; i < indexList.length; i++) {
        indexList[i].setPosition(i);
    }
    if (store != null) {
        try {
            store.resetAccessorKeys(indexList);
        } catch (HsqlException e) {
            indexList = (Index[]) ArrayUtil.toAdjustedArray(indexList, null, index.getPosition(), -1);
            for (i = 0; i < indexList.length; i++) {
                indexList[i].setPosition(i);
            }
            throw e;
        }
    }
    setBestRowIdentifiers();
}
Also used : Index(org.hsqldb_voltpatches.index.Index)

Aggregations

Index (org.hsqldb_voltpatches.index.Index)29 HsqlName (org.hsqldb_voltpatches.HsqlNameManager.HsqlName)6 RowIterator (org.hsqldb_voltpatches.navigator.RowIterator)4 PersistentStore (org.hsqldb_voltpatches.persist.PersistentStore)3 IndexAVL (org.hsqldb_voltpatches.index.IndexAVL)2 HashMappedList (org.hsqldb_voltpatches.lib.HashMappedList)2 HashMap (java.util.HashMap)1 HSQLParseException (org.hsqldb_voltpatches.HSQLInterface.HSQLParseException)1 HsqlArrayList (org.hsqldb_voltpatches.lib.HsqlArrayList)1 OrderedHashSet (org.hsqldb_voltpatches.lib.OrderedHashSet)1 Type (org.hsqldb_voltpatches.types.Type)1