Skip to content

Commit 05b30ff

Browse files
committed
Changes
1 parent 18ac0b8 commit 05b30ff

File tree

1 file changed

+21
-63
lines changed

1 file changed

+21
-63
lines changed

server/src/main/java/org/elasticsearch/ingest/ESONXContentParser.java

Lines changed: 21 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,10 @@ public class ESONXContentParser extends AbstractXContentParser {
6464
* Tracks the state of containers (objects/arrays) as we parse
6565
*/
6666
private static class ContainerContext {
67-
enum Type {
68-
OBJECT,
69-
ARRAY
70-
}
71-
72-
final Type type;
67+
final byte type;
7368
int fieldsRemaining;
7469

75-
ContainerContext(Type type, int fieldCount) {
70+
ContainerContext(byte type, int fieldCount) {
7671
this.type = type;
7772
this.fieldsRemaining = fieldCount;
7873
}
@@ -118,7 +113,7 @@ public Token nextToken() throws IOException {
118113
if (currentToken == null) {
119114
assert size >= currentIndex;
120115
ESONEntry.ObjectEntry rootEntry = (ESONEntry.ObjectEntry) keyArray.get(currentIndex);
121-
containerStack.push(new ContainerContext(ContainerContext.Type.OBJECT, rootEntry.offsetOrCount()));
116+
containerStack.push(new ContainerContext(ESONEntry.TYPE_OBJECT, rootEntry.offsetOrCount()));
122117
currentIndex++;
123118
currentToken = Token.START_OBJECT;
124119
return currentToken;
@@ -131,7 +126,7 @@ public Token nextToken() throws IOException {
131126

132127
if (containerStack.peek().fieldsRemaining == 0) {
133128
ContainerContext ctx = containerStack.pop();
134-
currentToken = ctx.type == ContainerContext.Type.OBJECT ? Token.END_OBJECT : Token.END_ARRAY;
129+
currentToken = ctx.type == ESONEntry.TYPE_OBJECT ? Token.END_OBJECT : Token.END_ARRAY;
135130
return currentToken;
136131
}
137132

@@ -143,7 +138,7 @@ public Token nextToken() throws IOException {
143138
assert currentContainer != null;
144139

145140
// Handle based on container type
146-
if (currentContainer.type == ContainerContext.Type.OBJECT && currentToken != Token.FIELD_NAME) {
141+
if (currentContainer.type == ESONEntry.TYPE_OBJECT && currentToken != Token.FIELD_NAME) {
147142
currentFieldName = entry.key();
148143
currentToken = Token.FIELD_NAME;
149144
return currentToken;
@@ -153,69 +148,32 @@ public Token nextToken() throws IOException {
153148
}
154149
}
155150

156-
private Token emitValue(ESONEntry entry) throws IOException {
151+
private Token emitValue(ESONEntry entry) {
157152
ContainerContext currentContainer = containerStack.peek();
158153
assert currentContainer != null;
159154
currentContainer.fieldsRemaining--;
160155
currentIndex++;
161156

157+
if (entry.type() == ESONEntry.TYPE_OBJECT || entry.type() == ESONEntry.TYPE_ARRAY) {
158+
containerStack.push(new ContainerContext(entry.type(), entry.offsetOrCount()));
159+
} else {
160+
currentType = ((ESONEntry.FieldEntry) entry).value;
161+
}
162+
162163
currentToken = switch (entry.type()) {
163-
case ESONEntry.TYPE_OBJECT -> {
164-
// Starting a nested object
165-
containerStack.push(new ContainerContext(ContainerContext.Type.OBJECT, entry.offsetOrCount()));
166-
yield Token.START_OBJECT;
167-
}
168-
case ESONEntry.TYPE_ARRAY -> {
169-
// Starting a nested array
170-
containerStack.push(new ContainerContext(ContainerContext.Type.ARRAY, entry.offsetOrCount()));
171-
yield Token.START_ARRAY;
172-
}
173-
case ESONEntry.TYPE_NULL -> {
174-
currentType = ((ESONEntry.FieldEntry) entry).value;
175-
yield Token.VALUE_NULL;
176-
}
177-
case ESONEntry.TYPE_TRUE, ESONEntry.TYPE_FALSE -> {
178-
currentType = ((ESONEntry.FieldEntry) entry).value;
179-
yield Token.VALUE_BOOLEAN;
180-
}
164+
case ESONEntry.TYPE_OBJECT -> Token.START_OBJECT;
165+
case ESONEntry.TYPE_ARRAY -> Token.START_ARRAY;
166+
case ESONEntry.TYPE_NULL -> Token.VALUE_NULL;
167+
case ESONEntry.TYPE_TRUE, ESONEntry.TYPE_FALSE -> Token.VALUE_BOOLEAN;
181168
case ESONEntry.TYPE_INT, ESONEntry.TYPE_LONG, ESONEntry.TYPE_FLOAT, ESONEntry.TYPE_DOUBLE, ESONEntry.BIG_INTEGER,
182-
ESONEntry.BIG_DECIMAL -> {
183-
currentType = ((ESONEntry.FieldEntry) entry).value;
184-
yield Token.VALUE_NUMBER;
185-
}
186-
case ESONEntry.STRING -> {
187-
currentType = ((ESONEntry.FieldEntry) entry).value;
188-
yield Token.VALUE_STRING;
189-
}
190-
case ESONEntry.BINARY -> {
191-
currentType = ((ESONEntry.FieldEntry) entry).value;
192-
yield Token.VALUE_EMBEDDED_OBJECT;
193-
}
194-
default -> {
195-
currentType = ((ESONEntry.FieldEntry) entry).value;
196-
yield determineTokenFromObject(((ESONSource.Mutation) currentType).object());
197-
}
169+
ESONEntry.BIG_DECIMAL -> Token.VALUE_NUMBER;
170+
case ESONEntry.STRING -> Token.VALUE_STRING;
171+
case ESONEntry.BINARY -> Token.VALUE_EMBEDDED_OBJECT;
172+
default -> throw new IllegalStateException("Unexpected entry type: " + entry.type());
198173
};
199174
return currentToken;
200175
}
201176

202-
private static Token determineTokenFromObject(Object obj) {
203-
if (obj == null) {
204-
return Token.VALUE_NULL;
205-
} else if (obj instanceof String) {
206-
return Token.VALUE_STRING;
207-
} else if (obj instanceof Number) {
208-
return Token.VALUE_NUMBER;
209-
} else if (obj instanceof Boolean) {
210-
return Token.VALUE_BOOLEAN;
211-
} else if (obj instanceof byte[]) {
212-
return Token.VALUE_EMBEDDED_OBJECT;
213-
} else {
214-
// TODO: Fix. This is because we have a variety of custom writers. We would need to expose those.
215-
return Token.VALUE_STRING;
216-
}
217-
}
218-
219177
// Helper method to materialize the current value on demand
220178
private Object getCurrentValue() {
221179
// TODO: Could probably optimize to not box all the numbers
@@ -277,7 +235,7 @@ public String currentName() throws IOException {
277235
}
278236
// When on a value token, return the field name if in an object
279237
ContainerContext ctx = containerStack.peek();
280-
if (ctx != null && ctx.type == ContainerContext.Type.OBJECT && currentFieldName != null) {
238+
if (ctx != null && ctx.type == ESONEntry.TYPE_OBJECT && currentFieldName != null) {
281239
return currentFieldName;
282240
}
283241
return null;

0 commit comments

Comments
 (0)