Skip to content

Commit 9a9354e

Browse files
committed
Change
1 parent 9487ac3 commit 9a9354e

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import org.apache.lucene.util.BytesRef;
1313
import org.elasticsearch.common.bytes.BytesReference;
14-
import org.elasticsearch.common.io.stream.RecyclerBytesStreamOutput;
14+
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1515
import org.elasticsearch.common.recycler.Recycler;
1616
import org.elasticsearch.transport.BytesRefRecycler;
1717
import org.elasticsearch.xcontent.ToXContent;
@@ -36,15 +36,16 @@ public class ESONSource {
3636

3737
public static class Builder {
3838

39-
private final RecyclerBytesStreamOutput bytes;
39+
private final BytesStreamOutput bytes;
4040
private final boolean ordered;
4141

4242
public Builder(boolean ordered) {
4343
this(BytesRefRecycler.NON_RECYCLING_INSTANCE, ordered);
4444
}
4545

4646
public Builder(Recycler<BytesRef> refRecycler, boolean ordered) {
47-
this.bytes = new RecyclerBytesStreamOutput(refRecycler);
47+
// this.bytes = new RecyclerBytesStreamOutput(refRecycler);
48+
this.bytes = new BytesStreamOutput(128);
4849
this.ordered = ordered;
4950
}
5051

@@ -67,7 +68,7 @@ public ESONObject parse(XContentParser parser) throws IOException {
6768
return rootObject;
6869
}
6970

70-
private static ESONObject parseObject(XContentParser parser, RecyclerBytesStreamOutput bytes, Supplier<Values> valuesSupplier)
71+
private static ESONObject parseObject(XContentParser parser, BytesStreamOutput bytes, Supplier<Values> valuesSupplier)
7172
throws IOException {
7273
Map<String, Type> map = new HashMap<>();
7374
String currentFieldName;
@@ -79,7 +80,7 @@ private static ESONObject parseObject(XContentParser parser, RecyclerBytesStream
7980
return new ESONObject(map, valuesSupplier);
8081
}
8182

82-
private static ESONArray parseArray(XContentParser parser, RecyclerBytesStreamOutput bytes, Supplier<Values> valuesSupplier)
83+
private static ESONArray parseArray(XContentParser parser, BytesStreamOutput bytes, Supplier<Values> valuesSupplier)
8384
throws IOException {
8485
List<Type> elements = new ArrayList<>();
8586
XContentParser.Token token;
@@ -92,7 +93,7 @@ private static ESONArray parseArray(XContentParser parser, RecyclerBytesStreamOu
9293

9394
private static Type parseValue(
9495
XContentParser parser,
95-
RecyclerBytesStreamOutput bytes,
96+
BytesStreamOutput bytes,
9697
Supplier<Values> valuesSupplier,
9798
XContentParser.Token token
9899
) throws IOException {
@@ -131,7 +132,7 @@ private static Type parseValue(
131132
}
132133
}
133134

134-
private static ValueType handleNumber(XContentParser parser, RecyclerBytesStreamOutput bytes) throws IOException {
135+
private static ValueType handleNumber(XContentParser parser, BytesStreamOutput bytes) throws IOException {
135136
switch (parser.numberType()) {
136137
case INT -> {
137138
int value = parser.intValue();
@@ -181,11 +182,11 @@ private static ValueType handleNumber(XContentParser parser, RecyclerBytesStream
181182
}
182183
}
183184

184-
private static void writeByteArray(RecyclerBytesStreamOutput bytes, byte[] value, int offset, int length) {
185+
private static void writeByteArray(BytesStreamOutput bytes, byte[] value, int offset, int length) {
185186
bytes.writeBytes(value, offset, length);
186187
}
187188

188-
private static void writeString(RecyclerBytesStreamOutput bytes, String value) throws IOException {
189+
private static void writeString(BytesStreamOutput bytes, String value) throws IOException {
189190
byte[] utf8Bytes = value.getBytes(java.nio.charset.StandardCharsets.UTF_8);
190191
writeByteArray(bytes, utf8Bytes, 0, utf8Bytes.length);
191192
}
@@ -391,13 +392,15 @@ public boolean remove(Object o) {
391392
@Override
392393
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
393394
builder.startObject();
395+
// TODO: Maybe need explicit null type to ensure not lost
394396
for (Entry<String, Type> entry : map.entrySet()) {
395397
builder.field(entry.getKey());
396398
switch (entry.getValue()) {
397399
case ESONObject o -> o.toXContent(builder, params);
398400
case ESONArray a -> a.toXContent(builder, params);
399401
case FixedValue v -> v.writeToXContent(builder, objectValues.get());
400402
case VariableValue v -> v.writeToXContent(builder, objectValues.get());
403+
case Mutation m -> builder.value(m.object());
401404
default -> throw new IllegalArgumentException("Unknown type: " + entry.getValue());
402405
}
403406
}
@@ -556,7 +559,14 @@ public Object getValue(Values source) {
556559
}
557560

558561
public void writeToXContent(XContentBuilder builder, Values values) throws IOException {
559-
builder.value(getValue(values));
562+
switch (valueType) {
563+
case INT -> builder.value(values.readInt(position));
564+
case LONG -> builder.value(values.readLong(position));
565+
case FLOAT -> builder.value(values.readFloat(position));
566+
case DOUBLE -> builder.value(values.readDouble(position));
567+
case BOOLEAN -> builder.value(values.readBoolean(position));
568+
default -> throw new IllegalArgumentException("Invalid value type: " + valueType);
569+
}
560570
}
561571
}
562572

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.logging.log4j.Logger;
1414
import org.apache.logging.log4j.util.Strings;
1515
import org.elasticsearch.ElasticsearchException;
16+
import org.elasticsearch.ElasticsearchGenerationException;
1617
import org.elasticsearch.ElasticsearchParseException;
1718
import org.elasticsearch.ExceptionsHelper;
1819
import org.elasticsearch.ResourceNotFoundException;
@@ -77,7 +78,9 @@
7778
import org.elasticsearch.script.ScriptService;
7879
import org.elasticsearch.threadpool.Scheduler;
7980
import org.elasticsearch.threadpool.ThreadPool;
81+
import org.elasticsearch.xcontent.ToXContent;
8082
import org.elasticsearch.xcontent.XContentBuilder;
83+
import org.elasticsearch.xcontent.XContentFactory;
8184
import org.elasticsearch.xcontent.XContentParser;
8285
import org.elasticsearch.xcontent.XContentParserConfiguration;
8386

@@ -1398,7 +1401,15 @@ private static void updateIndexRequestSource(final IndexRequest request, final I
13981401
// we already check for self references elsewhere (and clear the bit), so this should always be false,
13991402
// keeping the check and assert as a guard against extraordinarily surprising circumstances
14001403
assert ensureNoSelfReferences == false;
1401-
request.source(document.getSource(), request.getContentType(), ensureNoSelfReferences);
1404+
MapStructuredSource source = (MapStructuredSource) document.getSource();
1405+
ESONSource.ESONObject esonSource = (ESONSource.ESONObject) source.map();
1406+
try {
1407+
XContentBuilder builder = XContentFactory.contentBuilder(request.getContentType());
1408+
esonSource.toXContent(builder, ToXContent.EMPTY_PARAMS);
1409+
request.source(BytesReference.bytes(builder), builder.contentType());
1410+
} catch (IOException e) {
1411+
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
1412+
}
14021413
}
14031414

14041415
/**

0 commit comments

Comments
 (0)