From ebadf6ff095385bc6eeecdaf0de31fdf9e835b09 Mon Sep 17 00:00:00 2001 From: Finn Carroll Date: Mon, 26 Aug 2024 17:30:36 -0700 Subject: [PATCH] Spotless apply Signed-off-by: Finn Carroll --- .../org/opensearch/search/SearchHits.java | 2 +- .../serde/FetchSearchResultSerDe.java | 1 - .../transport/serde/SearchHitSerDe.java | 34 +++++-------------- .../transport/serde/SearchHitsSerDe.java | 14 +++----- .../org/opensearch/transport/serde/SerDe.java | 23 ++++++------- 5 files changed, 26 insertions(+), 48 deletions(-) diff --git a/server/src/main/java/org/opensearch/search/SearchHits.java b/server/src/main/java/org/opensearch/search/SearchHits.java index 6b385d566116f..69584e4c1076d 100644 --- a/server/src/main/java/org/opensearch/search/SearchHits.java +++ b/server/src/main/java/org/opensearch/search/SearchHits.java @@ -133,7 +133,7 @@ public SearchHits(StreamInput in) throws IOException { collapseValues = in.readOptionalArray(Lucene::readSortValue, Object[]::new); } - protected SearchHits () {} + protected SearchHits() {} @Override public void writeTo(StreamOutput out) throws IOException { diff --git a/server/src/main/java/org/opensearch/transport/serde/FetchSearchResultSerDe.java b/server/src/main/java/org/opensearch/transport/serde/FetchSearchResultSerDe.java index d230d1eeb4f87..f48d334ad3a2c 100644 --- a/server/src/main/java/org/opensearch/transport/serde/FetchSearchResultSerDe.java +++ b/server/src/main/java/org/opensearch/transport/serde/FetchSearchResultSerDe.java @@ -60,7 +60,6 @@ public void toProtobufStream(StreamOutput out) throws IOException { toProto().writeTo(out); } - @Override public void fromProtobufStream(StreamInput in) throws IOException { FetchSearchResultProto proto = FetchSearchResultProto.parseFrom(in); diff --git a/server/src/main/java/org/opensearch/transport/serde/SearchHitSerDe.java b/server/src/main/java/org/opensearch/transport/serde/SearchHitSerDe.java index a9b9a53800b71..256622a4a033b 100644 --- a/server/src/main/java/org/opensearch/transport/serde/SearchHitSerDe.java +++ b/server/src/main/java/org/opensearch/transport/serde/SearchHitSerDe.java @@ -20,8 +20,8 @@ import org.opensearch.search.SearchShardTarget; import org.opensearch.search.SearchSortValues; import org.opensearch.search.fetch.subphase.highlight.HighlightField; -import org.opensearch.serde.proto.SearchHitsTransportProto.SearchHitProto; import org.opensearch.serde.proto.SearchHitsTransportProto.NestedIdentityProto; +import org.opensearch.serde.proto.SearchHitsTransportProto.SearchHitProto; import java.io.IOException; import java.util.HashMap; @@ -197,17 +197,11 @@ SearchHitProto toProto() { builder.setExplanation(explanationToProto(explanation)); builder.setSortValues(searchSortValuesToProto(sortValues)); - documentFields.forEach((key, value) -> - builder.putDocumentFields(key, documentFieldToProto(value)) - ); + documentFields.forEach((key, value) -> builder.putDocumentFields(key, documentFieldToProto(value))); - metaFields.forEach((key, value) -> - builder.putMetaFields(key, documentFieldToProto(value)) - ); + metaFields.forEach((key, value) -> builder.putMetaFields(key, documentFieldToProto(value))); - highlightFields.forEach((key, value) -> - builder.putHighlightFields(key, highlightFieldToProto(value)) - ); + highlightFields.forEach((key, value) -> builder.putHighlightFields(key, highlightFieldToProto(value))); matchedQueries.forEach(builder::putMatchedQueries); @@ -216,9 +210,7 @@ SearchHitProto toProto() { builder.setShard(searchShardTargetToProto(shard)); } - innerHits.forEach((key, value) -> - builder.putInnerHits(key, new SearchHitsSerDe(value, strategy).toProto()) - ); + innerHits.forEach((key, value) -> builder.putInnerHits(key, new SearchHitsSerDe(value, strategy).toProto())); return builder.build(); } @@ -237,24 +229,16 @@ void fromProto(SearchHitProto proto) throws SerDe.SerializationException { matchedQueries = proto.getMatchedQueriesMap(); documentFields = new HashMap<>(); - proto.getDocumentFieldsMap().forEach((key, value) -> - documentFields.put(key, documentFieldFromProto(value)) - ); + proto.getDocumentFieldsMap().forEach((key, value) -> documentFields.put(key, documentFieldFromProto(value))); metaFields = new HashMap<>(); - proto.getMetaFieldsMap().forEach((key, value) -> - metaFields.put(key, documentFieldFromProto(value)) - ); + proto.getMetaFieldsMap().forEach((key, value) -> metaFields.put(key, documentFieldFromProto(value))); highlightFields = new HashMap<>(); - proto.getHighlightFieldsMap().forEach((key, value) -> - highlightFields.put(key, highlightFieldFromProto(value)) - ); + proto.getHighlightFieldsMap().forEach((key, value) -> highlightFields.put(key, highlightFieldFromProto(value))); innerHits = new HashMap<>(); - proto.getInnerHitsMap().forEach((key, value) -> - innerHits.put(key, new SearchHitsSerDe(value)) - ); + proto.getInnerHitsMap().forEach((key, value) -> innerHits.put(key, new SearchHitsSerDe(value))); shard = searchShardTargetFromProto(proto.getShard()); index = shard.getIndex(); diff --git a/server/src/main/java/org/opensearch/transport/serde/SearchHitsSerDe.java b/server/src/main/java/org/opensearch/transport/serde/SearchHitsSerDe.java index d9e49c4771799..9433d5446e271 100644 --- a/server/src/main/java/org/opensearch/transport/serde/SearchHitsSerDe.java +++ b/server/src/main/java/org/opensearch/transport/serde/SearchHitsSerDe.java @@ -111,31 +111,27 @@ public void fromNativeStream(StreamInput in) throws IOException { } SearchHitsProto toProto() { - SearchHitsProto.Builder builder = SearchHitsProto.newBuilder() - .setMaxScore(maxScore) - .setCollapseField(collapseField); + SearchHitsProto.Builder builder = SearchHitsProto.newBuilder().setMaxScore(maxScore).setCollapseField(collapseField); for (SearchHit hit : hits) { builder.addHits(new SearchHitSerDe(hit, strategy).toProto()); } TotalHits totHits = totalHits; - TotalHitsProto.Builder totHitsBuilder = TotalHitsProto.newBuilder() - .setRelation(totHits.relation.ordinal()) - .setValue(totHits.value); + TotalHitsProto.Builder totHitsBuilder = TotalHitsProto.newBuilder().setRelation(totHits.relation.ordinal()).setValue(totHits.value); builder.setTotalHits(totHitsBuilder); try (BytesStreamOutput sortOut = new BytesStreamOutput()) { sortOut.writeOptionalArray(Lucene::writeSortField, sortFields); builder.setSortFields(ByteString.copyFrom(sortOut.bytes().toBytesRef().bytes)); - } catch (IOException e){ + } catch (IOException e) { throw new SerDe.SerializationException("Failed to serialize SearchHits to proto", e); } try (BytesStreamOutput collapseOut = new BytesStreamOutput()) { collapseOut.writeOptionalArray(Lucene::writeSortValue, collapseValues); builder.setCollapseValues(ByteString.copyFrom(collapseOut.bytes().toBytesRef().bytes)); - } catch (IOException e){ + } catch (IOException e) { throw new SerDe.SerializationException("Failed to serialize SearchHits to proto", e); } @@ -167,7 +163,7 @@ void fromProto(SearchHitsProto proto) throws SerDe.SerializationException { } hits = new SearchHit[proto.getHitsCount()]; - for(int i = 0; i < hits.length; i++) { + for (int i = 0; i < hits.length; i++) { try { hits[i] = new SearchHitSerDe(proto.getHits(i)); } catch (IOException e) { diff --git a/server/src/main/java/org/opensearch/transport/serde/SerDe.java b/server/src/main/java/org/opensearch/transport/serde/SerDe.java index 286dae6e83323..fac079c6b9a87 100644 --- a/server/src/main/java/org/opensearch/transport/serde/SerDe.java +++ b/server/src/main/java/org/opensearch/transport/serde/SerDe.java @@ -24,12 +24,12 @@ import org.opensearch.search.SearchSortValues; import org.opensearch.search.fetch.subphase.highlight.HighlightField; import org.opensearch.serde.proto.SearchHitsTransportProto.DocumentFieldProto; +import org.opensearch.serde.proto.SearchHitsTransportProto.ExplanationProto; import org.opensearch.serde.proto.SearchHitsTransportProto.HighlightFieldProto; -import org.opensearch.serde.proto.SearchHitsTransportProto.SearchSortValuesProto; +import org.opensearch.serde.proto.SearchHitsTransportProto.IndexProto; import org.opensearch.serde.proto.SearchHitsTransportProto.SearchShardTargetProto; -import org.opensearch.serde.proto.SearchHitsTransportProto.ExplanationProto; +import org.opensearch.serde.proto.SearchHitsTransportProto.SearchSortValuesProto; import org.opensearch.serde.proto.SearchHitsTransportProto.ShardIdProto; -import org.opensearch.serde.proto.SearchHitsTransportProto.IndexProto; import java.io.IOException; import java.util.ArrayList; @@ -55,6 +55,7 @@ public static class SerializationException extends RuntimeException { public SerializationException(String message) { super(message); } + public SerializationException(String message, Throwable cause) { super(message, cause); } @@ -62,11 +63,13 @@ public SerializationException(String message, Throwable cause) { interface nativeSerializer { void toNativeStream(StreamOutput out) throws IOException; + void fromNativeStream(StreamInput in) throws IOException; } interface protobufSerializer { void toProtobufStream(StreamOutput out) throws IOException; + void fromProtobufStream(StreamInput in) throws IOException; } @@ -108,7 +111,7 @@ static DocumentFieldProto documentFieldToProto(DocumentField field) { try (BytesStreamOutput docsOut = new BytesStreamOutput()) { docsOut.writeCollection(field.getValues(), StreamOutput::writeGenericValue); builder.addValues(ByteString.copyFrom(docsOut.bytes().toBytesRef().bytes)); - } catch (IOException e){ + } catch (IOException e) { builder.addValues(ByteString.EMPTY); } @@ -133,8 +136,7 @@ static DocumentField documentFieldFromProto(DocumentFieldProto proto) throws Ser } static HighlightFieldProto highlightFieldToProto(HighlightField field) { - HighlightFieldProto.Builder builder = HighlightFieldProto.newBuilder() - .setName(field.getName()); + HighlightFieldProto.Builder builder = HighlightFieldProto.newBuilder().setName(field.getName()); for (Text frag : field.getFragments()) { builder.addFragments(frag.string()); @@ -161,14 +163,14 @@ static SearchSortValuesProto searchSortValuesToProto(SearchSortValues searchSort try (BytesStreamOutput formOut = new BytesStreamOutput()) { formOut.writeArray(Lucene::writeSortValue, searchSortValues.getFormattedSortValues()); builder.addFormattedSortValues(ByteString.copyFrom(formOut.bytes().toBytesRef().bytes)); - } catch (IOException e){ + } catch (IOException e) { builder.addFormattedSortValues(ByteString.EMPTY); } try (BytesStreamOutput rawOut = new BytesStreamOutput()) { rawOut.writeArray(Lucene::writeSortValue, searchSortValues.getFormattedSortValues()); builder.addRawSortValues(ByteString.copyFrom(rawOut.bytes().toBytesRef().bytes)); - } catch (IOException e){ + } catch (IOException e) { builder.addRawSortValues(ByteString.EMPTY); } @@ -230,10 +232,7 @@ public static ShardId shardIdFromProto(ShardIdProto proto) { } static IndexProto indexToProto(Index index) { - return IndexProto.newBuilder() - .setName(index.getName()) - .setUuid(index.getUUID()) - .build(); + return IndexProto.newBuilder().setName(index.getName()).setUuid(index.getUUID()).build(); } public static Index indexFromProto(IndexProto proto) {