Skip to content

Commit 49335bd

Browse files
committed
Add Builder in CompletionStats and refactor usage
Signed-off-by: Jean Kim <bgshhd95@gmail.com>
1 parent ea8a35b commit 49335bd

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

server/src/main/java/org/opensearch/index/engine/CompletionStatsCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ CompletionStats get(String... fieldNamePatterns) {
116116
}
117117
}
118118

119-
return new CompletionStats(sizeInBytes, new FieldMemoryStats(completionFields));
119+
return new CompletionStats.Builder().sizeInBytes(sizeInBytes).fields(new FieldMemoryStats(completionFields)).build();
120120
});
121121

122122
boolean success = false;
@@ -153,7 +153,7 @@ private static CompletionStats filterCompletionStatsByFieldName(String[] fieldNa
153153
} else {
154154
fieldMemoryStats = null;
155155
}
156-
return new CompletionStats(fullCompletionStats.getSizeInBytes(), fieldMemoryStats);
156+
return new CompletionStats.Builder().sizeInBytes(fullCompletionStats.getSizeInBytes()).fields(fieldMemoryStats).build();
157157
}
158158

159159
@Override

server/src/main/java/org/opensearch/search/suggest/completion/CompletionStats.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,26 @@ public class CompletionStats implements Writeable, ToXContentFragment {
6262

6363
public CompletionStats() {}
6464

65+
/**
66+
* Private constructor that takes a builder.
67+
* This is the sole entry point for creating a new CompletionStats object.
68+
* @param builder The builder instance containing all the values.
69+
*/
70+
private CompletionStats(Builder builder) {
71+
this.sizeInBytes = builder.sizeInBytes;
72+
this.fields = builder.fields;
73+
}
74+
6575
public CompletionStats(StreamInput in) throws IOException {
6676
sizeInBytes = in.readVLong();
6777
fields = in.readOptionalWriteable(FieldMemoryStats::new);
6878
}
6979

80+
/**
81+
* This constructor will be deprecated starting in version 3.4.0.
82+
* Use {@link CompletionStats.Builder} instead.
83+
*/
84+
@Deprecated
7085
public CompletionStats(long size, @Nullable FieldMemoryStats fields) {
7186
this.sizeInBytes = size;
7287
this.fields = fields;
@@ -84,6 +99,35 @@ public FieldMemoryStats getFields() {
8499
return fields;
85100
}
86101

102+
/**
103+
* Builder for the {@link CompletionStats} class.
104+
* Provides a fluent API for constructing a CompletionStats object.
105+
*/
106+
public static class Builder {
107+
private long sizeInBytes = 0;
108+
private FieldMemoryStats fields = null;
109+
110+
public Builder() {}
111+
112+
public Builder sizeInBytes(long bytes) {
113+
this.sizeInBytes = bytes;
114+
return this;
115+
}
116+
117+
public Builder fields(FieldMemoryStats fields) {
118+
this.fields = fields;
119+
return this;
120+
}
121+
122+
/**
123+
* Creates a {@link CompletionStats} object from the builder's current state.
124+
* @return A new CompletionStats instance.
125+
*/
126+
public CompletionStats build() {
127+
return new CompletionStats(this);
128+
}
129+
}
130+
87131
@Override
88132
public void writeTo(StreamOutput out) throws IOException {
89133
out.writeVLong(sizeInBytes);

server/src/test/java/org/opensearch/index/suggest/stats/CompletionsStatsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class CompletionsStatsTests extends OpenSearchTestCase {
4444

4545
public void testSerialize() throws IOException {
4646
FieldMemoryStats map = randomBoolean() ? null : FieldMemoryStatsTests.randomFieldMemoryStats();
47-
CompletionStats stats = new CompletionStats(randomNonNegativeLong(), map);
47+
CompletionStats stats = new CompletionStats.Builder().sizeInBytes(randomNonNegativeLong()).fields(map).build();
4848
BytesStreamOutput out = new BytesStreamOutput();
4949
stats.writeTo(out);
5050
StreamInput input = out.bytes().streamInput();

0 commit comments

Comments
 (0)