Skip to content

Commit 363f092

Browse files
committed
Change
1 parent 661537e commit 363f092

File tree

6 files changed

+57
-58
lines changed

6 files changed

+57
-58
lines changed

server/src/main/java/org/elasticsearch/common/io/stream/RecyclerBytesStreamOutput.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public class RecyclerBytesStreamOutput extends BytesStream implements Releasable
3838
static final VarHandle VH_BE_LONG = MethodHandles.byteArrayViewVarHandle(long[].class, ByteOrder.BIG_ENDIAN);
3939
static final VarHandle VH_LE_LONG = MethodHandles.byteArrayViewVarHandle(long[].class, ByteOrder.LITTLE_ENDIAN);
4040

41-
private ArrayList<Recycler.V<BytesRef>> pages = new ArrayList<>();
41+
private ArrayList<Recycler.V<BytesRef>> pages = new ArrayList<>(8);
4242
private final Recycler<BytesRef> recycler;
4343
private final int pageSize;
4444
private int pageIndex = -1;
4545
private int currentCapacity = 0;
4646
private int currentPageOffset;
47-
47+
4848
private byte[] bytesRefBytes;
4949
private int bytesRefOffset;
5050

@@ -213,9 +213,14 @@ public void seek(long position) {
213213
this.pageIndex = pageIndex;
214214
this.currentPageOffset = offsetInPage;
215215
}
216-
BytesRef page = pages.get(this.pageIndex).v();
217-
this.bytesRefBytes = page.bytes;
218-
this.bytesRefOffset = page.offset;
216+
if (position != 0) {
217+
BytesRef page = pages.get(this.pageIndex).v();
218+
this.bytesRefBytes = page.bytes;
219+
this.bytesRefOffset = page.offset;
220+
} else {
221+
this.bytesRefBytes = null;
222+
this.bytesRefOffset = 0;
223+
}
219224
}
220225

221226
public void skip(int length) {

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ public abstract class ESONEntry {
3737
private int offsetOrCount = -1;
3838

3939
ESONEntry(byte type, String key) {
40-
this(type, key, null);
40+
this(type, key, -1, null);
4141
}
4242

43-
ESONEntry(byte type, String key, ESONSource.Value value) {
43+
ESONEntry(byte type, String key, int offsetOrCount, ESONSource.Value value) {
4444
this.type = type;
4545
this.key = key;
46+
this.offsetOrCount = offsetOrCount;
4647
this.value = value;
4748
}
4849

@@ -104,15 +105,12 @@ public String toString() {
104105

105106
public static class FieldEntry extends ESONEntry {
106107

107-
public final ESONSource.Value value;
108-
109108
public FieldEntry(String key, ESONSource.Value value) {
110-
super(value.type(), key, value);
111-
this.value = value;
109+
super(value.type(), key, -1, value);
112110
}
113111

114112
public FieldEntry(String key, byte type, int offset) {
115-
this(key, parseValue(type, offset));
113+
super(type, key, offset, parseValue(type, offset));
116114
}
117115

118116
private static ESONSource.Value parseValue(byte type, int offset) {
@@ -130,7 +128,7 @@ private static ESONSource.Value parseValue(byte type, int offset) {
130128
public String toString() {
131129
return "FieldEntry{"
132130
+ "value="
133-
+ value
131+
+ value()
134132
+ ", type="
135133
+ type()
136134
+ ", key='"

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ public BytesReference getSerializedKeyBytes() {
7979
streamOutput.writeVInt(bytes.length);
8080
streamOutput.writeBytes(bytes, 0, bytes.length);
8181
streamOutput.writeByte(entry.type());
82-
// TODO: Combine
83-
if (entry instanceof ESONEntry.FieldEntry fieldEntry) {
84-
streamOutput.writeInt(fieldEntry.value.offset());
85-
} else {
86-
streamOutput.writeInt(entry.offsetOrCount());
87-
}
82+
streamOutput.writeInt(entry.offsetOrCount());
8883
}
8984
BytesReference bytes = streamOutput.bytes();
9085
ByteArrayOutputStream os = new ByteArrayOutputStream(bytes.length());

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void ensureMaterializedMap() {
6363
for (int i = 0; i < objEntry.offsetOrCount(); i++) {
6464
ESONEntry entry = esonFlat.keys().get(currentIndex);
6565
if (entry instanceof ESONEntry.FieldEntry fieldEntry) {
66-
materializedMap.put(fieldEntry.key(), fieldEntry.value);
66+
materializedMap.put(fieldEntry.key(), fieldEntry.value());
6767
currentIndex++;
6868
} else {
6969
if (entry instanceof ESONEntry.ObjectEntry) {
@@ -374,7 +374,7 @@ private void ensureMaterializedList() {
374374
for (int i = 0; i < arrEntry.offsetOrCount(); i++) {
375375
ESONEntry entry = esonFlat.keys().get(currentIndex);
376376
if (entry instanceof ESONEntry.FieldEntry fieldEntry) {
377-
materializedList.add(fieldEntry.value);
377+
materializedList.add(fieldEntry.value());
378378
currentIndex++;
379379
} else {
380380
if (entry instanceof ESONEntry.ObjectEntry) {
@@ -661,7 +661,7 @@ private static void handleObject(List<ESONEntry> flatKeyArray, Object object, St
661661
handleObject(flatKeyArray, value, null, newOffset, newValuesOut);
662662
}
663663
} else {
664-
flatKeyArray.add(new ESONEntry.FieldEntry(key, mutationToValue(newOffset, newValuesOut, obj)));
664+
flatKeyArray.add(mutationToValue(newOffset, key, newValuesOut, obj));
665665
}
666666
}
667667

@@ -717,7 +717,7 @@ private static void flattenArray(
717717
switch (type) {
718718
case ESONSource.Mutation mutation -> {
719719
// This is a mutated element - create new FieldEntry with mutation
720-
flatKeyArray.add(new ESONEntry.FieldEntry(null, mutationToValue(newOffset, newValuesOut, mutation.object())));
720+
flatKeyArray.add(mutationToValue(newOffset, null, newValuesOut, mutation.object()));
721721
elementCount++;
722722
}
723723
case ESONObject nestedObj -> {
@@ -746,68 +746,69 @@ private static void flattenArray(
746746
}
747747
}
748748

749-
private static ESONSource.Value mutationToValue(int newOffset, BytesStreamOutput newValuesOut, Object obj) throws IOException {
749+
private static ESONEntry.FieldEntry mutationToValue(int newOffset, String fieldName, BytesStreamOutput newValuesOut, Object obj)
750+
throws IOException {
750751
int position = newOffset + Math.toIntExact(newValuesOut.position());
751-
ESONSource.Value value;
752+
ESONEntry.FieldEntry value;
752753
if (obj == null) {
753-
value = ESONSource.ConstantValue.NULL;
754+
value = new ESONEntry.FieldEntry(fieldName, ESONSource.ConstantValue.NULL);
754755
} else if (obj instanceof Number num) {
755756
value = switch (num) {
756757
case Byte byteValue -> {
757758
newValuesOut.writeInt(byteValue.intValue());
758-
yield new ESONSource.FixedValue(position, ESONEntry.TYPE_INT);
759+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.TYPE_INT, position);
759760
}
760761
case Short shortValue -> {
761762
newValuesOut.writeInt(shortValue);
762-
yield new ESONSource.FixedValue(position, ESONEntry.TYPE_INT);
763+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.TYPE_INT, position);
763764
}
764765
case Integer intValue -> {
765766
newValuesOut.writeInt(intValue);
766-
yield new ESONSource.FixedValue(position, ESONEntry.TYPE_INT);
767+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.TYPE_INT, position);
767768
}
768769
case Long longValue -> {
769770
newValuesOut.writeLong(longValue);
770-
yield new ESONSource.FixedValue(position, ESONEntry.TYPE_LONG);
771+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.TYPE_LONG, position);
771772
}
772773
case Float floatValue -> {
773774
newValuesOut.writeFloat(floatValue);
774-
yield new ESONSource.FixedValue(position, ESONEntry.TYPE_FLOAT);
775+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.TYPE_FLOAT, position);
775776
}
776777
case Double doubleValue -> {
777778
newValuesOut.writeDouble(doubleValue);
778-
yield new ESONSource.FixedValue(position, ESONEntry.TYPE_DOUBLE);
779+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.TYPE_DOUBLE, position);
779780
}
780781
case BigInteger bigInteger -> {
781782
byte[] numberBytes = bigInteger.toString().getBytes(StandardCharsets.UTF_8);
782783
newValuesOut.writeVInt(numberBytes.length);
783784
newValuesOut.write(numberBytes);
784-
yield new ESONSource.VariableValue(position, ESONEntry.BIG_INTEGER);
785+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.BIG_INTEGER, position);
785786
}
786787
case BigDecimal bigDecimal -> {
787788
byte[] numberBytes = bigDecimal.toString().getBytes(StandardCharsets.UTF_8);
788789
newValuesOut.writeVInt(numberBytes.length);
789790
newValuesOut.write(numberBytes);
790-
yield new ESONSource.VariableValue(position, ESONEntry.BIG_DECIMAL);
791+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.BIG_DECIMAL, position);
791792
}
792793
default -> {
793794
byte[] utf8Bytes = num.toString().getBytes(StandardCharsets.UTF_8);
794795
newValuesOut.writeVInt(utf8Bytes.length);
795796
newValuesOut.write(utf8Bytes);
796-
yield new ESONSource.VariableValue(position, ESONEntry.STRING);
797+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.STRING, position);
797798
}
798799
};
799800
} else if (obj instanceof Boolean bool) {
800-
value = bool ? ESONSource.ConstantValue.TRUE : ESONSource.ConstantValue.FALSE;
801+
value = new ESONEntry.FieldEntry(fieldName, bool ? ESONSource.ConstantValue.TRUE : ESONSource.ConstantValue.FALSE);
801802
} else if (obj instanceof byte[] bytes) {
802803
newValuesOut.writeVInt(bytes.length);
803804
newValuesOut.writeBytes(bytes);
804-
value = new ESONSource.VariableValue(position, ESONEntry.BINARY);
805+
value = new ESONEntry.FieldEntry(fieldName, ESONEntry.BINARY, position);
805806
} else {
806807
String str = obj.toString();
807808
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
808809
newValuesOut.writeVInt(bytes.length);
809810
newValuesOut.writeBytes(bytes);
810-
value = new ESONSource.VariableValue(position, ESONEntry.STRING);
811+
value = new ESONEntry.FieldEntry(fieldName, ESONEntry.STRING, position);
811812
}
812813

813814
return value;

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ private static void parseArray(
9494
switch (token) {
9595
case START_OBJECT -> parseObject(parser, bytes, keyArray, null);
9696
case START_ARRAY -> parseArray(parser, bytes, keyArray, null);
97-
default -> {
98-
Value type = parseSimpleValue(parser, bytes, token);
99-
keyArray.add(new ESONEntry.FieldEntry(null, type));
100-
}
97+
default -> keyArray.add(parseSimpleValue(parser, null, bytes, token));
10198
}
10299
count++;
103100
}
@@ -112,59 +109,62 @@ private static void parseValue(XContentParser parser, String fieldName, Recycler
112109
switch (token) {
113110
case START_OBJECT -> parseObject(parser, bytes, keyArray, fieldName);
114111
case START_ARRAY -> parseArray(parser, bytes, keyArray, fieldName);
115-
default -> {
116-
Value type = parseSimpleValue(parser, bytes, token);
117-
keyArray.add(new ESONEntry.FieldEntry(fieldName, type));
118-
}
112+
default -> keyArray.add(parseSimpleValue(parser, fieldName, bytes, token));
119113
}
120114
}
121115

122-
private static Value parseSimpleValue(XContentParser parser, RecyclerBytesStreamOutput bytes, XContentParser.Token token)
123-
throws IOException {
116+
private static ESONEntry.FieldEntry parseSimpleValue(
117+
XContentParser parser,
118+
String fieldName,
119+
RecyclerBytesStreamOutput bytes,
120+
XContentParser.Token token
121+
) throws IOException {
124122
long position = bytes.position();
125123

126124
return switch (token) {
127125
case VALUE_STRING -> {
128126
XContentString.UTF8Bytes stringBytes = parser.optimizedText().bytes();
129127
bytes.writeVInt(stringBytes.length());
130128
bytes.writeBytes(stringBytes.bytes(), stringBytes.offset(), stringBytes.length());
131-
yield new VariableValue((int) position, ESONEntry.STRING);
129+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.STRING, (int) position);
132130
}
133131
case VALUE_NUMBER -> {
134132
XContentParser.NumberType numberType = parser.numberType();
135133
yield switch (numberType) {
136134
case INT -> {
137135
bytes.writeInt(parser.intValue());
138-
yield new FixedValue((int) position, ESONEntry.TYPE_INT);
136+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.TYPE_INT, (int) position);
139137
}
140138
case LONG -> {
141139
bytes.writeLong(parser.longValue());
142-
yield new FixedValue((int) position, ESONEntry.TYPE_LONG);
140+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.TYPE_LONG, (int) position);
143141
}
144142
case FLOAT -> {
145143
bytes.writeFloat(parser.floatValue());
146-
yield new FixedValue((int) position, ESONEntry.TYPE_FLOAT);
144+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.TYPE_FLOAT, (int) position);
147145
}
148146
case DOUBLE -> {
149147
bytes.writeDouble(parser.doubleValue());
150-
yield new FixedValue((int) position, ESONEntry.TYPE_DOUBLE);
148+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.TYPE_DOUBLE, (int) position);
151149
}
152150
case BIG_INTEGER, BIG_DECIMAL -> {
153151
byte type = numberType == XContentParser.NumberType.BIG_INTEGER ? ESONEntry.BIG_INTEGER : ESONEntry.BIG_DECIMAL;
154152
byte[] numberBytes = parser.text().getBytes(StandardCharsets.UTF_8);
155153
bytes.writeVInt(numberBytes.length);
156154
bytes.writeBytes(numberBytes, 0, numberBytes.length);
157-
yield new VariableValue((int) position, type);
155+
yield new ESONEntry.FieldEntry(fieldName, type, (int) position);
158156
}
159157
};
160158
}
161-
case VALUE_BOOLEAN -> parser.booleanValue() ? ConstantValue.TRUE : ConstantValue.FALSE;
162-
case VALUE_NULL -> ConstantValue.NULL;
159+
case VALUE_BOOLEAN -> parser.booleanValue()
160+
? new ESONEntry.FieldEntry(fieldName, ConstantValue.TRUE)
161+
: new ESONEntry.FieldEntry(fieldName, ConstantValue.FALSE);
162+
case VALUE_NULL -> new ESONEntry.FieldEntry(fieldName, ConstantValue.NULL);
163163
case VALUE_EMBEDDED_OBJECT -> {
164164
byte[] binaryValue = parser.binaryValue();
165165
bytes.writeVInt(binaryValue.length);
166166
bytes.write(binaryValue);
167-
yield new VariableValue((int) position, ESONEntry.BINARY);
167+
yield new ESONEntry.FieldEntry(fieldName, ESONEntry.BINARY, (int) position);
168168
}
169169
default -> throw new IllegalArgumentException("Unexpected token: " + token);
170170
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static XContentBuilder flattenToXContent(ESONFlat esonFlat, XContentBuild
9090
builder.field(fieldEntry.key());
9191
}
9292

93-
writeValue(values, builder, fieldEntry.value, params);
93+
writeValue(values, builder, fieldEntry.value(), params);
9494
currentContainer.remainingFields--;
9595
index++;
9696

0 commit comments

Comments
 (0)