Search in sources :

Example 1 with MetaIdentifiedDocPartIndex

use of com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex in project torodb by torodb.

the class SharedWriteBackendTransactionImpl method dropIndex.

@Override
public void dropIndex(MetaDatabase db, MutableMetaCollection col, MetaIndex index) {
    Preconditions.checkState(!isClosed(), "This transaction is closed");
    getSqlInterface().getMetaDataWriteInterface().deleteMetaIndex(getDsl(), db, col, index);
    Iterator<TableRef> tableRefIterator = index.streamTableRefs().iterator();
    while (tableRefIterator.hasNext()) {
        TableRef tableRef = tableRefIterator.next();
        MutableMetaDocPart docPart = col.getMetaDocPartByTableRef(tableRef);
        if (docPart != null) {
            Iterator<? extends MetaIdentifiedDocPartIndex> docPartIndexesIterator = docPart.streamIndexes().iterator();
            while (docPartIndexesIterator.hasNext()) {
                MetaIdentifiedDocPartIndex docPartIndex = docPartIndexesIterator.next();
                if (index.isCompatible(docPart, docPartIndex)) {
                    boolean existsAnyOtherCompatibleIndex = col.streamContainedMetaIndexes().anyMatch(otherIndex -> otherIndex != index && otherIndex.isCompatible(docPart, docPartIndex));
                    if (!existsAnyOtherCompatibleIndex) {
                        dropIndex(db, col, docPart, docPartIndex);
                        LOGGER.info("Dropped index {} for table {}", docPartIndex.getIdentifier(), docPart.getIdentifier());
                    }
                }
            }
        }
    }
}
Also used : MetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex) MutableMetaDocPart(com.torodb.core.transaction.metainf.MutableMetaDocPart) TableRef(com.torodb.core.TableRef)

Example 2 with MetaIdentifiedDocPartIndex

use of com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex in project torodb by torodb.

the class SharedWriteBackendTransactionImpl method addField.

@Override
public void addField(MetaDatabase db, MetaCollection col, MutableMetaDocPart docPart, MetaField newField) throws UserException {
    Preconditions.checkState(!isClosed(), "This transaction is closed");
    getSqlInterface().getMetaDataWriteInterface().addMetaField(getDsl(), db, col, docPart, newField);
    getSqlInterface().getStructureInterface().addColumnToDocPartTable(getDsl(), db.getIdentifier(), docPart.getIdentifier(), newField.getIdentifier(), getSqlInterface().getDataTypeProvider().getDataType(newField.getType()));
    List<Tuple2<MetaIndex, List<String>>> missingIndexes = col.getMissingIndexesForNewField(docPart, newField);
    for (Tuple2<MetaIndex, List<String>> missingIndexEntry : missingIndexes) {
        MetaIndex missingIndex = missingIndexEntry.v1();
        List<String> identifiers = missingIndexEntry.v2();
        MutableMetaDocPartIndex docPartIndex = docPart.getOrCreatePartialMutableDocPartIndexForMissingIndexAndNewField(missingIndex, identifiers, newField);
        if (missingIndex.isMatch(docPart, identifiers, docPartIndex)) {
            List<Tuple2<String, Boolean>> columnList = new ArrayList<>(docPartIndex.size());
            for (String identifier : identifiers) {
                MetaDocPartIndexColumn docPartIndexColumn = docPartIndex.getMetaDocPartIndexColumnByIdentifier(identifier);
                columnList.add(new Tuple2<>(docPartIndexColumn.getIdentifier(), docPartIndexColumn.getOrdering().isAscending()));
            }
            MetaIdentifiedDocPartIndex identifiedDocPartIndex = docPartIndex.immutableCopy(identifierFactory.toIndexIdentifier(db, docPart.getIdentifier(), columnList));
            getSqlInterface().getMetaDataWriteInterface().addMetaDocPartIndex(getDsl(), db, col, docPart, identifiedDocPartIndex);
            for (String identifier : identifiers) {
                MetaDocPartIndexColumn docPartIndexColumn = docPartIndex.getMetaDocPartIndexColumnByIdentifier(identifier);
                getSqlInterface().getMetaDataWriteInterface().addMetaDocPartIndexColumn(getDsl(), db, col, docPart, identifiedDocPartIndex, docPartIndexColumn);
            }
            getSqlInterface().getStructureInterface().createIndex(getDsl(), identifiedDocPartIndex.getIdentifier(), db.getIdentifier(), docPart.getIdentifier(), columnList, docPartIndex.isUnique());
            LOGGER.info("Created index {} for table {} associated to logical index {}.{}.{}", identifiedDocPartIndex.getIdentifier(), docPart.getIdentifier(), db.getName(), col.getName(), missingIndex.getName());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MetaIndex(com.torodb.core.transaction.metainf.MetaIndex) MetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex) Tuple2(org.jooq.lambda.tuple.Tuple2) ArrayList(java.util.ArrayList) List(java.util.List) MutableMetaDocPartIndex(com.torodb.core.transaction.metainf.MutableMetaDocPartIndex) MetaDocPartIndexColumn(com.torodb.core.transaction.metainf.MetaDocPartIndexColumn)

Example 3 with MetaIdentifiedDocPartIndex

use of com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex in project torodb by torodb.

the class AbstractMetaDataReadInterface method getIndexSize.

@Override
public Long getIndexSize(@Nonnull DSLContext dsl, @Nonnull MetaDatabase database, @Nonnull MetaCollection collection, @Nonnull String indexName) {
    long result = 0;
    MetaIndex index = collection.getMetaIndexByName(indexName);
    Iterator<TableRef> tableRefIterator = index.streamTableRefs().iterator();
    while (tableRefIterator.hasNext()) {
        TableRef tableRef = tableRefIterator.next();
        MetaDocPart docPart = collection.getMetaDocPartByTableRef(tableRef);
        Iterator<? extends MetaIdentifiedDocPartIndex> docPartIndexIterator = docPart.streamIndexes().iterator();
        while (docPartIndexIterator.hasNext()) {
            MetaIdentifiedDocPartIndex docPartIndex = docPartIndexIterator.next();
            if (index.isCompatible(docPart, docPartIndex)) {
                long relatedIndexCount = collection.streamContainedMetaIndexes().filter(i -> i.isCompatible(docPart, docPartIndex)).count();
                String statement = getReadIndexSizeStatement(database.getIdentifier(), docPart.getIdentifier(), docPartIndex.getIdentifier());
                result += sqlHelper.executeStatementWithResult(dsl, statement, Context.FETCH).get(0).into(Long.class) / relatedIndexCount;
            }
        }
    }
    return result;
}
Also used : MetaIndex(com.torodb.core.transaction.metainf.MetaIndex) KvTable(com.torodb.backend.tables.KvTable) Singleton(javax.inject.Singleton) Condition(org.jooq.Condition) MetaDocPartTable(com.torodb.backend.tables.MetaDocPartTable) MetaIndex(com.torodb.core.transaction.metainf.MetaIndex) Inject(javax.inject.Inject) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) Record1(org.jooq.Record1) DSLContext(org.jooq.DSLContext) Context(com.torodb.backend.ErrorHandler.Context) Nonnull(javax.annotation.Nonnull) MetaCollection(com.torodb.core.transaction.metainf.MetaCollection) Record(org.jooq.Record) Iterator(java.util.Iterator) KvRecord(com.torodb.backend.tables.records.KvRecord) Collection(java.util.Collection) MetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex) Result(org.jooq.Result) TableRef(com.torodb.core.TableRef) Stream(java.util.stream.Stream) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) MetaInfoKey(com.torodb.core.backend.MetaInfoKey) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) MetaDatabaseRecord(com.torodb.backend.tables.records.MetaDatabaseRecord) MetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) TableRef(com.torodb.core.TableRef)

Example 4 with MetaIdentifiedDocPartIndex

use of com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex in project torodb by torodb.

the class AbstractStructureInterface method renameCollection.

@Override
public void renameCollection(DSLContext dsl, String fromSchemaName, MetaCollection fromCollection, String toSchemaName, MetaCollection toCollection) {
    Iterator<? extends MetaDocPart> metaDocPartIterator = fromCollection.streamContainedMetaDocParts().iterator();
    while (metaDocPartIterator.hasNext()) {
        MetaDocPart fromMetaDocPart = metaDocPartIterator.next();
        MetaDocPart toMetaDocPart = toCollection.getMetaDocPartByTableRef(fromMetaDocPart.getTableRef());
        String renameStatement = getRenameTableStatement(fromSchemaName, fromMetaDocPart.getIdentifier(), toMetaDocPart.getIdentifier());
        sqlHelper.executeUpdate(dsl, renameStatement, Context.RENAME_TABLE);
        Iterator<? extends MetaIdentifiedDocPartIndex> metaDocPartIndexIterator = fromMetaDocPart.streamIndexes().iterator();
        while (metaDocPartIndexIterator.hasNext()) {
            MetaIdentifiedDocPartIndex fromMetaIndex = metaDocPartIndexIterator.next();
            MetaIdentifiedDocPartIndex toMetaIndex = toMetaDocPart.streamIndexes().filter(index -> index.hasSameColumns(fromMetaIndex)).findAny().get();
            String renameIndexStatement = getRenameIndexStatement(fromSchemaName, fromMetaIndex.getIdentifier(), toMetaIndex.getIdentifier());
            sqlHelper.executeUpdate(dsl, renameIndexStatement, Context.RENAME_INDEX);
        }
        if (!fromSchemaName.equals(toSchemaName)) {
            String setSchemaStatement = getSetTableSchemaStatement(fromSchemaName, fromMetaDocPart.getIdentifier(), toSchemaName);
            sqlHelper.executeUpdate(dsl, setSchemaStatement, Context.SET_TABLE_SCHEMA);
        }
    }
}
Also used : MetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart)

Example 5 with MetaIdentifiedDocPartIndex

use of com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex in project torodb by torodb.

the class BackendServiceImpl method enableIndexJobs.

private Stream<Consumer<DSLContext>> enableIndexJobs(MetaDatabase db, MetaCollection col) {
    List<Consumer<DSLContext>> consumerList = new ArrayList<>();
    Iterator<? extends MetaDocPart> docPartIterator = col.streamContainedMetaDocParts().iterator();
    while (docPartIterator.hasNext()) {
        MetaDocPart docPart = docPartIterator.next();
        Iterator<? extends MetaIdentifiedDocPartIndex> docPartIndexIterator = docPart.streamIndexes().iterator();
        while (docPartIndexIterator.hasNext()) {
            MetaIdentifiedDocPartIndex docPartIndex = docPartIndexIterator.next();
            consumerList.add(createIndexJob(db, docPart, docPartIndex));
        }
    }
    return consumerList.stream();
}
Also used : MetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex) Consumer(java.util.function.Consumer) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) ArrayList(java.util.ArrayList)

Aggregations

MetaIdentifiedDocPartIndex (com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex)7 MetaDocPart (com.torodb.core.transaction.metainf.MetaDocPart)3 MutableMetaDocPartIndex (com.torodb.core.transaction.metainf.MutableMetaDocPartIndex)3 ArrayList (java.util.ArrayList)3 Tuple2 (org.jooq.lambda.tuple.Tuple2)3 TableRef (com.torodb.core.TableRef)2 MetaDocPartIndexColumn (com.torodb.core.transaction.metainf.MetaDocPartIndexColumn)2 MetaIndex (com.torodb.core.transaction.metainf.MetaIndex)2 Preconditions (com.google.common.base.Preconditions)1 Context (com.torodb.backend.ErrorHandler.Context)1 KvTable (com.torodb.backend.tables.KvTable)1 MetaDocPartTable (com.torodb.backend.tables.MetaDocPartTable)1 KvRecord (com.torodb.backend.tables.records.KvRecord)1 MetaDatabaseRecord (com.torodb.backend.tables.records.MetaDatabaseRecord)1 MetaInfoKey (com.torodb.core.backend.MetaInfoKey)1 MetaCollection (com.torodb.core.transaction.metainf.MetaCollection)1 MetaDatabase (com.torodb.core.transaction.metainf.MetaDatabase)1 MetaIndexField (com.torodb.core.transaction.metainf.MetaIndexField)1 MutableMetaDocPart (com.torodb.core.transaction.metainf.MutableMetaDocPart)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1