Search in sources :

Example 26 with Index

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

the class TableWorks method dropIndex.

/**
     * Because of the way indexes and column data are held in memory and on
     * disk, it is necessary to recreate the table when an index is added to or
     * removed from a non-empty table.
     *
     * <p> Originally, this method would break existing foreign keys as the
     * table order in the DB was changed. The new table is now linked in place
     * of the old table (fredt@users)
     *
     * @param indexName String
     */
void dropIndex(String indexName) {
    Index index;
    index = table.getIndex(indexName);
    if (table.isIndexingMutable()) {
        table.dropIndex(session, indexName);
    } else {
        OrderedHashSet indexSet = new OrderedHashSet();
        indexSet.add(table.getIndex(indexName).getName());
        Table tn = table.moveDefinition(session, table.tableType, null, null, null, -1, 0, emptySet, indexSet);
        tn.moveData(session, table, -1, 0);
        updateConstraints(tn, emptySet);
        setNewTableInSchema(tn);
        database.persistentStoreCollection.releaseStore(table);
        table = tn;
    }
    if (!index.isConstraint()) {
        database.schemaManager.removeSchemaObject(index.getName());
    }
    database.schemaManager.recompileDependentObjects(table);
}
Also used : OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet) Index(org.hsqldb_voltpatches.index.Index)

Example 27 with Index

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

the class TableWorks method addForeignKey.

/**
     * Creates a foreign key on an existing table. Foreign keys are enforced by
     * indexes on both the referencing (child) and referenced (main) tables.
     *
     * <p> Since version 1.7.2, a unique constraint on the referenced columns
     * must exist. The non-unique index on the referencing table is now always
     * created whether or not a PK or unique constraint index on the columns
     * exist. Foriegn keys on temp tables can reference other temp tables with
     * the same rules above. Foreign keys on permanent tables cannot reference
     * temp tables. Duplicate foreign keys are now disallowed.
     *
     * @param c the constraint object
     */
void addForeignKey(Constraint c) {
    checkCreateForeignKey(c);
    Constraint uniqueConstraint = c.core.mainTable.getUniqueConstraintForColumns(c.core.mainCols, c.core.refCols);
    Index mainIndex = uniqueConstraint.getMainIndex();
    uniqueConstraint.checkReferencedRows(session, table, c.core.refCols);
    int offset = database.schemaManager.getTableIndex(table);
    boolean isForward = c.core.mainTable.getSchemaName() != table.getSchemaName();
    if (offset != -1 && offset < database.schemaManager.getTableIndex(c.core.mainTable)) {
        isForward = true;
    }
    HsqlName indexName = database.nameManager.newAutoName("IDX", table.getSchemaName(), table.getName(), SchemaObject.INDEX);
    Index refIndex = table.createIndexStructure(indexName, c.core.refCols, null, null, false, true, isForward);
    HsqlName mainName = database.nameManager.newAutoName("REF", c.getName().name, table.getSchemaName(), table.getName(), SchemaObject.INDEX);
    c.core.uniqueName = uniqueConstraint.getName();
    c.core.mainName = mainName;
    c.core.mainIndex = mainIndex;
    c.core.refTable = table;
    c.core.refName = c.getName();
    c.core.refIndex = refIndex;
    c.isForward = isForward;
    Table tn = table.moveDefinition(session, table.tableType, null, c, refIndex, -1, 0, emptySet, emptySet);
    tn.moveData(session, table, -1, 0);
    c.core.mainTable.addConstraint(new Constraint(mainName, c));
    database.schemaManager.addSchemaObject(c);
    database.persistentStoreCollection.releaseStore(table);
    setNewTableInSchema(tn);
    updateConstraints(tn, emptySet);
    database.schemaManager.recompileDependentObjects(tn);
    table = tn;
}
Also used : Index(org.hsqldb_voltpatches.index.Index) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName)

Example 28 with Index

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

the class Table method getIndexForColumns.

/**
     * Finds an existing index for a column set or create one for temporary
     * tables
     */
Index getIndexForColumns(OrderedIntHashSet set) {
    int maxMatchCount = 0;
    Index selected = null;
    if (set.isEmpty()) {
        return null;
    }
    for (int i = 0, count = indexList.length; i < count; i++) {
        Index currentindex = getIndex(i);
        int[] indexcols = currentindex.getColumns();
        int matchCount = set.getOrderedMatchCount(indexcols);
        if (matchCount == 0) {
            continue;
        }
        if (matchCount == indexcols.length) {
            return currentindex;
        }
        if (matchCount > maxMatchCount) {
            maxMatchCount = matchCount;
            selected = currentindex;
        }
    }
    if (selected != null) {
        return selected;
    }
    switch(tableType) {
        case TableBase.SYSTEM_SUBQUERY:
        case TableBase.SYSTEM_TABLE:
        case TableBase.VIEW_TABLE:
        case TableBase.TEMP_TABLE:
            {
                selected = createIndexForColumns(set.toArray());
            }
    }
    return selected;
}
Also used : Index(org.hsqldb_voltpatches.index.Index)

Example 29 with Index

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

the class Table method createAndAddExprIndexStructure.

// A VoltDB extension to support indexed expressions
public final Index createAndAddExprIndexStructure(HsqlName name, int[] columns, Expression[] indexExprs, boolean unique, boolean constraint) {
    Index newExprIndex = createIndexStructure(name, columns, null, null, unique, constraint, false);
    newExprIndex = newExprIndex.withExpressions(indexExprs);
    addIndex(newExprIndex);
    return newExprIndex;
}
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