Skip to content

Commit dcc6a93

Browse files
committed
A more memory efficient directives container
1 parent aef8032 commit dcc6a93

18 files changed

+383
-108
lines changed

src/main/java/graphql/language/DirectivesContainer.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,16 @@ public interface DirectivesContainer<T extends DirectivesContainer> extends Node
3434
*
3535
* @return a map of all directives by directive name
3636
*/
37-
default Map<String, List<Directive>> getDirectivesByName() {
38-
return ImmutableMap.copyOf(allDirectivesByName(getDirectives()));
39-
}
37+
Map<String, List<Directive>> getDirectivesByName();
4038

4139
/**
42-
* Returns all of the directives with the provided name, including repeatable and non repeatable directives.
40+
* Returns all the directives with the provided name, including repeatable and non repeatable directives.
4341
*
4442
* @param directiveName the name of the directives to retrieve
4543
*
4644
* @return the directives or empty list if there is not one with that name
4745
*/
48-
default List<Directive> getDirectives(String directiveName) {
49-
return getDirectivesByName().getOrDefault(directiveName, emptyList());
50-
}
46+
List<Directive> getDirectives(String directiveName);
5147

5248
/**
5349
* This returns true if the AST node contains one or more directives by the specified name
@@ -56,7 +52,5 @@ default List<Directive> getDirectives(String directiveName) {
5652
*
5753
* @return true if the AST node contains one or more directives by the specified name
5854
*/
59-
default boolean hasDirective(String directiveName) {
60-
return !getDirectives(directiveName).isEmpty();
61-
}
55+
boolean hasDirective(String directiveName);
6256
}

src/main/java/graphql/language/EnumTypeDefinition.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public class EnumTypeDefinition extends AbstractDescribedNode<EnumTypeDefinition> implements TypeDefinition<EnumTypeDefinition>, DirectivesContainer<EnumTypeDefinition>, NamedNode<EnumTypeDefinition> {
2424
private final String name;
2525
private final ImmutableList<EnumValueDefinition> enumValueDefinitions;
26-
private final ImmutableList<Directive> directives;
26+
private final NodeUtil.DirectivesHolder directives;
2727

2828
public static final String CHILD_ENUM_VALUE_DEFINITIONS = "enumValueDefinitions";
2929
public static final String CHILD_DIRECTIVES = "directives";
@@ -38,7 +38,7 @@ protected EnumTypeDefinition(String name,
3838
IgnoredChars ignoredChars, Map<String, String> additionalData) {
3939
super(sourceLocation, comments, ignoredChars, additionalData, description);
4040
this.name = name;
41-
this.directives = ImmutableKit.nonNullCopyOf(directives);
41+
this.directives = NodeUtil.DirectivesHolder.of(directives);
4242
this.enumValueDefinitions = ImmutableKit.nonNullCopyOf(enumValueDefinitions);
4343
}
4444

@@ -57,7 +57,22 @@ public List<EnumValueDefinition> getEnumValueDefinitions() {
5757

5858
@Override
5959
public List<Directive> getDirectives() {
60-
return directives;
60+
return directives.getDirectives();
61+
}
62+
63+
@Override
64+
public Map<String, List<Directive>> getDirectivesByName() {
65+
return directives.getDirectivesByName();
66+
}
67+
68+
@Override
69+
public List<Directive> getDirectives(String directiveName) {
70+
return directives.getDirectives(directiveName);
71+
}
72+
73+
@Override
74+
public boolean hasDirective(String directiveName) {
75+
return directives.hasDirective(directiveName);
6176
}
6277

6378
@Override
@@ -69,15 +84,15 @@ public String getName() {
6984
public List<Node> getChildren() {
7085
List<Node> result = new ArrayList<>();
7186
result.addAll(enumValueDefinitions);
72-
result.addAll(directives);
87+
result.addAll(directives.getDirectives());
7388
return result;
7489
}
7590

7691
@Override
7792
public NodeChildrenContainer getNamedChildren() {
7893
return newNodeChildrenContainer()
7994
.children(CHILD_ENUM_VALUE_DEFINITIONS, enumValueDefinitions)
80-
.children(CHILD_DIRECTIVES, directives)
95+
.children(CHILD_DIRECTIVES, directives.getDirectives())
8196
.build();
8297
}
8398

@@ -107,7 +122,7 @@ public boolean isEqualTo(Node o) {
107122
public EnumTypeDefinition deepCopy() {
108123
return new EnumTypeDefinition(name,
109124
deepCopy(enumValueDefinitions),
110-
deepCopy(directives),
125+
deepCopy(directives.getDirectives()),
111126
description,
112127
getSourceLocation(),
113128
getComments(),

src/main/java/graphql/language/EnumValueDefinition.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
import static graphql.Assert.assertNotNull;
1818
import static graphql.collect.ImmutableKit.emptyList;
1919
import static graphql.collect.ImmutableKit.emptyMap;
20-
import static graphql.collect.ImmutableKit.nonNullCopyOf;
2120
import static graphql.language.NodeChildrenContainer.newNodeChildrenContainer;
2221

2322
@PublicApi
2423
public class EnumValueDefinition extends AbstractDescribedNode<EnumValueDefinition> implements DirectivesContainer<EnumValueDefinition>, NamedNode<EnumValueDefinition> {
2524
private final String name;
26-
private final ImmutableList<Directive> directives;
25+
private final NodeUtil.DirectivesHolder directives;
2726

2827
public static final String CHILD_DIRECTIVES = "directives";
2928

@@ -36,7 +35,7 @@ protected EnumValueDefinition(String name,
3635
IgnoredChars ignoredChars, Map<String, String> additionalData) {
3736
super(sourceLocation, comments, ignoredChars, additionalData, description);
3837
this.name = name;
39-
this.directives = nonNullCopyOf(directives);
38+
this.directives = NodeUtil.DirectivesHolder.of(directives);
4039
}
4140

4241
/**
@@ -65,18 +64,33 @@ public String getName() {
6564

6665
@Override
6766
public List<Directive> getDirectives() {
68-
return directives;
67+
return directives.getDirectives();
68+
}
69+
70+
@Override
71+
public Map<String, List<Directive>> getDirectivesByName() {
72+
return directives.getDirectivesByName();
73+
}
74+
75+
@Override
76+
public List<Directive> getDirectives(String directiveName) {
77+
return directives.getDirectives(directiveName);
78+
}
79+
80+
@Override
81+
public boolean hasDirective(String directiveName) {
82+
return directives.hasDirective(directiveName);
6983
}
7084

7185
@Override
7286
public List<Node> getChildren() {
73-
return ImmutableList.copyOf(directives);
87+
return ImmutableList.copyOf(directives.getDirectives());
7488
}
7589

7690
@Override
7791
public NodeChildrenContainer getNamedChildren() {
7892
return newNodeChildrenContainer()
79-
.children(CHILD_DIRECTIVES, directives)
93+
.children(CHILD_DIRECTIVES, directives.getDirectives())
8094
.build();
8195
}
8296

@@ -104,7 +118,7 @@ public boolean isEqualTo(Node o) {
104118

105119
@Override
106120
public EnumValueDefinition deepCopy() {
107-
return new EnumValueDefinition(name, deepCopy(directives), description, getSourceLocation(), getComments(), getIgnoredChars(), getAdditionalData());
121+
return new EnumValueDefinition(name, deepCopy(directives.getDirectives()), description, getSourceLocation(), getComments(), getIgnoredChars(), getAdditionalData());
108122
}
109123

110124
@Override

src/main/java/graphql/language/Field.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class Field extends AbstractNode<Field> implements Selection<Field>, Sele
3232
private final String name;
3333
private final String alias;
3434
private final ImmutableList<Argument> arguments;
35-
private final ImmutableList<Directive> directives;
35+
private final NodeUtil.DirectivesHolder directives;
3636
private final SelectionSet selectionSet;
3737

3838
public static final String CHILD_ARGUMENTS = "arguments";
@@ -54,7 +54,7 @@ protected Field(String name,
5454
this.name = name == null ? null : Interning.intern(name);
5555
this.alias = alias;
5656
this.arguments = ImmutableList.copyOf(arguments);
57-
this.directives = ImmutableList.copyOf(directives);
57+
this.directives = NodeUtil.DirectivesHolder.of(directives);
5858
this.selectionSet = selectionSet;
5959
}
6060

@@ -103,7 +103,7 @@ public Field(String name, SelectionSet selectionSet) {
103103
public List<Node> getChildren() {
104104
List<Node> result = new ArrayList<>();
105105
result.addAll(arguments);
106-
result.addAll(directives);
106+
result.addAll(directives.getDirectives());
107107
if (selectionSet != null) {
108108
result.add(selectionSet);
109109
}
@@ -114,7 +114,7 @@ public List<Node> getChildren() {
114114
public NodeChildrenContainer getNamedChildren() {
115115
return NodeChildrenContainer.newNodeChildrenContainer()
116116
.children(CHILD_ARGUMENTS, arguments)
117-
.children(CHILD_DIRECTIVES, directives)
117+
.children(CHILD_DIRECTIVES, directives.getDirectives())
118118
.child(CHILD_SELECTION_SET, selectionSet)
119119
.build();
120120
}
@@ -147,7 +147,22 @@ public List<Argument> getArguments() {
147147

148148
@Override
149149
public List<Directive> getDirectives() {
150-
return directives;
150+
return directives.getDirectives();
151+
}
152+
153+
@Override
154+
public Map<String, List<Directive>> getDirectivesByName() {
155+
return directives.getDirectivesByName();
156+
}
157+
158+
@Override
159+
public List<Directive> getDirectives(String directiveName) {
160+
return directives.getDirectives(directiveName);
161+
}
162+
163+
@Override
164+
public boolean hasDirective(String directiveName) {
165+
return directives.hasDirective(directiveName);
151166
}
152167

153168
@Override
@@ -175,7 +190,7 @@ public Field deepCopy() {
175190
return new Field(name,
176191
alias,
177192
deepCopy(arguments),
178-
deepCopy(directives),
193+
deepCopy(directives.getDirectives()),
179194
deepCopy(selectionSet),
180195
getSourceLocation(),
181196
getComments(),

src/main/java/graphql/language/FieldDefinition.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class FieldDefinition extends AbstractDescribedNode<FieldDefinition> impl
2525
private final String name;
2626
private final Type type;
2727
private final ImmutableList<InputValueDefinition> inputValueDefinitions;
28-
private final ImmutableList<Directive> directives;
28+
private final NodeUtil.DirectivesHolder directives;
2929

3030
public static final String CHILD_TYPE = "type";
3131
public static final String CHILD_INPUT_VALUE_DEFINITION = "inputValueDefinition";
@@ -45,7 +45,7 @@ protected FieldDefinition(String name,
4545
this.name = name;
4646
this.type = type;
4747
this.inputValueDefinitions = ImmutableList.copyOf(inputValueDefinitions);
48-
this.directives = ImmutableList.copyOf(directives);
48+
this.directives = NodeUtil.DirectivesHolder.of(directives);
4949
}
5050

5151
public FieldDefinition(String name,
@@ -68,15 +68,30 @@ public List<InputValueDefinition> getInputValueDefinitions() {
6868

6969
@Override
7070
public List<Directive> getDirectives() {
71-
return directives;
71+
return directives.getDirectives();
72+
}
73+
74+
@Override
75+
public Map<String, List<Directive>> getDirectivesByName() {
76+
return directives.getDirectivesByName();
77+
}
78+
79+
@Override
80+
public List<Directive> getDirectives(String directiveName) {
81+
return directives.getDirectives(directiveName);
82+
}
83+
84+
@Override
85+
public boolean hasDirective(String directiveName) {
86+
return directives.hasDirective(directiveName);
7287
}
7388

7489
@Override
7590
public List<Node> getChildren() {
7691
List<Node> result = new ArrayList<>();
7792
result.add(type);
7893
result.addAll(inputValueDefinitions);
79-
result.addAll(directives);
94+
result.addAll(directives.getDirectives());
8095
return result;
8196
}
8297

@@ -85,7 +100,7 @@ public NodeChildrenContainer getNamedChildren() {
85100
return newNodeChildrenContainer()
86101
.child(CHILD_TYPE, type)
87102
.children(CHILD_INPUT_VALUE_DEFINITION, inputValueDefinitions)
88-
.children(CHILD_DIRECTIVES, directives)
103+
.children(CHILD_DIRECTIVES, directives.getDirectives())
89104
.build();
90105
}
91106

@@ -117,7 +132,7 @@ public FieldDefinition deepCopy() {
117132
return new FieldDefinition(name,
118133
deepCopy(type),
119134
deepCopy(inputValueDefinitions),
120-
deepCopy(directives),
135+
deepCopy(directives.getDirectives()),
121136
description,
122137
getSourceLocation(),
123138
getComments(),

src/main/java/graphql/language/FragmentDefinition.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class FragmentDefinition extends AbstractNode<FragmentDefinition> impleme
2727

2828
private final String name;
2929
private final TypeName typeCondition;
30-
private final ImmutableList<Directive> directives;
30+
private final NodeUtil.DirectivesHolder directives;
3131
private final SelectionSet selectionSet;
3232

3333
public static final String CHILD_TYPE_CONDITION = "typeCondition";
@@ -46,7 +46,7 @@ protected FragmentDefinition(String name,
4646
super(sourceLocation, comments, ignoredChars, additionalData);
4747
this.name = name;
4848
this.typeCondition = typeCondition;
49-
this.directives = ImmutableList.copyOf(directives);
49+
this.directives = NodeUtil.DirectivesHolder.of(directives);
5050
this.selectionSet = selectionSet;
5151
}
5252

@@ -62,9 +62,23 @@ public TypeName getTypeCondition() {
6262

6363
@Override
6464
public List<Directive> getDirectives() {
65-
return directives;
65+
return directives.getDirectives();
6666
}
6767

68+
@Override
69+
public Map<String, List<Directive>> getDirectivesByName() {
70+
return directives.getDirectivesByName();
71+
}
72+
73+
@Override
74+
public List<Directive> getDirectives(String directiveName) {
75+
return directives.getDirectives(directiveName);
76+
}
77+
78+
@Override
79+
public boolean hasDirective(String directiveName) {
80+
return directives.hasDirective(directiveName);
81+
}
6882

6983
@Override
7084
public SelectionSet getSelectionSet() {
@@ -75,7 +89,7 @@ public SelectionSet getSelectionSet() {
7589
public List<Node> getChildren() {
7690
List<Node> result = new ArrayList<>();
7791
result.add(typeCondition);
78-
result.addAll(directives);
92+
result.addAll(directives.getDirectives());
7993
result.add(selectionSet);
8094
return result;
8195
}
@@ -84,7 +98,7 @@ public List<Node> getChildren() {
8498
public NodeChildrenContainer getNamedChildren() {
8599
return newNodeChildrenContainer()
86100
.child(CHILD_TYPE_CONDITION, typeCondition)
87-
.children(CHILD_DIRECTIVES, directives)
101+
.children(CHILD_DIRECTIVES, directives.getDirectives())
88102
.child(CHILD_SELECTION_SET, selectionSet)
89103
.build();
90104
}
@@ -116,7 +130,7 @@ public boolean isEqualTo(Node o) {
116130
public FragmentDefinition deepCopy() {
117131
return new FragmentDefinition(name,
118132
deepCopy(typeCondition),
119-
deepCopy(directives),
133+
deepCopy(directives.getDirectives()),
120134
deepCopy(selectionSet),
121135
getSourceLocation(),
122136
getComments(),

0 commit comments

Comments
 (0)