Skip to content

Commit abbb856

Browse files
authored
Naming fix up to IntMap (#2631)
1 parent f8dd9d2 commit abbb856

File tree

4 files changed

+58
-24
lines changed

4 files changed

+58
-24
lines changed

src/main/java/graphql/execution/instrumentation/dataloader/FieldLevelTrackingApproach.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ public class FieldLevelTrackingApproach {
2929

3030
private static class CallStack implements InstrumentationState {
3131

32-
private final IntMap expectedFetchCountPerLevel = new IntMap();
33-
private final IntMap fetchCountPerLevel = new IntMap();
34-
private final IntMap expectedStrategyCallsPerLevel = new IntMap();
35-
private final IntMap happenedStrategyCallsPerLevel = new IntMap();
36-
private final IntMap happenedOnFieldValueCallsPerLevel = new IntMap();
32+
private final LevelMap expectedFetchCountPerLevel = new LevelMap();
33+
private final LevelMap fetchCountPerLevel = new LevelMap();
34+
private final LevelMap expectedStrategyCallsPerLevel = new LevelMap();
35+
private final LevelMap happenedStrategyCallsPerLevel = new LevelMap();
36+
private final LevelMap happenedOnFieldValueCallsPerLevel = new LevelMap();
3737

3838
private final Set<Integer> dispatchedLevels = new LinkedHashSet<>();
3939

4040
CallStack() {
41-
expectedStrategyCallsPerLevel.increment(1, 1);
41+
expectedStrategyCallsPerLevel.set(1, 1);
4242
}
4343

4444
void increaseExpectedFetchCount(int level, int count) {
@@ -95,11 +95,11 @@ public boolean dispatchIfNotDispatchedBefore(int level) {
9595
}
9696

9797
public void clearAndMarkCurrentLevelAsReady(int level) {
98-
expectedFetchCountPerLevel.reset();
99-
fetchCountPerLevel.reset();
100-
expectedStrategyCallsPerLevel.reset();
101-
happenedStrategyCallsPerLevel.reset();
102-
happenedOnFieldValueCallsPerLevel.reset();
98+
expectedFetchCountPerLevel.clear();
99+
fetchCountPerLevel.clear();
100+
expectedStrategyCallsPerLevel.clear();
101+
happenedStrategyCallsPerLevel.clear();
102+
happenedOnFieldValueCallsPerLevel.clear();
103103
dispatchedLevels.clear();
104104

105105
// make sure the level is ready

src/main/java/graphql/execution/instrumentation/dataloader/IntMap.java renamed to src/main/java/graphql/execution/instrumentation/dataloader/LevelMap.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44

55
import java.util.Arrays;
66

7+
/**
8+
* This data structure tracks the number of expected calls on a given level
9+
*/
710
@Internal
8-
public class IntMap {
11+
public class LevelMap {
912

1013
// A reasonable default that guarantees no additional allocations for most use cases.
1114
private static final int DEFAULT_INITIAL_SIZE = 16;
1215

1316
// this array is mutable in both size and contents.
1417
private int[] countsByLevel;
1518

16-
public IntMap(int initialSize) {
19+
public LevelMap(int initialSize) {
1720
if (initialSize < 0) {
1821
throw new IllegalArgumentException("negative size " + initialSize);
1922
}
2023
countsByLevel = new int[initialSize];
2124
}
2225

23-
public IntMap() {
26+
public LevelMap() {
2427
this(DEFAULT_INITIAL_SIZE);
2528
}
2629

@@ -35,14 +38,23 @@ public int get(int level) {
3538
}
3639

3740
public void increment(int level, int by) {
41+
mutatePreconditions(level);
42+
countsByLevel[level] += by;
43+
}
44+
45+
public void set(int level, int newValue) {
46+
mutatePreconditions(level);
47+
countsByLevel[level] = newValue;
48+
}
49+
50+
private void mutatePreconditions(int level) {
3851
if (level < 0) {
3952
throw new IllegalArgumentException("negative level " + level);
4053
}
4154
if (level + 1 > countsByLevel.length) {
4255
int newSize = level == 0 ? 1 : level * 2;
4356
countsByLevel = Arrays.copyOf(countsByLevel, newSize);
4457
}
45-
countsByLevel[level] += by;
4658
}
4759

4860
@Override
@@ -56,7 +68,7 @@ public String toString() {
5668
return result.toString();
5769
}
5870

59-
public void reset() {
71+
public void clear() {
6072
Arrays.fill(countsByLevel, 0);
6173
}
6274
}

src/test/groovy/graphql/execution/instrumentation/dataloader/IntMapTest.groovy renamed to src/test/groovy/graphql/execution/instrumentation/dataloader/LevelMapTest.groovy

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package graphql.execution.instrumentation.dataloader
22

33
import spock.lang.Specification
44

5-
class IntMapTest extends Specification {
5+
class LevelMapTest extends Specification {
66

77
def "increase adds levels"() {
88
given:
9-
IntMap sut = new IntMap(0) // starts empty
9+
LevelMap sut = new LevelMap(0) // starts empty
1010

1111
when:
1212
sut.increment(2, 42) // level 2 has count 42
@@ -19,7 +19,7 @@ class IntMapTest extends Specification {
1919

2020
def "increase count by 10 for every level"() {
2121
given:
22-
IntMap sut = new IntMap(0)
22+
LevelMap sut = new LevelMap(0)
2323

2424
when:
2525
5.times {Integer level ->
@@ -31,10 +31,9 @@ class IntMapTest extends Specification {
3131
sut.get(level) == 10
3232
}
3333
}
34-
3534
def "increase yields new count"() {
3635
given:
37-
IntMap sut = new IntMap(0)
36+
LevelMap sut = new LevelMap(0)
3837

3938
when:
4039
sut.increment(1, 0)
@@ -55,9 +54,32 @@ class IntMapTest extends Specification {
5554
sut.get(1) == 101
5655
}
5756

57+
def "set yields new value"() {
58+
given:
59+
LevelMap sut = new LevelMap(0)
60+
61+
when:
62+
sut.set(1, 1)
63+
64+
then:
65+
sut.get(1) == 1
66+
67+
when:
68+
sut.increment(1, 100)
69+
70+
then:
71+
sut.get(1) == 101
72+
73+
when:
74+
sut.set(1, 666)
75+
76+
then:
77+
sut.get(1) == 666
78+
}
79+
5880
def "toString() is important for debugging"() {
5981
given:
60-
IntMap sut = new IntMap(0)
82+
LevelMap sut = new LevelMap(0)
6183

6284
when:
6385
sut.toString()

src/test/java/benchmark/IntMapBenchmark.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package benchmark;
22

3-
import graphql.execution.instrumentation.dataloader.IntMap;
3+
import graphql.execution.instrumentation.dataloader.LevelMap;
44
import org.openjdk.jmh.annotations.Benchmark;
55
import org.openjdk.jmh.annotations.BenchmarkMode;
66
import org.openjdk.jmh.annotations.Measurement;
@@ -33,7 +33,7 @@ public void benchmarkLinkedHashMap(Blackhole blackhole) {
3333

3434
@Benchmark
3535
public void benchmarkIntMap(Blackhole blackhole) {
36-
IntMap result = new IntMap(16);
36+
LevelMap result = new LevelMap(16);
3737
for (int i = 0; i < 30; i++) {
3838
int level = i % 10;
3939
int count = i * 2;

0 commit comments

Comments
 (0)