This is an automated email from the ASF dual-hosted git repository.

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 4f5612fd8 RATIS-2023. Remove duplicate RefCountingMap (#1039)
4f5612fd8 is described below

commit 4f5612fd89f4ff63a8f529d1517edc663cf84ec2
Author: Doroszlai, Attila <[email protected]>
AuthorDate: Thu Feb 1 19:11:13 2024 +0100

    RATIS-2023. Remove duplicate RefCountingMap (#1039)
---
 .../org/apache/ratis/util}/RefCountingMap.java     |  20 +--
 .../org/apache/ratis/util}/TestRefCountingMap.java |   3 +-
 .../ratis/metrics/impl/MetricRegistriesImpl.java   |   1 +
 .../apache/ratis/metrics/impl/RefCountingMap.java  |  94 -------------
 .../dropwizard3/Dm3MetricRegistriesImpl.java       |   1 +
 .../metrics/dropwizard3/TestRefCountingMap.java    | 147 ---------------------
 6 files changed, 14 insertions(+), 252 deletions(-)

diff --git 
a/ratis-metrics-dropwizard3/src/main/java/org/apache/ratis/metrics/dropwizard3/RefCountingMap.java
 b/ratis-common/src/main/java/org/apache/ratis/util/RefCountingMap.java
similarity index 88%
rename from 
ratis-metrics-dropwizard3/src/main/java/org/apache/ratis/metrics/dropwizard3/RefCountingMap.java
rename to ratis-common/src/main/java/org/apache/ratis/util/RefCountingMap.java
index 6c3ad6f8c..a6a9eb81a 100644
--- 
a/ratis-metrics-dropwizard3/src/main/java/org/apache/ratis/metrics/dropwizard3/RefCountingMap.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/RefCountingMap.java
@@ -15,7 +15,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.metrics.dropwizard3;
+package org.apache.ratis.util;
 
 import java.util.Collection;
 import java.util.Set;
@@ -31,7 +31,7 @@ import java.util.stream.Collectors;
  * 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();
@@ -55,15 +55,15 @@ class RefCountingMap<K, V> {
 
   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));
   }
 
@@ -72,23 +72,23 @@ class RefCountingMap<K, V> {
    * @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();
   }
 }
diff --git 
a/ratis-metrics-default/src/test/java/org/apache/ratis/metrics/impl/TestRefCountingMap.java
 b/ratis-common/src/test/java/org/apache/ratis/util/TestRefCountingMap.java
similarity index 98%
rename from 
ratis-metrics-default/src/test/java/org/apache/ratis/metrics/impl/TestRefCountingMap.java
rename to 
ratis-common/src/test/java/org/apache/ratis/util/TestRefCountingMap.java
index be6eb3037..db6e9239a 100644
--- 
a/ratis-metrics-default/src/test/java/org/apache/ratis/metrics/impl/TestRefCountingMap.java
+++ b/ratis-common/src/test/java/org/apache/ratis/util/TestRefCountingMap.java
@@ -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;
@@ -26,6 +26,7 @@ import java.util.Collection;
 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;
 
diff --git 
a/ratis-metrics-default/src/main/java/org/apache/ratis/metrics/impl/MetricRegistriesImpl.java
 
b/ratis-metrics-default/src/main/java/org/apache/ratis/metrics/impl/MetricRegistriesImpl.java
index 17968ae9f..088508fab 100644
--- 
a/ratis-metrics-default/src/main/java/org/apache/ratis/metrics/impl/MetricRegistriesImpl.java
+++ 
b/ratis-metrics-default/src/main/java/org/apache/ratis/metrics/impl/MetricRegistriesImpl.java
@@ -28,6 +28,7 @@ import java.util.function.Consumer;
 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;
diff --git 
a/ratis-metrics-default/src/main/java/org/apache/ratis/metrics/impl/RefCountingMap.java
 
b/ratis-metrics-default/src/main/java/org/apache/ratis/metrics/impl/RefCountingMap.java
deleted file mode 100644
index 49759781f..000000000
--- 
a/ratis-metrics-default/src/main/java/org/apache/ratis/metrics/impl/RefCountingMap.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.ratis.metrics.impl;
-
-import java.util.Collection;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.function.Supplier;
-import java.util.stream.Collectors;
-
-/**
- * A map of K to V, but does ref counting for added and removed values. The 
values are
- * not added directly, but instead requested from the given Supplier if ref 
count == 0. Each put()
- * 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> {
-  private static class Payload<V> {
-    private final V value;
-    private final AtomicInteger refCount = new AtomicInteger();
-
-    Payload(V v) {
-      this.value = v;
-    }
-
-    V get() {
-      return value;
-    }
-
-    V increment() {
-      return refCount.incrementAndGet() > 0? value: null;
-    }
-
-    Payload<V> decrement() {
-      return refCount.decrementAndGet() > 0? this: null;
-    }
-  }
-
-  private final ConcurrentMap<K, Payload<V>> map = new ConcurrentHashMap<>();
-
-  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) {
-    return p == null ? null : p.get();
-  }
-
-  V get(K k) {
-    return get(map.get(k));
-  }
-
-  /**
-   * Decrements the ref count of k, and removes from map if ref count == 0.
-   * @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) {
-    return get(map.computeIfPresent(k, (k1, v) -> v.decrement()));
-  }
-
-  void clear() {
-    map.clear();
-  }
-
-  Set<K> keySet() {
-    return map.keySet();
-  }
-
-  Collection<V> values() {
-    return 
map.values().stream().map(Payload::get).collect(Collectors.toList());
-  }
-
-  int size() {
-    return map.size();
-  }
-}
diff --git 
a/ratis-metrics-dropwizard3/src/main/java/org/apache/ratis/metrics/dropwizard3/Dm3MetricRegistriesImpl.java
 
b/ratis-metrics-dropwizard3/src/main/java/org/apache/ratis/metrics/dropwizard3/Dm3MetricRegistriesImpl.java
index b26f2e27a..a90c5a0ce 100644
--- 
a/ratis-metrics-dropwizard3/src/main/java/org/apache/ratis/metrics/dropwizard3/Dm3MetricRegistriesImpl.java
+++ 
b/ratis-metrics-dropwizard3/src/main/java/org/apache/ratis/metrics/dropwizard3/Dm3MetricRegistriesImpl.java
@@ -28,6 +28,7 @@ import java.util.function.Consumer;
 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;
diff --git 
a/ratis-metrics-dropwizard3/src/test/java/org/apache/ratis/metrics/dropwizard3/TestRefCountingMap.java
 
b/ratis-metrics-dropwizard3/src/test/java/org/apache/ratis/metrics/dropwizard3/TestRefCountingMap.java
deleted file mode 100644
index 87b8bf012..000000000
--- 
a/ratis-metrics-dropwizard3/src/test/java/org/apache/ratis/metrics/dropwizard3/TestRefCountingMap.java
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.ratis.metrics.dropwizard3;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-import java.util.Collection;
-import java.util.Set;
-
-import org.apache.ratis.thirdparty.com.google.common.collect.Lists;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-public class TestRefCountingMap {
-
-  private RefCountingMap<String, String> map;
-
-  @BeforeEach
-  public void setUp() {
-    map = new RefCountingMap<>();
-  }
-
-  @Test
-  public void testPutGet() {
-    map.put("foo", () -> "foovalue");
-
-    String v = map.get("foo");
-    assertNotNull(v);
-    assertEquals("foovalue", v);
-  }
-
-  @Test
-  public void testPutMulti() {
-    String v1 = map.put("foo", () -> "foovalue");
-    String v2 =  map.put("foo", () -> "foovalue2");
-    String v3 = map.put("foo", () -> "foovalue3");
-
-    String v = map.get("foo");
-    assertEquals("foovalue", v);
-    assertEquals(v, v1);
-    assertEquals(v, v2);
-    assertEquals(v, v3);
-  }
-
-  @Test
-  public void testPutRemove() {
-    map.put("foo", () -> "foovalue");
-    String v = map.remove("foo");
-    assertNull(v);
-    v = map.get("foo");
-    assertNull(v);
-  }
-
-  @Test
-  public void testPutRemoveMulti() {
-    map.put("foo", () -> "foovalue");
-    map.put("foo", () -> "foovalue2");
-    map.put("foo", () -> "foovalue3");
-
-    // remove 1
-    String v = map.remove("foo");
-    assertEquals("foovalue", v);
-
-    // remove 2
-    v = map.remove("foo");
-    assertEquals("foovalue", v);
-
-    // remove 3
-    v = map.remove("foo");
-    assertNull(v);
-    v = map.get("foo");
-    assertNull(v);
-  }
-
-  @Test
-  public void testSize() {
-    assertEquals(0, map.size());
-
-    // put a key
-    map.put("foo", () -> "foovalue");
-    assertEquals(1, map.size());
-
-    // put a different key
-    map.put("bar", () -> "foovalue2");
-    assertEquals(2, map.size());
-
-    // put the same key again
-    map.put("bar", () -> "foovalue3");
-    assertEquals(2, map.size()); // map should be same size
-  }
-
-  @Test
-  public void testClear() {
-    map.put("foo", () -> "foovalue");
-    map.put("bar", () -> "foovalue2");
-    map.put("baz", () -> "foovalue3");
-
-    map.clear();
-
-    assertEquals(0, map.size());
-  }
-
-
-  @Test
-  public void testKeySet() {
-    map.put("foo", () -> "foovalue");
-    map.put("bar", () -> "foovalue2");
-    map.put("baz", () -> "foovalue3");
-
-    Set<String> keys = map.keySet();
-    assertEquals(3, keys.size());
-
-    Lists.newArrayList("foo", "bar", "baz").forEach(v -> 
assertTrue(keys.contains(v)));
-  }
-
-  @Test
-  public void testValues() {
-    map.put("foo", () -> "foovalue");
-    map.put("foo", () -> "foovalue2");
-    map.put("bar", () -> "foovalue3");
-    map.put("baz", () -> "foovalue4");
-
-    Collection<String> values = map.values();
-    assertEquals(3, values.size());
-
-    Lists.newArrayList("foovalue", "foovalue3", "foovalue4")
-            .forEach(v -> assertTrue(values.contains(v)));
-  }
-}

Reply via email to