Search in sources :

Example 1 with DropIndexesResult

use of com.torodb.mongodb.commands.signatures.admin.DropIndexesCommand.DropIndexesResult in project torodb by torodb.

the class DropIndexesImplementation method apply.

@Override
public Status<DropIndexesResult> apply(Request req, Command<? super DropIndexesArgument, ? super DropIndexesResult> command, DropIndexesArgument arg, WriteMongodTransaction context) {
    int indexesBefore = (int) context.getTorodTransaction().getIndexesInfo(req.getDatabase(), arg.getCollection()).count();
    List<String> indexesToDrop;
    if (!arg.isDropAllIndexes()) {
        if (!arg.isDropByKeys()) {
            if (Constants.ID_INDEX.equals(arg.getIndexToDrop())) {
                return Status.from(ErrorCode.INVALID_OPTIONS, "cannot drop _id index");
            }
            indexesToDrop = Arrays.asList(arg.getIndexToDrop());
        } else {
            if (arg.getKeys().stream().anyMatch(key -> !(KnownType.contains(key.getType())) || (key.getType() != KnownType.asc.getIndexType() && key.getType() != KnownType.desc.getIndexType()))) {
                return getStatusForIndexNotFoundWithKeys(arg);
            }
            indexesToDrop = context.getTorodTransaction().getIndexesInfo(req.getDatabase(), arg.getCollection()).filter(index -> indexFieldsMatchKeys(index, arg.getKeys())).map(index -> index.getName()).collect(Collectors.toList());
            if (indexesToDrop.isEmpty()) {
                return getStatusForIndexNotFoundWithKeys(arg);
            }
        }
    } else {
        indexesToDrop = context.getTorodTransaction().getIndexesInfo(req.getDatabase(), arg.getCollection()).filter(indexInfo -> !Constants.ID_INDEX.equals(indexInfo.getName())).map(indexInfo -> indexInfo.getName()).collect(Collectors.toList());
    }
    for (String indexToDrop : indexesToDrop) {
        boolean dropped = context.getTorodTransaction().dropIndex(req.getDatabase(), arg.getCollection(), indexToDrop);
        if (!dropped) {
            return Status.from(ErrorCode.INDEX_NOT_FOUND, "index not found with name [" + indexToDrop + "]");
        }
    }
    return Status.ok(new DropIndexesResult(indexesBefore));
}
Also used : Request(com.eightkdata.mongowp.server.api.Request) Arrays(java.util.Arrays) AttributeReference(com.torodb.core.language.AttributeReference) Constants(com.torodb.mongodb.language.Constants) Iterator(java.util.Iterator) DropIndexesResult(com.torodb.mongodb.commands.signatures.admin.DropIndexesCommand.DropIndexesResult) Command(com.eightkdata.mongowp.server.api.Command) WriteTorodbCommandImpl(com.torodb.mongodb.commands.impl.WriteTorodbCommandImpl) Collectors(java.util.stream.Collectors) WriteMongodTransaction(com.torodb.mongodb.core.WriteMongodTransaction) IndexFieldInfo(com.torodb.torod.IndexFieldInfo) List(java.util.List) Status(com.eightkdata.mongowp.Status) KnownType(com.torodb.mongodb.commands.pojos.index.IndexOptions.KnownType) ErrorCode(com.eightkdata.mongowp.ErrorCode) IndexInfo(com.torodb.torod.IndexInfo) IndexOptions(com.torodb.mongodb.commands.pojos.index.IndexOptions) DropIndexesArgument(com.torodb.mongodb.commands.signatures.admin.DropIndexesCommand.DropIndexesArgument) DropIndexesResult(com.torodb.mongodb.commands.signatures.admin.DropIndexesCommand.DropIndexesResult)

Example 2 with DropIndexesResult

use of com.torodb.mongodb.commands.signatures.admin.DropIndexesCommand.DropIndexesResult in project torodb by torodb.

the class DropIndexesReplImpl method apply.

@Override
public Status<DropIndexesResult> apply(Request req, Command<? super DropIndexesArgument, ? super DropIndexesResult> command, DropIndexesArgument arg, SharedWriteTorodTransaction trans) {
    int indexesBefore = (int) trans.getIndexesInfo(req.getDatabase(), arg.getCollection()).count();
    List<String> indexesToDrop;
    if (!arg.isDropAllIndexes()) {
        if (!arg.isDropByKeys()) {
            if (Constants.ID_INDEX.equals(arg.getIndexToDrop())) {
                LOGGER.warn("Trying to drop index {}. Ignoring the whole request", arg.getIndexToDrop());
                return Status.ok(new DropIndexesResult(indexesBefore));
            }
            indexesToDrop = Arrays.asList(arg.getIndexToDrop());
        } else {
            indexesToDrop = trans.getIndexesInfo(req.getDatabase(), arg.getCollection()).filter(index -> indexFieldsMatchKeys(index, arg.getKeys())).map(index -> index.getName()).collect(Collectors.toList());
            if (indexesToDrop.isEmpty()) {
                LOGGER.warn("Index not found with keys [" + arg.getKeys().stream().map(key -> '"' + key.getKeys().stream().collect(Collectors.joining(".")) + "\" :" + key.getType().getName()).collect(Collectors.joining(", ")) + "]. Ignoring the whole request", arg.getIndexToDrop());
                return Status.ok(new DropIndexesResult(indexesBefore));
            }
        }
    } else {
        indexesToDrop = trans.getIndexesInfo(req.getDatabase(), arg.getCollection()).filter(indexInfo -> !Constants.ID_INDEX.equals(indexInfo.getName())).map(indexInfo -> indexInfo.getName()).collect(Collectors.toList());
    }
    for (String indexToDrop : indexesToDrop) {
        LOGGER.info("Dropping index {} on collection {}.{}", req.getDatabase(), arg.getCollection(), indexToDrop);
        boolean dropped = trans.dropIndex(req.getDatabase(), arg.getCollection(), indexToDrop);
        if (!dropped) {
            LOGGER.info("Trying to drop index {}, but it has not been " + "found. This is normal since the index could have been filtered or " + "we are reapplying oplog during a recovery. Ignoring it", indexToDrop);
        }
    }
    return Status.ok(new DropIndexesResult(indexesBefore));
}
Also used : Request(com.eightkdata.mongowp.server.api.Request) SharedWriteTorodTransaction(com.torodb.torod.SharedWriteTorodTransaction) Arrays(java.util.Arrays) AttributeReference(com.torodb.core.language.AttributeReference) Constants(com.torodb.mongodb.language.Constants) Iterator(java.util.Iterator) DropIndexesResult(com.torodb.mongodb.commands.signatures.admin.DropIndexesCommand.DropIndexesResult) Command(com.eightkdata.mongowp.server.api.Command) Collectors(java.util.stream.Collectors) IndexFieldInfo(com.torodb.torod.IndexFieldInfo) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Status(com.eightkdata.mongowp.Status) KnownType(com.torodb.mongodb.commands.pojos.index.IndexOptions.KnownType) IndexInfo(com.torodb.torod.IndexInfo) IndexOptions(com.torodb.mongodb.commands.pojos.index.IndexOptions) DropIndexesArgument(com.torodb.mongodb.commands.signatures.admin.DropIndexesCommand.DropIndexesArgument) LogManager(org.apache.logging.log4j.LogManager) DropIndexesResult(com.torodb.mongodb.commands.signatures.admin.DropIndexesCommand.DropIndexesResult)

Aggregations

Status (com.eightkdata.mongowp.Status)2 Command (com.eightkdata.mongowp.server.api.Command)2 Request (com.eightkdata.mongowp.server.api.Request)2 AttributeReference (com.torodb.core.language.AttributeReference)2 IndexOptions (com.torodb.mongodb.commands.pojos.index.IndexOptions)2 KnownType (com.torodb.mongodb.commands.pojos.index.IndexOptions.KnownType)2 DropIndexesArgument (com.torodb.mongodb.commands.signatures.admin.DropIndexesCommand.DropIndexesArgument)2 DropIndexesResult (com.torodb.mongodb.commands.signatures.admin.DropIndexesCommand.DropIndexesResult)2 Constants (com.torodb.mongodb.language.Constants)2 IndexFieldInfo (com.torodb.torod.IndexFieldInfo)2 IndexInfo (com.torodb.torod.IndexInfo)2 Arrays (java.util.Arrays)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ErrorCode (com.eightkdata.mongowp.ErrorCode)1 WriteTorodbCommandImpl (com.torodb.mongodb.commands.impl.WriteTorodbCommandImpl)1 WriteMongodTransaction (com.torodb.mongodb.core.WriteMongodTransaction)1 SharedWriteTorodTransaction (com.torodb.torod.SharedWriteTorodTransaction)1 LogManager (org.apache.logging.log4j.LogManager)1