Skip to content

Commit cfdf651

Browse files
committed
Improve serialization of big integer
Signed-off-by: Sandesh Kumar <[email protected]>
1 parent eb28e77 commit cfdf651

File tree

2 files changed

+49
-52
lines changed

2 files changed

+49
-52
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
4040
- Optimized date histogram aggregations by preventing unnecessary object allocations in date rounding utils ([19088](https://github.com/opensearch-project/OpenSearch/pull/19088))
4141
- Optimize source conversion in gRPC search hits using zero-copy BytesRef ([#19280](https://github.com/opensearch-project/OpenSearch/pull/19280))
4242
- Add failureaccess as runtime dependency to transport-grpc module ([#19339](https://github.com/opensearch-project/OpenSearch/pull/19339))
43+
- Improve serialization of BigInteger in Lucene.java ([19348](https://github.com/opensearch-project/OpenSearch/pull/19348)))
4344

4445
### Fixed
4546
- Fix unnecessary refreshes on update preparation failures ([#15261](https://github.com/opensearch-project/OpenSearch/issues/15261))

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

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private static SegmentInfos readSegmentInfos(String segmentsFileName, Directory
198198
*/
199199
public static SegmentInfos pruneUnreferencedFiles(String segmentsFileName, Directory directory) throws IOException {
200200
final SegmentInfos si = readSegmentInfos(segmentsFileName, directory);
201-
try (Lock writeLock = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
201+
try (Lock ignored = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
202202
int foundSegmentFiles = 0;
203203
for (final String file : directory.listAll()) {
204204
/*
@@ -222,7 +222,7 @@ public static SegmentInfos pruneUnreferencedFiles(String segmentsFileName, Direc
222222
}
223223
final IndexCommit cp = getIndexCommit(si, directory);
224224
try (
225-
IndexWriter writer = new IndexWriter(
225+
IndexWriter ignored = new IndexWriter(
226226
directory,
227227
new IndexWriterConfig(Lucene.STANDARD_ANALYZER).setSoftDeletesField(Lucene.SOFT_DELETES_FIELD)
228228
.setIndexCommit(cp)
@@ -249,15 +249,15 @@ public static IndexCommit getIndexCommit(SegmentInfos si, Directory directory) t
249249
* this operation fails.
250250
*/
251251
public static void cleanLuceneIndex(Directory directory) throws IOException {
252-
try (Lock writeLock = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
252+
try (Lock ignored = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
253253
for (final String file : directory.listAll()) {
254254
if (file.startsWith(IndexFileNames.SEGMENTS)) {
255255
directory.deleteFile(file); // remove all segment_N files
256256
}
257257
}
258258
}
259259
try (
260-
IndexWriter writer = new IndexWriter(
260+
IndexWriter ignored = new IndexWriter(
261261
directory,
262262
new IndexWriterConfig(Lucene.STANDARD_ANALYZER).setSoftDeletesField(Lucene.SOFT_DELETES_FIELD)
263263
.setMergePolicy(NoMergePolicy.INSTANCE) // no merges
@@ -388,7 +388,7 @@ private static Comparable readTypedValue(StreamInput in) throws IOException {
388388
case 7 -> in.readShort();
389389
case 8 -> in.readBoolean();
390390
case 9 -> in.readBytesRef();
391-
case 10 -> new BigInteger(in.readString());
391+
case 10 -> new BigInteger(in.readByteArray());
392392
default -> throw new IOException("Can't match type [" + type + "]");
393393
};
394394
}
@@ -405,9 +405,8 @@ public static void writeTotalHits(StreamOutput out, TotalHits totalHits) throws
405405
}
406406

407407
public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) throws IOException {
408-
if (topDocs.topDocs instanceof CollapseTopFieldDocs) {
408+
if (topDocs.topDocs instanceof CollapseTopFieldDocs collapseDocs) {
409409
out.writeByte((byte) 2);
410-
CollapseTopFieldDocs collapseDocs = (CollapseTopFieldDocs) topDocs.topDocs;
411410

412411
writeTotalHits(out, topDocs.topDocs.totalHits);
413412
out.writeFloat(topDocs.maxScore);
@@ -421,9 +420,8 @@ public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) th
421420
writeFieldDoc(out, (FieldDoc) doc);
422421
writeSortValue(out, collapseDocs.collapseValues[i]);
423422
}
424-
} else if (topDocs.topDocs instanceof TopFieldDocs) {
423+
} else if (topDocs.topDocs instanceof TopFieldDocs topFieldDocs) {
425424
out.writeByte((byte) 1);
426-
TopFieldDocs topFieldDocs = (TopFieldDocs) topDocs.topDocs;
427425

428426
writeTotalHits(out, topDocs.topDocs.totalHits);
429427
out.writeFloat(topDocs.maxScore);
@@ -459,57 +457,58 @@ private static void writeMissingValue(StreamOutput out, Object missingValue) thr
459457

460458
private static Object readMissingValue(StreamInput in) throws IOException {
461459
final byte id = in.readByte();
462-
switch (id) {
463-
case 0:
464-
return in.readGenericValue();
465-
case 1:
466-
return SortField.STRING_FIRST;
467-
case 2:
468-
return SortField.STRING_LAST;
469-
default:
470-
throw new IOException("Unknown missing value id: " + id);
471-
}
460+
return switch (id) {
461+
case 0 -> in.readGenericValue();
462+
case 1 -> SortField.STRING_FIRST;
463+
case 2 -> SortField.STRING_LAST;
464+
default -> throw new IOException("Unknown missing value id: " + id);
465+
};
472466
}
473467

474468
public static void writeSortValue(StreamOutput out, Object field) throws IOException {
475-
if (field == null) {
476-
out.writeByte((byte) 0);
477-
} else {
478-
Class type = field.getClass();
479-
if (type == String.class) {
469+
switch (field) {
470+
case null -> out.writeByte((byte) 0);
471+
case String s -> {
480472
out.writeByte((byte) 1);
481-
out.writeString((String) field);
482-
} else if (type == Integer.class) {
473+
out.writeString(s);
474+
}
475+
case Integer i -> {
483476
out.writeByte((byte) 2);
484-
out.writeInt((Integer) field);
485-
} else if (type == Long.class) {
477+
out.writeInt(i);
478+
}
479+
case Long l -> {
486480
out.writeByte((byte) 3);
487-
out.writeLong((Long) field);
488-
} else if (type == Float.class) {
481+
out.writeLong(l);
482+
}
483+
case Float f -> {
489484
out.writeByte((byte) 4);
490-
out.writeFloat((Float) field);
491-
} else if (type == Double.class) {
485+
out.writeFloat(f);
486+
}
487+
case Double d -> {
492488
out.writeByte((byte) 5);
493-
out.writeDouble((Double) field);
494-
} else if (type == Byte.class) {
489+
out.writeDouble(d);
490+
}
491+
case Byte b -> {
495492
out.writeByte((byte) 6);
496-
out.writeByte((Byte) field);
497-
} else if (type == Short.class) {
493+
out.writeByte(b);
494+
}
495+
case Short s -> {
498496
out.writeByte((byte) 7);
499-
out.writeShort((Short) field);
500-
} else if (type == Boolean.class) {
497+
out.writeShort(s);
498+
}
499+
case Boolean b -> {
501500
out.writeByte((byte) 8);
502-
out.writeBoolean((Boolean) field);
503-
} else if (type == BytesRef.class) {
501+
out.writeBoolean(b);
502+
}
503+
case BytesRef b -> {
504504
out.writeByte((byte) 9);
505-
out.writeBytesRef((BytesRef) field);
506-
} else if (type == BigInteger.class) {
507-
// TODO: improve serialization of BigInteger
505+
out.writeBytesRef(b);
506+
}
507+
case BigInteger i -> {
508508
out.writeByte((byte) 10);
509-
out.writeString(field.toString());
510-
} else {
511-
throw new IOException("Can't handle sort field value of type [" + type + "]");
509+
out.writeByteArray(i.toByteArray());
512510
}
511+
default -> throw new IOException("Can't handle sort field value of type [" + field.getClass() + "]");
513512
}
514513
}
515514

@@ -577,13 +576,12 @@ public static void writeSortField(StreamOutput out, SortField sortField) throws
577576
);
578577
newSortField.setMissingValue(sortField.getMissingValue());
579578
sortField = newSortField;
580-
} else if (sortField instanceof NonPruningSortField) {
579+
} else if (sortField instanceof NonPruningSortField nonPruningSortField) {
581580
// There are 2 cases of how NonPruningSortField wraps around its underlying sort field.
582581
// Which are through the SortField class or SortedSetSortField class
583582
// We will serialize the sort field based on the type of underlying sort field
584583
// Here the underlying sort field is SortedSetSortField, therefore, we will follow the
585584
// logic in serializing SortedSetSortField and also unwrap the SortField case.
586-
NonPruningSortField nonPruningSortField = (NonPruningSortField) sortField;
587585
if (nonPruningSortField.getDelegate().getClass() == SortedSetSortField.class) {
588586
SortField newSortField = new SortField(
589587
nonPruningSortField.getField(),
@@ -699,11 +697,9 @@ public static Version parseVersionLenient(String toParse, Version defaultValue)
699697
public static SegmentReader segmentReader(LeafReader reader) {
700698
if (reader instanceof SegmentReader) {
701699
return (SegmentReader) reader;
702-
} else if (reader instanceof FilterLeafReader) {
703-
final FilterLeafReader fReader = (FilterLeafReader) reader;
700+
} else if (reader instanceof FilterLeafReader fReader) {
704701
return segmentReader(FilterLeafReader.unwrap(fReader));
705-
} else if (reader instanceof FilterCodecReader) {
706-
final FilterCodecReader fReader = (FilterCodecReader) reader;
702+
} else if (reader instanceof FilterCodecReader fReader) {
707703
return segmentReader(FilterCodecReader.unwrap(fReader));
708704
}
709705
// hard fail - we can't get a SegmentReader

0 commit comments

Comments
 (0)