
io.mindmaps.graql.internal.query.DeleteQueryImpl Maven / Gradle / Ivy
/*
* MindmapsDB - A Distributed Semantic Database
* Copyright (C) 2016 Mindmaps Research Ltd
*
* MindmapsDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MindmapsDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MindmapsDB. If not, see .
*/
package io.mindmaps.graql.internal.query;
import com.google.common.collect.ImmutableMap;
import io.mindmaps.MindmapsGraph;
import io.mindmaps.concept.Concept;
import io.mindmaps.concept.Resource;
import io.mindmaps.exception.ConceptException;
import io.mindmaps.graql.DeleteQuery;
import io.mindmaps.graql.MatchQuery;
import io.mindmaps.graql.admin.DeleteQueryAdmin;
import io.mindmaps.graql.admin.MatchQueryAdmin;
import io.mindmaps.graql.admin.VarAdmin;
import io.mindmaps.graql.internal.validation.DeleteQueryValidator;
import io.mindmaps.util.ErrorMessage;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* A DeleteQuery that will execute deletions for every result of a MatchQuery
*/
class DeleteQueryImpl implements DeleteQueryAdmin {
private final ImmutableMap deleters;
private final MatchQueryAdmin matchQuery;
/**
* @param deleters a collection of variable patterns to delete
* @param matchQuery a pattern to match and delete for each result
*/
DeleteQueryImpl(Collection deleters, MatchQuery matchQuery) {
Map deletersMap =
deleters.stream().collect(Collectors.toMap(VarAdmin::getName, Function.identity()));
this.deleters = ImmutableMap.copyOf(deletersMap);
this.matchQuery = matchQuery.admin();
matchQuery.admin().getGraph().ifPresent(
graph -> new DeleteQueryValidator(this).validate(graph)
);
}
@Override
public Void execute() {
matchQuery.forEach(results -> results.forEach(this::deleteResult));
return null;
}
@Override
public Stream resultsString() {
execute();
return Stream.empty();
}
@Override
public boolean isReadOnly() {
return false;
}
@Override
public DeleteQuery withGraph(MindmapsGraph graph) {
return Queries.delete(deleters.values(), matchQuery.withGraph(graph));
}
@Override
public DeleteQueryAdmin admin() {
return this;
}
/**
* Delete a result from a query. This may involve deleting the whole concept or specific edges, depending
* on what deleters were provided.
* @param name the variable name to delete
* @param result the concept that matches the variable in the graph
*/
private void deleteResult(String name, Concept result) {
VarAdmin deleter = deleters.get(name);
// Check if this has been requested to be deleted
if (deleter == null) return;
String id = result.getId();
if (deleter.hasNoProperties()) {
// Delete whole concept if nothing specified to delete
deleteConcept(id);
} else {
deleter.getHasRoles().forEach(
role -> role.getId().ifPresent(
typeName -> getGraph().getRelationType(id).deleteHasRole(getGraph().getRoleType(typeName))
)
);
deleter.getPlaysRoles().forEach(
role -> role.getId().ifPresent(
typeName -> getGraph().getType(id).deletePlaysRole(getGraph().getRoleType(typeName))
)
);
deleter.getScopes().forEach(
scope -> scope.getId().ifPresent(
scopeName -> getGraph().getRelation(id).deleteScope(getGraph().getInstance(scopeName))
)
);
deleter.getResourcePredicates().forEach((type, predicates) -> {
Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy