Search in sources :

Example 1 with PageDelegateIndex

use of org.h2.index.PageDelegateIndex 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 2 with PageDelegateIndex

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

the class RegularTable method addIndex.

@Override
public Index addIndex(Session session, String indexName, int indexId, IndexColumn[] cols, IndexType indexType, boolean create, String indexComment) {
    if (indexType.isPrimaryKey()) {
        for (IndexColumn c : cols) {
            Column column = c.column;
            if (column.isNullable()) {
                throw DbException.get(ErrorCode.COLUMN_MUST_NOT_BE_NULLABLE_1, column.getName());
            }
            column.setPrimaryKey(true);
        }
    }
    boolean isSessionTemporary = isTemporary() && !isGlobalTemporary();
    if (!isSessionTemporary) {
        database.lockMeta(session);
    }
    Index index;
    if (isPersistIndexes() && indexType.isPersistent()) {
        int mainIndexColumn;
        if (database.isStarting() && database.getPageStore().getRootPageId(indexId) != 0) {
            mainIndexColumn = -1;
        } else if (!database.isStarting() && mainIndex.getRowCount(session) != 0) {
            mainIndexColumn = -1;
        } else {
            mainIndexColumn = getMainIndexColumn(indexType, cols);
        }
        if (mainIndexColumn != -1) {
            mainIndex.setMainIndexColumn(mainIndexColumn);
            index = new PageDelegateIndex(this, indexId, indexName, indexType, mainIndex, create, session);
        } else if (indexType.isSpatial()) {
            index = new SpatialTreeIndex(this, indexId, indexName, cols, indexType, true, create, session);
        } else {
            index = new PageBtreeIndex(this, indexId, indexName, cols, indexType, create, session);
        }
    } else {
        if (indexType.isHash()) {
            if (cols.length != 1) {
                throw DbException.getUnsupportedException("hash indexes may index only one column");
            }
            if (indexType.isUnique()) {
                index = new HashIndex(this, indexId, indexName, cols, indexType);
            } else {
                index = new NonUniqueHashIndex(this, indexId, indexName, cols, indexType);
            }
        } else if (indexType.isSpatial()) {
            index = new SpatialTreeIndex(this, indexId, indexName, cols, indexType, false, true, session);
        } else {
            index = new TreeIndex(this, indexId, indexName, cols, indexType);
        }
    }
    if (database.isMultiVersion()) {
        index = new MultiVersionIndex(index, this);
    }
    if (index.needRebuild() && rowCount > 0) {
        try {
            Index scan = getScanIndex(session);
            long remaining = scan.getRowCount(session);
            long total = remaining;
            Cursor cursor = scan.find(session, null, null);
            long i = 0;
            int bufferSize = (int) Math.min(rowCount, database.getMaxMemoryRows());
            ArrayList<Row> buffer = new ArrayList<>(bufferSize);
            String n = getName() + ":" + index.getName();
            int t = MathUtils.convertLongToInt(total);
            while (cursor.next()) {
                database.setProgress(DatabaseEventListener.STATE_CREATE_INDEX, n, MathUtils.convertLongToInt(i++), t);
                Row row = cursor.get();
                buffer.add(row);
                if (buffer.size() >= bufferSize) {
                    addRowsToIndex(session, buffer, index);
                }
                remaining--;
            }
            addRowsToIndex(session, buffer, index);
            if (SysProperties.CHECK && remaining != 0) {
                DbException.throwInternalError("rowcount remaining=" + remaining + " " + getName());
            }
        } catch (DbException e) {
            getSchema().freeUniqueName(indexName);
            try {
                index.remove(session);
            } catch (DbException e2) {
                // this could happen, for example on failure in the storage
                // but if that is not the case it means
                // there is something wrong with the database
                trace.error(e2, "could not remove index");
                throw e2;
            }
            throw e;
        }
    }
    index.setTemporary(isTemporary());
    if (index.getCreateSQL() != null) {
        index.setComment(indexComment);
        if (isSessionTemporary) {
            session.addLocalTempTableIndex(index);
        } else {
            database.addSchemaObject(session, index);
        }
    }
    indexes.add(index);
    setModified();
    return index;
}
Also used : SpatialTreeIndex(org.h2.index.SpatialTreeIndex) PageDelegateIndex(org.h2.index.PageDelegateIndex) NonUniqueHashIndex(org.h2.index.NonUniqueHashIndex) PageBtreeIndex(org.h2.index.PageBtreeIndex) ArrayList(java.util.ArrayList) NonUniqueHashIndex(org.h2.index.NonUniqueHashIndex) Index(org.h2.index.Index) HashIndex(org.h2.index.HashIndex) ScanIndex(org.h2.index.ScanIndex) PageBtreeIndex(org.h2.index.PageBtreeIndex) TreeIndex(org.h2.index.TreeIndex) PageDataIndex(org.h2.index.PageDataIndex) PageDelegateIndex(org.h2.index.PageDelegateIndex) MultiVersionIndex(org.h2.index.MultiVersionIndex) SpatialTreeIndex(org.h2.index.SpatialTreeIndex) NonUniqueHashIndex(org.h2.index.NonUniqueHashIndex) HashIndex(org.h2.index.HashIndex) Cursor(org.h2.index.Cursor) Constraint(org.h2.constraint.Constraint) DbException(org.h2.message.DbException) TreeIndex(org.h2.index.TreeIndex) SpatialTreeIndex(org.h2.index.SpatialTreeIndex) MultiVersionIndex(org.h2.index.MultiVersionIndex) Row(org.h2.result.Row)

Example 3 with PageDelegateIndex

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

the class PageStore method removeMeta.

private void removeMeta(Row row) {
    int id = row.getValue(0).getInt();
    PageIndex index = metaObjects.get(id);
    index.getTable().removeIndex(index);
    if (index instanceof PageBtreeIndex || index instanceof PageDelegateIndex) {
        if (index.isTemporary()) {
            pageStoreSession.removeLocalTempTableIndex(index);
        } else {
            index.getSchema().remove(index);
        }
    }
    index.remove(pageStoreSession);
    metaObjects.remove(id);
}
Also used : PageDelegateIndex(org.h2.index.PageDelegateIndex) PageBtreeIndex(org.h2.index.PageBtreeIndex) PageIndex(org.h2.index.PageIndex)

Aggregations

PageDelegateIndex (org.h2.index.PageDelegateIndex)3 PageBtreeIndex (org.h2.index.PageBtreeIndex)2 PageDataIndex (org.h2.index.PageDataIndex)2 Row (org.h2.result.Row)2 ArrayList (java.util.ArrayList)1 Constraint (org.h2.constraint.Constraint)1 Cursor (org.h2.index.Cursor)1 HashIndex (org.h2.index.HashIndex)1 Index (org.h2.index.Index)1 MultiVersionIndex (org.h2.index.MultiVersionIndex)1 NonUniqueHashIndex (org.h2.index.NonUniqueHashIndex)1 PageIndex (org.h2.index.PageIndex)1 ScanIndex (org.h2.index.ScanIndex)1 SpatialTreeIndex (org.h2.index.SpatialTreeIndex)1 TreeIndex (org.h2.index.TreeIndex)1 DbException (org.h2.message.DbException)1 IndexColumn (org.h2.table.IndexColumn)1 RegularTable (org.h2.table.RegularTable)1 Table (org.h2.table.Table)1 StatementBuilder (org.h2.util.StatementBuilder)1