Skip to content

Commit a3c533b

Browse files
committed
Add Builder in RequestCacheStats and refactor usage
Signed-off-by: Jean Kim <bgshhd95@gmail.com>
1 parent 660e0aa commit a3c533b

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

server/src/main/java/org/opensearch/index/cache/request/RequestCacheStats.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,30 @@ public class RequestCacheStats implements Writeable, ToXContentFragment {
5757

5858
public RequestCacheStats() {}
5959

60+
/**
61+
* Private constructor that takes a builder.
62+
* This is the sole entry point for creating a new RequestCacheStats object.
63+
* @param builder The builder instance containing all the values.
64+
*/
65+
private RequestCacheStats(Builder builder) {
66+
this.memorySize = builder.memorySize;
67+
this.evictions = builder.evictions;
68+
this.hitCount = builder.hitCount;
69+
this.missCount = builder.missCount;
70+
}
71+
6072
public RequestCacheStats(StreamInput in) throws IOException {
6173
memorySize = in.readVLong();
6274
evictions = in.readVLong();
6375
hitCount = in.readVLong();
6476
missCount = in.readVLong();
6577
}
6678

79+
/**
80+
* This constructor will be deprecated starting in version 3.4.0.
81+
* Use {@link Builder} instead.
82+
*/
83+
@Deprecated
6784
public RequestCacheStats(long memorySize, long evictions, long hitCount, long missCount) {
6885
this.memorySize = memorySize;
6986
this.evictions = evictions;
@@ -98,6 +115,48 @@ public long getMissCount() {
98115
return this.missCount;
99116
}
100117

118+
/**
119+
* Builder for the {@link RequestCacheStats} class.
120+
* Provides a fluent API for constructing a RequestCacheStats object.
121+
*/
122+
public static class Builder {
123+
private long memorySize = 0;
124+
private long evictions = 0;
125+
private long hitCount = 0;
126+
private long missCount = 0;
127+
128+
public Builder() {}
129+
130+
public Builder memorySize(long count) {
131+
this.memorySize = count;
132+
return this;
133+
}
134+
135+
public Builder evictions(long count) {
136+
this.evictions = count;
137+
return this;
138+
}
139+
140+
public Builder hitCount(long count) {
141+
this.hitCount = count;
142+
return this;
143+
}
144+
145+
public Builder missCount(long count) {
146+
this.missCount = count;
147+
return this;
148+
}
149+
150+
/**
151+
* Creates a {@link RequestCacheStats} object from the builder's current state.
152+
* @return A new RequestCacheStats instance.
153+
*/
154+
public RequestCacheStats build() {
155+
return new RequestCacheStats(this);
156+
}
157+
158+
}
159+
101160
@Override
102161
public void writeTo(StreamOutput out) throws IOException {
103162
out.writeVLong(memorySize);

server/src/main/java/org/opensearch/index/cache/request/ShardRequestCache.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public final class ShardRequestCache {
5454
final CounterMetric missCount = new CounterMetric();
5555

5656
public RequestCacheStats stats() {
57-
return new RequestCacheStats(Math.max(0, totalMetric.count()), evictionsMetric.count(), hitCount.count(), missCount.count());
57+
return new RequestCacheStats.Builder().memorySize(Math.max(0, totalMetric.count()))
58+
.evictions(evictionsMetric.count())
59+
.hitCount(hitCount.count())
60+
.missCount(missCount.count())
61+
.build();
5862
}
5963

6064
public void onHit() {

0 commit comments

Comments
 (0)