/
Deleter.java
191 lines (167 loc) · 9.2 KB
/
Deleter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (C) 2021 Vaticle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.vaticle.typedb.core.query;
import com.vaticle.factory.tracing.client.FactoryTracingThreadStatic;
import com.vaticle.typedb.common.collection.Either;
import com.vaticle.typedb.core.common.exception.ErrorMessage;
import com.vaticle.typedb.core.common.exception.TypeDBException;
import com.vaticle.typedb.core.common.parameters.Context;
import com.vaticle.typedb.core.common.parameters.Label;
import com.vaticle.typedb.core.concept.answer.ConceptMap;
import com.vaticle.typedb.core.concept.thing.Attribute;
import com.vaticle.typedb.core.concept.thing.Relation;
import com.vaticle.typedb.core.concept.thing.Thing;
import com.vaticle.typedb.core.concept.type.RoleType;
import com.vaticle.typedb.core.concept.type.ThingType;
import com.vaticle.typedb.core.pattern.constraint.thing.HasConstraint;
import com.vaticle.typedb.core.pattern.variable.ThingVariable;
import com.vaticle.typedb.core.pattern.variable.TypeVariable;
import com.vaticle.typedb.core.pattern.variable.Variable;
import com.vaticle.typedb.core.pattern.variable.VariableRegistry;
import com.vaticle.typedb.core.reasoner.Reasoner;
import com.vaticle.typeql.lang.pattern.variable.Reference;
import com.vaticle.typeql.lang.query.TypeQLDelete;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.vaticle.factory.tracing.client.FactoryTracingThreadStatic.traceOnThread;
import static com.vaticle.typedb.core.common.exception.ErrorMessage.ThingWrite.DELETE_RELATION_CONSTRAINT_TOO_MANY;
import static com.vaticle.typedb.core.common.exception.ErrorMessage.ThingWrite.ILLEGAL_ANONYMOUS_RELATION_IN_DELETE;
import static com.vaticle.typedb.core.common.exception.ErrorMessage.ThingWrite.ILLEGAL_ANONYMOUS_VARIABLE_IN_DELETE;
import static com.vaticle.typedb.core.common.exception.ErrorMessage.ThingWrite.ILLEGAL_IS_CONSTRAINT;
import static com.vaticle.typedb.core.common.exception.ErrorMessage.ThingWrite.ILLEGAL_TYPE_VARIABLE_IN_DELETE;
import static com.vaticle.typedb.core.common.exception.ErrorMessage.ThingWrite.INVALID_DELETE_HAS;
import static com.vaticle.typedb.core.common.exception.ErrorMessage.ThingWrite.INVALID_DELETE_THING;
import static com.vaticle.typedb.core.common.exception.ErrorMessage.ThingWrite.INVALID_DELETE_THING_DIRECT;
import static com.vaticle.typedb.core.common.exception.ErrorMessage.ThingWrite.THING_IID_NOT_INSERTABLE;
import static com.vaticle.typedb.core.common.iterator.Iterators.iterate;
import static com.vaticle.typedb.core.common.parameters.Arguments.Query.Producer.EXHAUSTIVE;
import static com.vaticle.typedb.core.query.common.Util.getRoleType;
public class Deleter {
private static final String TRACE_PREFIX = "deleter.";
private final Matcher matcher;
private final Set<ThingVariable> variables;
private final Context.Query context;
public Deleter(Matcher matcher, Set<ThingVariable> variables, Context.Query context) {
this.matcher = matcher;
this.variables = variables;
this.context = context;
this.context.producer(Either.first(EXHAUSTIVE));
}
public static Deleter create(Reasoner reasoner, TypeQLDelete query, Context.Query context) {
try (FactoryTracingThreadStatic.ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "create")) {
VariableRegistry registry = VariableRegistry.createFromThings(query.variables(), false);
registry.variables().forEach(Deleter::validate);
assert query.match().namedVariablesUnbound().containsAll(query.namedVariablesUnbound());
Matcher matcher = Matcher.create(reasoner, query.match().get(query.namedVariablesUnbound()));
return new Deleter(matcher, registry.things(), context);
}
}
public static void validate(Variable var) {
try (FactoryTracingThreadStatic.ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "validate")) {
if (var.isType()) validate(var.asType());
else validate(var.asThing());
}
}
private static void validate(TypeVariable var) {
if (!var.reference().isLabel()) throw TypeDBException.of(ILLEGAL_TYPE_VARIABLE_IN_DELETE, var.reference());
}
private static void validate(ThingVariable var) {
if (!var.reference().isName()) {
ErrorMessage.ThingWrite msg = var.relation().isPresent()
? ILLEGAL_ANONYMOUS_RELATION_IN_DELETE
: ILLEGAL_ANONYMOUS_VARIABLE_IN_DELETE;
throw TypeDBException.of(msg, var);
} else if (var.iid().isPresent()) {
throw TypeDBException.of(THING_IID_NOT_INSERTABLE, var.reference(), var.iid().get());
} else if (!var.is().isEmpty()) {
throw TypeDBException.of(ILLEGAL_IS_CONSTRAINT, var, var.is().iterator().next());
}
}
public void execute() {
try (FactoryTracingThreadStatic.ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "execute")) {
List<ConceptMap> matches = matcher.execute(context).toList();
matches.forEach(matched -> new Operation(matched, variables).execute());
}
}
static class Operation {
private static final String TRACE_PREFIX = "operation.";
private final ConceptMap matched;
private final Set<ThingVariable> variables;
private final Map<ThingVariable, Thing> detached;
Operation(ConceptMap matched, Set<ThingVariable> variables) {
this.matched = matched;
this.variables = variables;
this.detached = new HashMap<>();
}
void execute() {
try (FactoryTracingThreadStatic.ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "execute")) {
variables.forEach(this::delete);
variables.forEach(this::deleteIsa);
}
}
private void delete(ThingVariable var) {
try (FactoryTracingThreadStatic.ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "delete")) {
validate(var);
Thing thing = matched.get(var.reference().asName()).asThing();
if (!var.has().isEmpty()) deleteHas(var, thing);
if (var.relation().isPresent()) deleteRelation(var, thing.asRelation());
detached.put(var, thing);
}
}
private void deleteHas(ThingVariable var, Thing thing) {
try (FactoryTracingThreadStatic.ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "delete_has")) {
for (HasConstraint hasConstraint : var.has()) {
Reference.Name attRef = hasConstraint.attribute().reference().asName();
Attribute att = matched.get(attRef).asAttribute();
if (thing.getHas(att.getType()).anyMatch(a -> a.equals(att))) thing.unsetHas(att);
else throw TypeDBException.of(INVALID_DELETE_HAS, var.reference(), attRef);
}
}
}
private void deleteRelation(ThingVariable var, Relation relation) {
try (FactoryTracingThreadStatic.ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "delete_relation")) {
if (var.relation().isPresent()) {
var.relation().get().players().forEach(rolePlayer -> {
Thing player = matched.get(rolePlayer.player().reference().asName()).asThing();
RoleType roleType = getRoleType(relation, player, rolePlayer);
relation.removePlayer(roleType, player);
});
} else {
throw TypeDBException.of(DELETE_RELATION_CONSTRAINT_TOO_MANY, var.reference());
}
}
}
private void deleteIsa(ThingVariable var) {
try (FactoryTracingThreadStatic.ThreadTrace ignored = traceOnThread(TRACE_PREFIX + "delete_isa")) {
Thing thing = detached.get(var);
ThingType type = thing.getType();
if (var.isa().isPresent() && !thing.isDeleted()) {
Label typeLabel = var.isa().get().type().label().get().properLabel();
if (var.isa().get().isExplicit()) {
if (type.getLabel().equals(typeLabel)) thing.delete();
else throw TypeDBException.of(INVALID_DELETE_THING_DIRECT, var.reference(), typeLabel);
} else {
if (type.getSupertypes().anyMatch(t -> t.getLabel().equals(typeLabel))) thing.delete();
else throw TypeDBException.of(INVALID_DELETE_THING, var.reference(), typeLabel);
}
}
}
}
}
}