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));
}
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));
}
Aggregations