diff --git a/CHANGELOG.md b/CHANGELOG.md index 702be3d6f45b0..f798587c85234 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) @@ -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 diff --git a/server/src/main/java/org/opensearch/common/cache/Cache.java b/server/src/main/java/org/opensearch/common/cache/Cache.java index 10628e25dba5b..edb58c230a6a4 100644 --- a/server/src/main/java/org/opensearch/common/cache/Cache.java +++ b/server/src/main/java/org/opensearch/common/cache/Cache.java @@ -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(); } /** @@ -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; @@ -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); + } + } } /**