Skip to content

Commit 13b4902

Browse files
committed
cleanup and a bit of performance for the possible mapping calculator
1 parent 89edd9a commit 13b4902

1 file changed

Lines changed: 113 additions & 83 deletions

File tree

src/main/java/graphql/schema/diffing/PossibleMappingsCalculator.java

Lines changed: 113 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package graphql.schema.diffing;
22

33
import com.google.common.collect.BiMap;
4-
import com.google.common.collect.HashBasedTable;
54
import com.google.common.collect.HashBiMap;
65
import com.google.common.collect.HashMultimap;
76
import com.google.common.collect.ImmutableList;
87
import com.google.common.collect.Multimap;
9-
import com.google.common.collect.Table;
108
import graphql.Assert;
119
import graphql.Internal;
1210
import graphql.util.FpKit;
@@ -791,63 +789,119 @@ public static class PossibleMappings {
791789
public Set<Vertex> allIsolatedSource = new LinkedHashSet<>();
792790
public Set<Vertex> allIsolatedTarget = new LinkedHashSet<>();
793791

794-
public Table<List<String>, Set<Vertex>, Set<Vertex>> contexts = HashBasedTable.create();
792+
// public Table<List<String>, Set<Vertex>, Set<Vertex>> contexts = HashBasedTable.create();
795793

796794
public Multimap<Vertex, Vertex> possibleMappings = HashMultimap.create();
797795

798796
public BiMap<Vertex, Vertex> fixedOneToOneMappings = HashBiMap.create();
799797
public List<Vertex> fixedOneToOneSources = new ArrayList<>();
800798
public List<Vertex> fixedOneToOneTargets = new ArrayList<>();
801799

802-
public void putPossibleMappings(Collection<Vertex> sourceVertices, Collection<Vertex> targetVertex) {
803-
for (Vertex sourceVertex : sourceVertices) {
804-
possibleMappings.putAll(sourceVertex, targetVertex);
800+
public void putPossibleMappings(List<String> contextId,
801+
Collection<Vertex> sourceVertices,
802+
Collection<Vertex> targetVertices,
803+
String typeName) {
804+
if (sourceVertices.isEmpty() && targetVertices.isEmpty()) {
805+
return;
805806
}
806-
}
807+
if (sourceVertices.size() == 1 && targetVertices.size() == 1) {
808+
Vertex sourceVertex = sourceVertices.iterator().next();
809+
Vertex targetVertex = targetVertices.iterator().next();
810+
fixedOneToOneMappings.put(sourceVertex, targetVertex);
811+
fixedOneToOneSources.add(sourceVertex);
812+
fixedOneToOneTargets.add(targetVertex);
813+
return;
814+
}
815+
816+
// System.out.println("non trivial context " + contextId);
817+
818+
// TODO: add islated source and target to allIsolatedSource and
819+
// allIsolatedTarget and also update the mapping
820+
Set<Vertex> newIsolatedSource = Collections.emptySet();
821+
Set<Vertex> newIsolatedTarget = Collections.emptySet();
822+
if (sourceVertices.size() > targetVertices.size()) {
823+
newIsolatedTarget = Vertex.newIsolatedNodes(sourceVertices.size() - targetVertices.size(), "target-isolated-" + typeName + "-");
824+
} else if (targetVertices.size() > sourceVertices.size()) {
825+
newIsolatedSource = Vertex.newIsolatedNodes(targetVertices.size() - sourceVertices.size(), "source-isolated-" + typeName + "-");
826+
}
827+
this.allIsolatedSource.addAll(newIsolatedSource);
828+
this.allIsolatedTarget.addAll(newIsolatedTarget);
829+
830+
if (sourceVertices.size() == 0) {
831+
Iterator<Vertex> iterator = newIsolatedSource.iterator();
832+
for (Vertex targetVertex : targetVertices) {
833+
Vertex isolatedSourceVertex = iterator.next();
834+
fixedOneToOneMappings.put(isolatedSourceVertex, targetVertex);
835+
fixedOneToOneSources.add(isolatedSourceVertex);
836+
fixedOneToOneTargets.add(targetVertex);
837+
}
838+
return;
839+
}
840+
if (targetVertices.size() == 0) {
841+
Iterator<Vertex> iterator = newIsolatedTarget.iterator();
842+
for (Vertex sourceVertex : sourceVertices) {
843+
Vertex isolatedTargetVertex = iterator.next();
844+
fixedOneToOneMappings.put(sourceVertex, isolatedTargetVertex);
845+
fixedOneToOneSources.add(sourceVertex);
846+
fixedOneToOneTargets.add(isolatedTargetVertex);
847+
}
848+
return;
849+
}
850+
// if ((APPLIED_DIRECTIVE.equals(typeName) || APPLIED_ARGUMENT.equals(typeName)) && sourceVertices.size() > 1) {
851+
// for (Vertex sourceVertex : sourceVertices) {
852+
// if (sourceVertex.isIsolated()) {
853+
// continue;
854+
// }
855+
// Vertex targetVertex = Vertex.newIsolatedNode("target-isolated-" + typeName);
856+
// fixedOneToOneMappings.put(sourceVertex, targetVertex);
857+
// fixedOneToOneSources.add(sourceVertex);
858+
// fixedOneToOneTargets.add(targetVertex);
859+
// }
860+
// for (Vertex targetVertex : targetVertices) {
861+
// if (targetVertex.isIsolated()) {
862+
// continue;
863+
// }
864+
// Vertex sourceVertex = Vertex.newIsolatedNode("source-isolated-" + typeName);
865+
// fixedOneToOneMappings.put(sourceVertex, targetVertex);
866+
// fixedOneToOneSources.add(sourceVertex);
867+
// fixedOneToOneTargets.add(targetVertex);
868+
// }
869+
// return;
870+
// }
807871

808-
public void addIsolatedSource(Collection<Vertex> isolatedSource) {
809-
allIsolatedSource.addAll(isolatedSource);
810-
}
872+
for (Vertex sourceVertex : sourceVertices) {
873+
possibleMappings.putAll(sourceVertex, targetVertices);
874+
possibleMappings.putAll(sourceVertex, newIsolatedTarget);
875+
}
876+
for (Vertex sourceIsolatedVertex : newIsolatedSource) {
877+
possibleMappings.putAll(sourceIsolatedVertex, targetVertices);
878+
possibleMappings.putAll(sourceIsolatedVertex, newIsolatedTarget);
879+
}
811880

812-
public void addIsolatedTarget(Collection<Vertex> isolatedTarget) {
813-
allIsolatedTarget.addAll(isolatedTarget);
814881
}
815882

816883
//
817884
public boolean mappingPossible(Vertex sourceVertex, Vertex targetVertex) {
818885
return possibleMappings.containsEntry(sourceVertex, targetVertex);
819886
}
820-
821-
public void putContext(List<String> contextId, Set<Vertex> source, Set<Vertex> target) {
822-
if (contexts.containsRow(contextId)) {
823-
throw new IllegalArgumentException("Already context " + contextId);
824-
}
825-
Assert.assertTrue(source.size() == target.size());
826-
if (source.size() == 1) {
827-
Vertex sourceVertex = source.iterator().next();
828-
Vertex targetVertex = target.iterator().next();
829-
fixedOneToOneMappings.put(sourceVertex, targetVertex);
830-
fixedOneToOneSources.add(sourceVertex);
831-
fixedOneToOneTargets.add(targetVertex);
832-
833-
} else {
834-
if (target.stream().allMatch(Vertex::isIsolated) || source.stream().allMatch(Vertex::isIsolated)) {
835-
Iterator<Vertex> iterator = target.iterator();
836-
for (Vertex sourceVertex : source) {
837-
Vertex targetVertex = iterator.next();
838-
fixedOneToOneMappings.put(sourceVertex, targetVertex);
839-
fixedOneToOneSources.add(sourceVertex);
840-
fixedOneToOneTargets.add(targetVertex);
841-
}
842-
} else {
843-
// System.out.println("multiple elements in context" + contextId + " with count: " + source.size());
844-
// System.out.println("sources: " + source);
845-
// System.out.println("target: " + target);
846-
}
847-
}
848-
contexts.put(contextId, source, target);
849-
}
850-
887+
//
888+
// public void putContext(List<String> contextId, Set<Vertex> source, Set<Vertex> target) {
889+
//// if (contexts.containsRow(contextId)) {
890+
//// throw new IllegalArgumentException("Already context " + contextId);
891+
//// }
892+
// Assert.assertTrue(source.size() == target.size());
893+
//// if (source.size() == 1) {
894+
//// Vertex sourceVertex = source.iterator().next();
895+
//// Vertex targetVertex = target.iterator().next();
896+
//// fixedOneToOneMappings.put(sourceVertex, targetVertex);
897+
//// fixedOneToOneSources.add(sourceVertex);
898+
//// fixedOneToOneTargets.add(targetVertex);
899+
////
900+
// System.out.println("multiple elements in context" + contextId + " with count: " + source.size());
901+
////// System.out.println("sources: " + source);
902+
////// System.out.println("target: " + target);
903+
//// }
904+
// }
851905
}
852906

853907

@@ -910,35 +964,14 @@ private void calcPossibleMappingImpl(
910964
Set<Vertex> notUsedTarget = new LinkedHashSet<>(targetVerticesInContext);
911965
notUsedTarget.removeAll(usedTargetVertices);
912966

913-
// make sure the current context is the same size
914-
if (notUsedSource.size() > notUsedTarget.size()) {
915-
Set<Vertex> newTargetVertices = Vertex.newIsolatedNodes(notUsedSource.size() - notUsedTarget.size(), "target-isolated-" + typeNameForDebug + "-");
916-
possibleMappings.addIsolatedTarget(newTargetVertices);
917-
notUsedTarget.addAll(newTargetVertices);
918-
} else if (notUsedTarget.size() > notUsedSource.size()) {
919-
Set<Vertex> newSourceVertices = Vertex.newIsolatedNodes(notUsedTarget.size() - notUsedSource.size(), "source-isolated-" + typeNameForDebug + "-");
920-
possibleMappings.addIsolatedSource(newSourceVertices);
921-
notUsedSource.addAll(newSourceVertices);
922-
}
923-
possibleMappings.putPossibleMappings(notUsedSource, notUsedTarget);
967+
possibleMappings.putPossibleMappings(currentContextId, notUsedSource, notUsedTarget, typeNameForDebug);
924968
usedSourceVertices.addAll(notUsedSource);
925969
usedTargetVertices.addAll(notUsedTarget);
926-
if (notUsedSource.size() > 0) {
927-
possibleMappings.putContext(currentContextId, notUsedSource, notUsedTarget);
928-
}
929-
}
930-
931-
Set<Vertex> possibleTargetVertices = new LinkedHashSet<>();
932-
for (String insertedContext : insertedContexts) {
933-
ImmutableList<Vertex> vertices = targetGroups.get(insertedContext);
934-
for (Vertex targetVertex : vertices) {
935-
if (!usedTargetVertices.contains(targetVertex)) {
936-
possibleTargetVertices.add(targetVertex);
937-
}
938-
}
939-
usedTargetVertices.addAll(vertices);
940970
}
941971

972+
/**
973+
* update the used vertices with the deleted and inserted contexts
974+
*/
942975
Set<Vertex> possibleSourceVertices = new LinkedHashSet<>();
943976
for (String deletedContext : deletedContexts) {
944977
ImmutableList<Vertex> vertices = sourceGroups.get(deletedContext);
@@ -950,23 +983,20 @@ private void calcPossibleMappingImpl(
950983
usedSourceVertices.addAll(vertices);
951984
}
952985

953-
if (possibleSourceVertices.size() > possibleTargetVertices.size()) {
954-
Set<Vertex> newTargetVertices = Vertex.newIsolatedNodes(possibleSourceVertices.size() - possibleTargetVertices.size(), "target-isolated-" + typeNameForDebug + "-");
955-
possibleMappings.addIsolatedTarget(newTargetVertices);
956-
possibleTargetVertices.addAll(newTargetVertices);
957-
} else if (possibleTargetVertices.size() > possibleSourceVertices.size()) {
958-
Set<Vertex> newSourceVertices = Vertex.newIsolatedNodes(possibleTargetVertices.size() - possibleSourceVertices.size(), "source-isolated-" + typeNameForDebug + "-");
959-
possibleMappings.addIsolatedSource(newSourceVertices);
960-
possibleSourceVertices.addAll(newSourceVertices);
961-
}
962-
// if there are only added or removed vertices in the current context, contextId might be empty
963-
if (possibleSourceVertices.size() > 0) {
964-
if (contextId.size() == 0) {
965-
contextId = singletonList(typeNameForDebug);
986+
Set<Vertex> possibleTargetVertices = new LinkedHashSet<>();
987+
for (String insertedContext : insertedContexts) {
988+
ImmutableList<Vertex> vertices = targetGroups.get(insertedContext);
989+
for (Vertex targetVertex : vertices) {
990+
if (!usedTargetVertices.contains(targetVertex)) {
991+
possibleTargetVertices.add(targetVertex);
992+
}
966993
}
967-
possibleMappings.putContext(contextId, possibleSourceVertices, possibleTargetVertices);
994+
usedTargetVertices.addAll(vertices);
995+
}
996+
if (contextId.size() == 0) {
997+
contextId = singletonList(typeNameForDebug);
968998
}
969-
possibleMappings.putPossibleMappings(possibleSourceVertices, possibleTargetVertices);
999+
possibleMappings.putPossibleMappings(contextId, possibleSourceVertices, possibleTargetVertices, typeNameForDebug);
9701000
}
9711001

9721002
public PossibleMappings getIsolatedVertices() {

0 commit comments

Comments
 (0)