Skip to content

Commit 5e3c000

Browse files
Dry up o.e.c.lucene.Lucene transport logic (elastic#126646)
We've accumulated quited a bit of duplication here, drying this logic up a little.
1 parent 3d4011f commit 5e3c000

File tree

4 files changed

+51
-83
lines changed

4 files changed

+51
-83
lines changed

server/src/main/java/org/elasticsearch/common/lucene/Lucene.java

Lines changed: 44 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,15 @@ public static TopDocsAndMaxScore readTopDocs(StreamInput in) throws IOException
312312
} else if (type == 1) {
313313
TotalHits totalHits = readTotalHits(in);
314314
float maxScore = in.readFloat();
315-
SortField[] fields = in.readArray(Lucene::readSortField, SortField[]::new);
316-
FieldDoc[] fieldDocs = new FieldDoc[in.readVInt()];
317-
for (int i = 0; i < fieldDocs.length; i++) {
318-
fieldDocs[i] = readFieldDoc(in);
319-
}
315+
SortField[] fields = readSortFieldArray(in);
316+
FieldDoc[] fieldDocs = in.readArray(Lucene::readFieldDoc, FieldDoc[]::new);
320317
return new TopDocsAndMaxScore(new TopFieldDocs(totalHits, fieldDocs, fields), maxScore);
321318
} else if (type == 2) {
322319
TotalHits totalHits = readTotalHits(in);
323320
float maxScore = in.readFloat();
324321

325322
String field = in.readString();
326-
SortField[] fields = in.readArray(Lucene::readSortField, SortField[]::new);
323+
SortField[] fields = readSortFieldArray(in);
327324
int size = in.readVInt();
328325
Object[] collapseValues = new Object[size];
329326
FieldDoc[] fieldDocs = new FieldDoc[size];
@@ -338,65 +335,30 @@ public static TopDocsAndMaxScore readTopDocs(StreamInput in) throws IOException
338335
}
339336

340337
public static FieldDoc readFieldDoc(StreamInput in) throws IOException {
341-
Comparable<?>[] cFields = new Comparable<?>[in.readVInt()];
342-
for (int j = 0; j < cFields.length; j++) {
343-
byte type = in.readByte();
344-
if (type == 0) {
345-
cFields[j] = null;
346-
} else if (type == 1) {
347-
cFields[j] = in.readString();
348-
} else if (type == 2) {
349-
cFields[j] = in.readInt();
350-
} else if (type == 3) {
351-
cFields[j] = in.readLong();
352-
} else if (type == 4) {
353-
cFields[j] = in.readFloat();
354-
} else if (type == 5) {
355-
cFields[j] = in.readDouble();
356-
} else if (type == 6) {
357-
cFields[j] = in.readByte();
358-
} else if (type == 7) {
359-
cFields[j] = in.readShort();
360-
} else if (type == 8) {
361-
cFields[j] = in.readBoolean();
362-
} else if (type == 9) {
363-
cFields[j] = in.readBytesRef();
364-
} else if (type == 10) {
365-
cFields[j] = new BigInteger(in.readString());
366-
} else {
367-
throw new IOException("Can't match type [" + type + "]");
368-
}
369-
}
370-
return new FieldDoc(in.readVInt(), in.readFloat(), cFields);
338+
var sortValues = readSortValues(in);
339+
return new FieldDoc(in.readVInt(), in.readFloat(), sortValues);
340+
}
341+
342+
public static Object[] readSortValues(StreamInput in) throws IOException {
343+
return in.readArray(Lucene::readSortValue, Object[]::new);
371344
}
372345

373346
public static Comparable<?> readSortValue(StreamInput in) throws IOException {
374347
byte type = in.readByte();
375-
if (type == 0) {
376-
return null;
377-
} else if (type == 1) {
378-
return in.readString();
379-
} else if (type == 2) {
380-
return in.readInt();
381-
} else if (type == 3) {
382-
return in.readLong();
383-
} else if (type == 4) {
384-
return in.readFloat();
385-
} else if (type == 5) {
386-
return in.readDouble();
387-
} else if (type == 6) {
388-
return in.readByte();
389-
} else if (type == 7) {
390-
return in.readShort();
391-
} else if (type == 8) {
392-
return in.readBoolean();
393-
} else if (type == 9) {
394-
return in.readBytesRef();
395-
} else if (type == 10) {
396-
return new BigInteger(in.readString());
397-
} else {
398-
throw new IOException("Can't match type [" + type + "]");
399-
}
348+
return switch (type) {
349+
case 0 -> null;
350+
case 1 -> in.readString();
351+
case 2 -> in.readInt();
352+
case 3 -> in.readLong();
353+
case 4 -> in.readFloat();
354+
case 5 -> in.readDouble();
355+
case 6 -> in.readByte();
356+
case 7 -> in.readShort();
357+
case 8 -> in.readBoolean();
358+
case 9 -> in.readBytesRef();
359+
case 10 -> new BigInteger(in.readString());
360+
default -> throw new IOException("Can't match type [" + type + "]");
361+
};
400362
}
401363

402364
public static ScoreDoc readScoreDoc(StreamInput in) throws IOException {
@@ -425,7 +387,7 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
425387
out.writeByte((byte) 2);
426388
writeTotalHits(out, topDocs.totalHits);
427389
out.writeString(topFieldGroups.field);
428-
out.writeArray(Lucene::writeSortField, topFieldGroups.fields);
390+
writeSortFieldArray(out, topFieldGroups.fields);
429391
out.writeVInt(topDocs.scoreDocs.length);
430392
for (int i = 0; i < topDocs.scoreDocs.length; i++) {
431393
ScoreDoc doc = topFieldGroups.scoreDocs[i];
@@ -436,7 +398,7 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
436398
} else if (topDocs instanceof TopFieldDocs topFieldDocs) {
437399
out.writeByte((byte) 1);
438400
writeTotalHits(out, topDocs.totalHits);
439-
out.writeArray(Lucene::writeSortField, topFieldDocs.fields);
401+
writeSortFieldArray(out, topFieldDocs.fields);
440402
out.writeArray((o, doc) -> {
441403
writeFieldDoc(o, (FieldDoc) doc);
442404
o.writeVInt(doc.shardIndex);
@@ -451,6 +413,10 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
451413
}
452414
}
453415

416+
public static void writeSortFieldArray(StreamOutput out, SortField[] sortFields) throws IOException {
417+
out.writeArray(Lucene::writeSortField, sortFields);
418+
}
419+
454420
/**
455421
* Read side counterpart to {@link #writeTopDocsIncludingShardIndex} and the same as {@link #readTopDocs(StreamInput)} but for the
456422
* added shard index values that are read.
@@ -473,7 +439,7 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
473439
return new TopDocs(totalHits, scoreDocs);
474440
} else if (type == 1) {
475441
TotalHits totalHits = readTotalHits(in);
476-
SortField[] fields = in.readArray(Lucene::readSortField, SortField[]::new);
442+
SortField[] fields = readSortFieldArray(in);
477443
FieldDoc[] fieldDocs = new FieldDoc[in.readVInt()];
478444
for (int i = 0; i < fieldDocs.length; i++) {
479445
var fieldDoc = readFieldDoc(in);
@@ -484,7 +450,7 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
484450
} else if (type == 2) {
485451
TotalHits totalHits = readTotalHits(in);
486452
String field = in.readString();
487-
SortField[] fields = in.readArray(Lucene::readSortField, SortField[]::new);
453+
SortField[] fields = readSortFieldArray(in);
488454
int size = in.readVInt();
489455
Object[] collapseValues = new Object[size];
490456
FieldDoc[] fieldDocs = new FieldDoc[size];
@@ -500,6 +466,10 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
500466
}
501467
}
502468

469+
public static SortField[] readSortFieldArray(StreamInput in) throws IOException {
470+
return in.readArray(Lucene::readSortField, SortField[]::new);
471+
}
472+
503473
public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) throws IOException {
504474
if (topDocs.topDocs instanceof TopFieldGroups topFieldGroups) {
505475
out.writeByte((byte) 2);
@@ -508,7 +478,7 @@ public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) th
508478
out.writeFloat(topDocs.maxScore);
509479

510480
out.writeString(topFieldGroups.field);
511-
out.writeArray(Lucene::writeSortField, topFieldGroups.fields);
481+
writeSortFieldArray(out, topFieldGroups.fields);
512482

513483
out.writeVInt(topFieldGroups.scoreDocs.length);
514484
for (int i = 0; i < topFieldGroups.scoreDocs.length; i++) {
@@ -522,7 +492,7 @@ public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) th
522492
writeTotalHits(out, topFieldDocs.totalHits);
523493
out.writeFloat(topDocs.maxScore);
524494

525-
out.writeArray(Lucene::writeSortField, topFieldDocs.fields);
495+
writeSortFieldArray(out, topFieldDocs.fields);
526496
out.writeArray((o, doc) -> writeFieldDoc(o, (FieldDoc) doc), topFieldDocs.scoreDocs);
527497
} else {
528498
out.writeByte((byte) 0);
@@ -597,14 +567,17 @@ public static void writeSortValue(StreamOutput out, Object field) throws IOExcep
597567

598568
public static void writeFieldDoc(StreamOutput out, FieldDoc fieldDoc) throws IOException {
599569
out.writeArray(Lucene::writeSortValue, fieldDoc.fields);
600-
out.writeVInt(fieldDoc.doc);
601-
out.writeFloat(fieldDoc.score);
570+
doWriteScoreDoc(out, fieldDoc);
602571
}
603572

604573
public static void writeScoreDoc(StreamOutput out, ScoreDoc scoreDoc) throws IOException {
605574
if (scoreDoc.getClass().equals(ScoreDoc.class) == false) {
606575
throw new IllegalArgumentException("This method can only be used to serialize a ScoreDoc, not a " + scoreDoc.getClass());
607576
}
577+
doWriteScoreDoc(out, scoreDoc);
578+
}
579+
580+
private static void doWriteScoreDoc(StreamOutput out, ScoreDoc scoreDoc) throws IOException {
608581
out.writeVInt(scoreDoc.doc);
609582
out.writeFloat(scoreDoc.score);
610583
}
@@ -661,17 +634,12 @@ private static SortField rewriteMergeSortField(SortField sortField) {
661634
}
662635
}
663636

664-
public static void writeSortField(StreamOutput out, SortField sortField) throws IOException {
637+
static void writeSortField(StreamOutput out, SortField sortField) throws IOException {
665638
sortField = rewriteMergeSortField(sortField);
666639
if (sortField.getClass() != SortField.class) {
667640
throw new IllegalArgumentException("Cannot serialize SortField impl [" + sortField + "]");
668641
}
669-
if (sortField.getField() == null) {
670-
out.writeBoolean(false);
671-
} else {
672-
out.writeBoolean(true);
673-
out.writeString(sortField.getField());
674-
}
642+
out.writeOptionalString(sortField.getField());
675643
if (sortField.getComparatorSource() != null) {
676644
IndexFieldData.XFieldComparatorSource comparatorSource = (IndexFieldData.XFieldComparatorSource) sortField
677645
.getComparatorSource();

server/src/main/java/org/elasticsearch/search/SearchHits.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ public static SearchHits readFrom(StreamInput in, boolean pooled) throws IOExcep
139139
isPooled = isPooled || hit.isPooled();
140140
}
141141
}
142-
var sortFields = in.readOptionalArray(Lucene::readSortField, SortField[]::new);
142+
var sortFields = in.readOptional(Lucene::readSortFieldArray);
143143
var collapseField = in.readOptionalString();
144-
var collapseValues = in.readOptionalArray(Lucene::readSortValue, Object[]::new);
144+
var collapseValues = in.readOptional(Lucene::readSortValues);
145145
if (isPooled) {
146146
return new SearchHits(hits, totalHits, maxScore, sortFields, collapseField, collapseValues);
147147
} else {
@@ -163,7 +163,7 @@ public void writeTo(StreamOutput out) throws IOException {
163163
}
164164
out.writeFloat(maxScore);
165165
out.writeArray(hits);
166-
out.writeOptionalArray(Lucene::writeSortField, sortFields);
166+
out.writeOptional(Lucene::writeSortFieldArray, sortFields);
167167
out.writeOptionalString(collapseField);
168168
out.writeOptionalArray(Lucene::writeSortValue, collapseValues);
169169
}

server/src/main/java/org/elasticsearch/search/SearchSortValues.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public SearchSortValues(Object[] rawSortValues, DocValueFormat[] sortValueFormat
5353
}
5454

5555
public static SearchSortValues readFrom(StreamInput in) throws IOException {
56-
Object[] formattedSortValues = in.readArray(Lucene::readSortValue, Object[]::new);
57-
Object[] rawSortValues = in.readArray(Lucene::readSortValue, Object[]::new);
56+
Object[] formattedSortValues = Lucene.readSortValues(in);
57+
Object[] rawSortValues = Lucene.readSortValues(in);
5858
if (formattedSortValues.length == 0 && rawSortValues.length == 0) {
5959
return EMPTY;
6060
}

server/src/main/java/org/elasticsearch/search/SearchSortValuesAndFormats.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public SearchSortValuesAndFormats(Object[] rawSortValues, DocValueFormat[] sortV
4848
}
4949

5050
public SearchSortValuesAndFormats(StreamInput in) throws IOException {
51-
this.rawSortValues = in.readArray(Lucene::readSortValue, Object[]::new);
52-
this.formattedSortValues = in.readArray(Lucene::readSortValue, Object[]::new);
51+
this.rawSortValues = Lucene.readSortValues(in);
52+
this.formattedSortValues = Lucene.readSortValues(in);
5353
this.sortValueFormats = new DocValueFormat[formattedSortValues.length];
5454
for (int i = 0; i < sortValueFormats.length; ++i) {
5555
sortValueFormats[i] = in.readNamedWriteable(DocValueFormat.class);

0 commit comments

Comments
 (0)