Skip to content

Commit 0c2a00d

Browse files
committed
Improvements
1 parent f5f6b52 commit 0c2a00d

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

server/src/main/java/org/elasticsearch/action/index/IndexRequest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.index.VersionType;
3939
import org.elasticsearch.index.mapper.MapperService;
4040
import org.elasticsearch.index.shard.ShardId;
41+
import org.elasticsearch.ingest.ESONFlat;
4142
import org.elasticsearch.ingest.ESONIndexed;
4243
import org.elasticsearch.ingest.ESONSource;
4344
import org.elasticsearch.ingest.ESONXContentSerializer;
@@ -105,7 +106,7 @@ public class IndexRequest extends ReplicatedWriteRequest<IndexRequest> implement
105106
private String routing;
106107

107108
private BytesReference source;
108-
private ESONIndexed.ESONObject structuredSource;
109+
private ESONFlat structuredSource;
109110
private boolean useStructuredSource = false;
110111

111112
private OpType opType = OpType.INDEX;
@@ -425,7 +426,7 @@ public BytesReference source() {
425426

426427
public void setStructuredSource(ESONIndexed.ESONObject esonSource) {
427428
this.useStructuredSource = true;
428-
this.structuredSource = esonSource;
429+
this.structuredSource = esonSource.esonFlat();
429430
try (XContentBuilder builder = XContentFactory.contentBuilder(contentType)) {
430431
ESONXContentSerializer.flattenToXContent(esonSource.esonFlat(), builder, ToXContent.EMPTY_PARAMS);
431432
source = BytesReference.bytes(builder);
@@ -444,7 +445,7 @@ public void ensureStructureSource() {
444445
private void createStructuredSource() {
445446
ESONSource.Builder builder = new ESONSource.Builder((int) (source.length() * 0.70));
446447
try (XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, source, contentType)) {
447-
structuredSource = builder.parse(parser);
448+
structuredSource = builder.parse(parser).esonFlat();
448449
} catch (IOException e) {
449450
throw new UncheckedIOException(e);
450451
}
@@ -455,13 +456,13 @@ public boolean isStructuredSource() {
455456
}
456457

457458
public ESONIndexed.ESONObject structuredSource() {
458-
return structuredSource;
459+
return ESONIndexed.fromFlat(structuredSource);
459460
}
460461

461462
public Map<String, Object> sourceAsMap() {
462463
if (useStructuredSource) {
463464
assert structuredSource != null;
464-
return structuredSource;
465+
return ESONIndexed.fromFlat(structuredSource);
465466
} else {
466467
return XContentHelper.convertToMap(source, false, contentType).v2();
467468
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535

3636
public class ESONIndexed {
3737

38+
public static ESONObject fromFlat(ESONFlat esonFlat) {
39+
return new ESONObject(0, esonFlat);
40+
}
41+
3842
public static class ESONObject implements ESONSource.Value, Map<String, Object>, ToXContent {
3943
private final int keyArrayIndex;
4044
private final ESONEntry.ObjectEntry objEntry;
@@ -533,7 +537,8 @@ private static int skipContainer(List<ESONEntry> keyArray, ESONEntry entry, int
533537
}
534538

535539
public static ESONIndexed.ESONObject flatten(ESONIndexed.ESONObject original) {
536-
BytesStreamOutput newValuesOut = new BytesStreamOutput();
540+
// TODO: Add a better estimate of the size to be added
541+
BytesStreamOutput newValuesOut = new BytesStreamOutput(128);
537542
List<ESONEntry> flatKeyArray = new ArrayList<>(original.esonFlat.keys().size());
538543
BytesReference originalData = original.esonFlat.values().data();
539544

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,13 @@ public Object getValue(Values source) {
226226
}
227227

228228
public void writeToXContent(XContentBuilder builder, Values values) throws IOException {
229+
BytesReference slice = values.data.slice(position, length);
230+
229231
byte[] bytes;
230232
int offset;
231-
if (values.data().hasArray()) {
232-
BytesRef bytesRef = values.data().toBytesRef();
233-
bytes = bytesRef.bytes;
234-
offset = bytesRef.offset + position;
233+
if (slice.hasArray()) {
234+
bytes = slice.array();
235+
offset = slice.arrayOffset() + position;
235236
} else {
236237
bytes = values.readByteArray(position, length);
237238
offset = 0;
@@ -279,12 +280,13 @@ private byte[] readByteArray(int position, int length) {
279280
}
280281

281282
public String readString(int position, int length) {
283+
BytesReference slice = data.slice(position, length);
284+
282285
final byte[] bytes;
283286
final int offset;
284-
// TODO: Maybe slice to improve chance of backing array
285-
if (data.hasArray()) {
286-
bytes = data.array();
287-
offset = data.arrayOffset() + position;
287+
if (slice.hasArray()) {
288+
bytes = slice.array();
289+
offset = slice.arrayOffset() + position;
288290
} else {
289291
bytes = readByteArray(position, length);
290292
offset = 0;

0 commit comments

Comments
 (0)