001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.metrics.impl;
020
021import java.util.Collection;
022import java.util.Set;
023import java.util.concurrent.ConcurrentHashMap;
024import java.util.function.Supplier;
025import java.util.stream.Collectors;
026
027import org.apache.yetus.audience.InterfaceAudience;
028
029/**
030 * A map of K to V, but does ref counting for added and removed values. The values are
031 * not added directly, but instead requested from the given Supplier if ref count == 0. Each put()
032 * call will increment the ref count, and each remove() will decrement it. The values are removed
033 * from the map iff ref count == 0.
034 */
035@InterfaceAudience.Private
036class RefCountingMap<K, V> {
037
038  private ConcurrentHashMap<K, Payload<V>> map = new ConcurrentHashMap<>();
039  private static class Payload<V> {
040    V v;
041    int refCount;
042    Payload(V v) {
043      this.v = v;
044      this.refCount = 1; // create with ref count = 1
045    }
046  }
047
048  V put(K k, Supplier<V> supplier) {
049    return ((Payload<V>)map.compute(k, (k1, oldValue) -> {
050      if (oldValue != null) {
051        oldValue.refCount++;
052        return oldValue;
053      } else {
054        return new Payload(supplier.get());
055      }
056    })).v;
057  }
058
059  V get(K k) {
060    Payload<V> p = map.get(k);
061    return p == null ? null : p.v;
062  }
063
064  /**
065   * Decrements the ref count of k, and removes from map if ref count == 0.
066   * @param k the key to remove
067   * @return the value associated with the specified key or null if key is removed from map.
068   */
069  V remove(K k) {
070    Payload<V> p = map.computeIfPresent(k, (k1, v) -> --v.refCount <= 0 ? null : v);
071    return p == null ? null : p.v;
072  }
073
074  void clear() {
075    map.clear();
076  }
077
078  Set<K> keySet() {
079    return map.keySet();
080  }
081
082  Collection<V> values() {
083    return map.values().stream().map(v -> v.v).collect(Collectors.toList());
084  }
085
086  int size() {
087    return map.size();
088  }
089}