Skip to content

Commit 8f5dad2

Browse files
committed
removed selection set tracking
1 parent d186d8f commit 8f5dad2

File tree

2 files changed

+18
-59
lines changed

2 files changed

+18
-59
lines changed

src/main/java/graphql/servlet/instrumentation/AbstractTrackingApproach.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ protected RequestStack getStack() {
5050
public ExecutionStrategyInstrumentationContext beginExecutionStrategy(InstrumentationExecutionStrategyParameters parameters) {
5151
ExecutionId executionId = parameters.getExecutionContext().getExecutionId();
5252
ExecutionPath path = parameters.getExecutionStrategyParameters().getPath();
53-
List<Selection> selectionSet = Optional.ofNullable(parameters.getExecutionStrategyParameters().getField())
54-
.map(MergedField::getSingleField).map(Field::getSelectionSet).map(SelectionSet::getSelections).orElse(Collections.emptyList());
5553
int parentLevel = path.getLevel();
5654
int curLevel = parentLevel + 1;
5755
int fieldCount = parameters.getExecutionStrategyParameters().getFields().size();
@@ -74,7 +72,7 @@ public void onCompleted(ExecutionResult result, Throwable t) {
7472
@Override
7573
public void onFieldValuesInfo(List<FieldValueInfo> fieldValueInfoList) {
7674
synchronized (stack) {
77-
stack.setStatus(executionId, handleOnFieldValuesInfo(fieldValueInfoList, stack, executionId, curLevel, selectionSet));
75+
stack.setStatus(executionId, handleOnFieldValuesInfo(fieldValueInfoList, stack, executionId, curLevel));
7876
if (stack.allReady()) {
7977
dispatchWithoutLocking();
8078
}
@@ -86,7 +84,7 @@ public void onDeferredField(MergedField field) {
8684
// fake fetch count for this field
8785
synchronized (stack) {
8886
stack.increaseFetchCount(executionId, curLevel);
89-
stack.setStatus(executionId, dispatchIfNeeded(stack, executionId, curLevel, selectionSet));
87+
stack.setStatus(executionId, dispatchIfNeeded(stack, executionId, curLevel));
9088
if (stack.allReady()) {
9189
dispatchWithoutLocking();
9290
}
@@ -98,7 +96,7 @@ public void onDeferredField(MergedField field) {
9896
//
9997
// thread safety : called with synchronised(stack)
10098
//
101-
private boolean handleOnFieldValuesInfo(List<FieldValueInfo> fieldValueInfoList, RequestStack stack, ExecutionId executionId, int curLevel, List<Selection> selectionSet) {
99+
private boolean handleOnFieldValuesInfo(List<FieldValueInfo> fieldValueInfoList, RequestStack stack, ExecutionId executionId, int curLevel) {
102100
stack.increaseHappenedOnFieldValueCalls(executionId, curLevel);
103101
int expectedStrategyCalls = 0;
104102
for (FieldValueInfo fieldValueInfo : fieldValueInfoList) {
@@ -109,7 +107,7 @@ private boolean handleOnFieldValuesInfo(List<FieldValueInfo> fieldValueInfoList,
109107
}
110108
}
111109
stack.increaseExpectedStrategyCalls(executionId, curLevel + 1, expectedStrategyCalls);
112-
return dispatchIfNeeded(stack, executionId, curLevel + 1, selectionSet);
110+
return dispatchIfNeeded(stack, executionId, curLevel + 1);
113111
}
114112

115113
private int getCountForList(FieldValueInfo fieldValueInfo) {
@@ -128,8 +126,6 @@ private int getCountForList(FieldValueInfo fieldValueInfo) {
128126
public DeferredFieldInstrumentationContext beginDeferredField(InstrumentationDeferredFieldParameters parameters) {
129127
ExecutionId executionId = parameters.getExecutionContext().getExecutionId();
130128
int level = parameters.getExecutionStrategyParameters().getPath().getLevel();
131-
List<Selection> selectionSet = Optional.ofNullable(parameters.getExecutionStrategyParameters().getField())
132-
.map(MergedField::getSingleField).map(Field::getSelectionSet).map(SelectionSet::getSelections).orElse(Collections.emptyList());
133129
synchronized (stack) {
134130
stack.clearAndMarkCurrentLevelAsReady(executionId, level);
135131
}
@@ -147,7 +143,7 @@ public void onCompleted(ExecutionResult result, Throwable t) {
147143
@Override
148144
public void onFieldValueInfo(FieldValueInfo fieldValueInfo) {
149145
synchronized (stack) {
150-
stack.setStatus(executionId, handleOnFieldValuesInfo(Collections.singletonList(fieldValueInfo), stack, executionId, level, selectionSet));
146+
stack.setStatus(executionId, handleOnFieldValuesInfo(Collections.singletonList(fieldValueInfo), stack, executionId, level));
151147
if (stack.allReady()) {
152148
dispatchWithoutLocking();
153149
}
@@ -160,16 +156,14 @@ public void onFieldValueInfo(FieldValueInfo fieldValueInfo) {
160156
public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
161157
ExecutionId executionId = parameters.getExecutionContext().getExecutionId();
162158
ExecutionPath path = parameters.getEnvironment().getExecutionStepInfo().getPath();
163-
List<Selection> selectionSet = Optional.ofNullable(parameters.getEnvironment().getField())
164-
.map(Field::getSelectionSet).map(SelectionSet::getSelections).orElse(Collections.emptyList());
165159
int level = path.getLevel();
166160
return new InstrumentationContext<Object>() {
167161

168162
@Override
169163
public void onDispatched(CompletableFuture result) {
170164
synchronized (stack) {
171165
stack.increaseFetchCount(executionId, level);
172-
stack.setStatus(executionId, dispatchIfNeeded(stack, executionId, level, selectionSet));
166+
stack.setStatus(executionId, dispatchIfNeeded(stack, executionId, level));
173167

174168
if (stack.allReady()) {
175169
dispatchWithoutLocking();
@@ -197,9 +191,9 @@ public void removeTracking(ExecutionId executionId) {
197191
//
198192
// thread safety : called with synchronised(stack)
199193
//
200-
private boolean dispatchIfNeeded(RequestStack stack, ExecutionId executionId, int level, List<Selection> selectionSet) {
194+
private boolean dispatchIfNeeded(RequestStack stack, ExecutionId executionId, int level) {
201195
if (levelReady(stack, executionId, level)) {
202-
return stack.dispatchIfNotDispatchedBefore(executionId, level, selectionSet);
196+
return stack.dispatchIfNotDispatchedBefore(executionId, level);
203197
}
204198
return false;
205199
}

src/main/java/graphql/servlet/instrumentation/RequestStack.java

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import graphql.language.SelectionSet;
88
import graphql.schema.GraphQLOutputType;
99

10+
import java.util.ArrayList;
1011
import java.util.Collections;
1112
import java.util.Iterator;
1213
import java.util.LinkedHashMap;
@@ -35,7 +36,7 @@ private static class CallStack {
3536
private final Map<Integer, Integer> happenedOnFieldValueCallsPerLevel = new LinkedHashMap<>();
3637

3738

38-
private final Map<Integer, List<Selection>> dispatchedLevels = new LinkedHashMap<>();
39+
private final List<Integer> dispatchedLevels = new ArrayList<>();
3940

4041
private CallStack() {
4142
expectedStrategyCallsPerLevel.put(1, 1);
@@ -74,7 +75,7 @@ private boolean allFetchesHappened(int level) {
7475
return Objects.equals(fetchCountPerLevel.get(level), expectedFetchCountPerLevel.get(level));
7576
}
7677

77-
private Map<Integer, List<Selection>> getDispatchedLevels() {
78+
private List<Integer> getDispatchedLevels() {
7879
return dispatchedLevels;
7980
}
8081

@@ -90,12 +91,12 @@ public String toString() {
9091
'}';
9192
}
9293

93-
private boolean dispatchIfNotDispatchedBefore(int level, List<Selection> selectionSet) {
94-
if (dispatchedLevels.containsKey(level)) {
94+
private boolean dispatchIfNotDispatchedBefore(int level) {
95+
if (dispatchedLevels.contains(level)) {
9596
Assert.assertShouldNeverHappen("level " + level + " already dispatched");
9697
return false;
9798
}
98-
dispatchedLevels.put(level, selectionSet);
99+
dispatchedLevels.add(level);
99100
return true;
100101
}
101102

@@ -131,45 +132,9 @@ public void setStatus(ExecutionId executionId, boolean toState) {
131132
* @return if all managed executions are ready to be dispatched.
132133
*/
133134
public boolean allReady() {
135+
List<Integer> dispatchStack = activeRequests.values().stream().findFirst().map(CallStack::getDispatchedLevels).orElse(Collections.emptyList());
134136
return status.values().stream().noneMatch(Boolean.FALSE::equals) &&
135-
activeRequests.values().stream().map(CallStack::getDispatchedLevels).allMatch(dispatchMap ->
136-
verifyAgainstOthers(dispatchMap, activeRequests.values().stream().map(CallStack::getDispatchedLevels).collect(Collectors.toList())));
137-
}
138-
139-
boolean verifyAgainstOthers(Map<Integer, List<Selection>> current, List<Map<Integer, List<Selection>>> others) {
140-
for (Map<Integer, List<Selection>> other : others) {
141-
Iterator<Map.Entry<Integer, List<Selection>>> currentIter = current.entrySet().iterator();
142-
Iterator<Map.Entry<Integer, List<Selection>>> otherIter = other.entrySet().iterator();
143-
if (currentIter.hasNext() && otherIter.hasNext()) {
144-
Map.Entry<Integer, List<Selection>> currentFirstEntry = currentIter.next();
145-
Map.Entry<Integer, List<Selection>> otherFirstEntry = otherIter.next();
146-
boolean matching = selectionsEqual(currentFirstEntry.getValue(), otherFirstEntry.getValue());
147-
while (matching && currentIter.hasNext() && otherIter.hasNext()) {
148-
currentFirstEntry = currentIter.next();
149-
otherFirstEntry = otherIter.next();
150-
matching = selectionsEqual(currentFirstEntry.getValue(), otherFirstEntry.getValue());
151-
}
152-
if (matching && (currentIter.hasNext() || otherIter.hasNext())) {
153-
return false;
154-
}
155-
} else if (otherIter.hasNext() || currentIter.hasNext()) {
156-
return false;
157-
}
158-
}
159-
return true;
160-
}
161-
162-
private boolean selectionsEqual(List<Selection> first, List<Selection> second) {
163-
if (first.size() != second.size()) {
164-
return false;
165-
} else {
166-
for (int i = 0; i < first.size(); i++) {
167-
if (!first.get(i).isEqualTo(second.get(i))) {
168-
return false;
169-
}
170-
}
171-
return true;
172-
}
137+
activeRequests.values().stream().map(CallStack::getDispatchedLevels).allMatch(dispatchStack::equals);
173138
}
174139

175140
/**
@@ -312,12 +277,12 @@ public boolean allStrategyCallsHappened(ExecutionId executionId, int level) {
312277
* @param level the level to get the value of
313278
* @return dispatchIfNotDispattchedBefore
314279
*/
315-
public boolean dispatchIfNotDispatchedBefore(ExecutionId executionId, int level, List<Selection> selectionSet) {
280+
public boolean dispatchIfNotDispatchedBefore(ExecutionId executionId, int level) {
316281
if (!activeRequests.containsKey(executionId)) {
317282
throw new IllegalStateException(
318283
String.format("Execution %s not managed by this RequestStack, can not get dispatch if not dispatched before value", executionId));
319284
}
320-
return activeRequests.get(executionId).dispatchIfNotDispatchedBefore(level, selectionSet);
285+
return activeRequests.get(executionId).dispatchIfNotDispatchedBefore(level);
321286
}
322287

323288
/**

0 commit comments

Comments
 (0)