Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Update NoOpResult constructors in the Engine to be public ([#19950](https://github.com/opensearch-project/OpenSearch/pull/19950))
- Refactor the TranslogStats and RequestCacheStats class to use the Builder pattern instead of constructors ([#19961](https://github.com/opensearch-project/OpenSearch/pull/19961))
- Refactor the IndexPressutreStats, DeviceStats and TransportStats class to use the Builder pattern instead of constructors ([#19991](https://github.com/opensearch-project/OpenSearch/pull/19991))
- Refactor the Cache.CacheStats class to use the Builder pattern instead of constructors ([#20015](https://github.com/opensearch-project/OpenSearch/pull/20015))

### Fixed
- Fix Allocation and Rebalance Constraints of WeightFunction are incorrectly reset ([#19012](https://github.com/opensearch-project/OpenSearch/pull/19012))
Expand Down Expand Up @@ -122,6 +123,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Deprecated existing constructors in FieldDataStats and CompletionStats in favor of the new Builder ([#19936](https://github.com/opensearch-project/OpenSearch/pull/19936))
- Deprecated existing constructors in TranslogStats and RequestCacheStats in favor of the new Builder ([#19961](https://github.com/opensearch-project/OpenSearch/pull/19961))
- Deprecated existing constructors in IndexPressutreStats, DeviceStats and TransportStats in favor of the new Builder ([#19991](https://github.com/opensearch-project/OpenSearch/pull/19991))
- Deprecated existing constructors in Cache.CacheStats in favor of the new Builder ([#20015](https://github.com/opensearch-project/OpenSearch/pull/20015))

### Removed

Expand Down
53 changes: 52 additions & 1 deletion server/src/main/java/org/opensearch/common/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ public CacheStats stats() {
misses += segments[i].segmentStats.misses.longValue();
evictions += segments[i].segmentStats.evictions.longValue();
}
return new CacheStats(hits, misses, evictions);
return new CacheStats.Builder().hits(hits).misses(misses).evictions(evictions).build();
}

/**
Expand All @@ -804,6 +804,22 @@ public static class CacheStats {
private long misses;
private long evictions;

/**
* Private constructor that takes a builder.
* This is the sole entry point for creating a new CacheStats object.
* @param builder The builder instance containing all the values.
*/
private CacheStats(Builder builder) {
this.hits = builder.hits;
this.misses = builder.misses;
this.evictions = builder.evictions;
}

/**
* This constructor will be deprecated starting in version 3.4.0.
* Use {@link CacheStats} instead.
*/
@Deprecated
public CacheStats(long hits, long misses, long evictions) {
this.hits = hits;
this.misses = misses;
Expand All @@ -821,6 +837,41 @@ public long getMisses() {
public long getEvictions() {
return evictions;
}

/**
* Builder for the {@link CacheStats} class.
* Provides a fluent API for constructing a CacheStats object.
*/
public static class Builder {
private long hits = 0;
private long misses = 0;
private long evictions = 0;

public Builder() {}

public Builder hits(long hits) {
this.hits = hits;
return this;
}

public Builder misses(long misses) {
this.misses = misses;
return this;
}

public Builder evictions(long evictions) {
this.evictions = evictions;
return this;
}

/**
* Creates a {@link CacheStats} object from the builder's current state.
* @return A new CacheStats instance.
*/
public CacheStats build() {
return new CacheStats(this);
}
}
}

/**
Expand Down
Loading