Skip to content

Commit

Permalink
RATIS-2023. Remove duplicate RefCountingMap (apache#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroszlai authored and szetszwo committed Jun 16, 2024
1 parent dfa30fe commit f728173
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ratis.metrics.impl;
package org.apache.ratis.util;

import java.util.Collection;
import java.util.Set;
Expand All @@ -31,7 +31,7 @@
* call will increment the ref count, and each remove() will decrement it. The values are removed
* from the map iff ref count == 0.
*/
class RefCountingMap<K, V> {
public final class RefCountingMap<K, V> {
private static class Payload<V> {
private final V value;
private final AtomicInteger refCount = new AtomicInteger();
Expand All @@ -55,15 +55,15 @@ Payload<V> decrement() {

private final ConcurrentMap<K, Payload<V>> map = new ConcurrentHashMap<>();

V put(K k, Supplier<V> supplier) {
public V put(K k, Supplier<V> supplier) {
return map.compute(k, (k1, old) -> old != null? old: new Payload<>(supplier.get())).increment();
}

static <V> V get(Payload<V> p) {
public static <V> V get(Payload<V> p) {
return p == null ? null : p.get();
}

V get(K k) {
public V get(K k) {
return get(map.get(k));
}

Expand All @@ -72,23 +72,23 @@ V get(K k) {
* @param k the key to remove
* @return the value associated with the specified key or null if key is removed from map.
*/
V remove(K k) {
public V remove(K k) {
return get(map.computeIfPresent(k, (k1, v) -> v.decrement()));
}

void clear() {
public void clear() {
map.clear();
}

Set<K> keySet() {
public Set<K> keySet() {
return map.keySet();
}

Collection<V> values() {
public Collection<V> values() {
return map.values().stream().map(Payload::get).collect(Collectors.toList());
}

int size() {
public int size() {
return map.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ratis.metrics.impl;
package org.apache.ratis.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -26,6 +26,7 @@
import java.util.Set;

import org.apache.ratis.thirdparty.com.google.common.collect.Lists;
import org.apache.ratis.util.RefCountingMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.ratis.metrics.MetricRegistries;
import org.apache.ratis.metrics.MetricRegistryInfo;
import org.apache.ratis.metrics.RatisMetricRegistry;
import org.apache.ratis.util.RefCountingMap;
import org.apache.ratis.util.TimeDuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.ratis.metrics.MetricRegistries;
import org.apache.ratis.metrics.MetricRegistryInfo;
import org.apache.ratis.metrics.RatisMetricRegistry;
import org.apache.ratis.util.RefCountingMap;
import org.apache.ratis.util.TimeDuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit f728173

Please sign in to comment.