Skip to content

Commit a8da31f

Browse files
committedMay 15, 2024
Separate serialization logic from class
Signed-off-by: Vacha Shah <vachshah@amazon.com>
1 parent 14f1c43 commit a8da31f

File tree

7 files changed

+454
-170
lines changed

7 files changed

+454
-170
lines changed
 

‎server/src/main/java/org/opensearch/common/cache/policy/CachedQueryResult.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public static void loadQSR(
5252
) throws IOException {
5353
StreamInput in = new NamedWriteableAwareStreamInput(serializedCQR.streamInput(), registry);
5454
PolicyValues pv = new PolicyValues(in); // Read and discard PolicyValues
55-
qsr.readFromWithId(id, in);
55+
qsr.getSerializer().readFromWithId(in);
5656
}
5757

5858
public void writeToNoId(StreamOutput out) throws IOException {
5959
policyValues.writeTo(out);
60-
qsr.writeToNoId(out);
60+
qsr.getSerializer().writeToNoId(out);
6161
}
6262

6363
/**

‎server/src/main/java/org/opensearch/search/fetch/FetchSearchResult.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.opensearch.search.SearchHits;
4040
import org.opensearch.search.SearchPhaseResult;
4141
import org.opensearch.search.SearchShardTarget;
42+
import org.opensearch.search.fetch.subphase.serializer.NativeFetchSearchResultSerializer;
4243
import org.opensearch.search.internal.ShardSearchContextId;
4344
import org.opensearch.search.query.QuerySearchResult;
4445

@@ -52,16 +53,16 @@
5253
@PublicApi(since = "1.0.0")
5354
public final class FetchSearchResult extends SearchPhaseResult {
5455

55-
private SearchHits hits;
5656
// client side counter
5757
private transient int counter;
58+
private final NativeFetchSearchResultSerializer serializer = new NativeFetchSearchResultSerializer();
5859

5960
public FetchSearchResult() {}
6061

6162
public FetchSearchResult(StreamInput in) throws IOException {
6263
super(in);
6364
contextId = new ShardSearchContextId(in);
64-
hits = new SearchHits(in);
65+
serializer.readFetchSearchResult(in);
6566
}
6667

6768
public FetchSearchResult(ShardSearchContextId id, SearchShardTarget shardTarget) {
@@ -81,7 +82,7 @@ public FetchSearchResult fetchResult() {
8182

8283
public void hits(SearchHits hits) {
8384
assert assertNoSearchTarget(hits);
84-
this.hits = hits;
85+
serializer.setHits(hits);
8586
}
8687

8788
private boolean assertNoSearchTarget(SearchHits hits) {
@@ -92,7 +93,7 @@ private boolean assertNoSearchTarget(SearchHits hits) {
9293
}
9394

9495
public SearchHits hits() {
95-
return hits;
96+
return serializer.getHits();
9697
}
9798

9899
public FetchSearchResult initCounter() {
@@ -107,6 +108,6 @@ public int counterGetAndIncrement() {
107108
@Override
108109
public void writeTo(StreamOutput out) throws IOException {
109110
contextId.writeTo(out);
110-
hits.writeTo(out);
111+
serializer.writeFetchSearchResult(out);
111112
}
112113
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.search.fetch.subphase.serializer;
10+
11+
import java.io.IOException;
12+
13+
public interface FetchSearchResultSerializer<T, V> {
14+
15+
void readFetchSearchResult(T in) throws IOException;
16+
17+
void writeFetchSearchResult(V out) throws IOException;
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.search.fetch.subphase.serializer;
10+
11+
import org.opensearch.core.common.io.stream.StreamInput;
12+
import org.opensearch.core.common.io.stream.StreamOutput;
13+
import org.opensearch.search.SearchHits;
14+
15+
import java.io.IOException;
16+
17+
public class NativeFetchSearchResultSerializer implements FetchSearchResultSerializer<StreamInput, StreamOutput> {
18+
19+
private SearchHits hits;
20+
21+
@Override
22+
public void readFetchSearchResult(StreamInput in) throws IOException {
23+
hits = new SearchHits(in);
24+
}
25+
26+
@Override
27+
public void writeFetchSearchResult(StreamOutput out) throws IOException {
28+
hits.writeTo(out);
29+
}
30+
31+
public SearchHits getHits() {
32+
return hits;
33+
}
34+
35+
public void setHits(SearchHits hits) {
36+
this.hits = hits;
37+
}
38+
39+
}

0 commit comments

Comments
 (0)