聊聊Elasticsearch的ConcurrentMapLong

41次阅读

共计 4054 个字符,预计需要花费 11 分钟才能阅读完成。

本文主要研究一下 Elasticsearch 的 ConcurrentMapLong

ConcurrentMapLong

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/ConcurrentMapLong.java

public interface ConcurrentMapLong<T> extends ConcurrentMap<Long, T> {T get(long key);

    T remove(long key);

    T put(long key, T value);

    T putIfAbsent(long key, T value);
}
  • ConcurrentMapLong 继承了 ConcurrentMap 接口,并指定 key 类型为 Long

ConcurrentHashMapLong

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/ConcurrentHashMapLong.java

public class ConcurrentHashMapLong<T> implements ConcurrentMapLong<T> {

    private final ConcurrentMap<Long, T> map;

    public ConcurrentHashMapLong(ConcurrentMap<Long, T> map) {this.map = map;}

    @Override
    public T get(long key) {return map.get(key);
    }

    @Override
    public T remove(long key) {return map.remove(key);
    }

    @Override
    public T put(long key, T value) {return map.put(key, value);
    }

    @Override
    public T putIfAbsent(long key, T value) {return map.putIfAbsent(key, value);
    }

    // MAP DELEGATION

    @Override
    public boolean isEmpty() {return map.isEmpty();
    }

    @Override
    public int size() {return map.size();
    }

    @Override
    public T get(Object key) {return map.get(key);
    }

    @Override
    public boolean containsKey(Object key) {return map.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {return map.containsValue(value);
    }

    @Override
    public T put(Long key, T value) {return map.put(key, value);
    }

    @Override
    public T putIfAbsent(Long key, T value) {return map.putIfAbsent(key, value);
    }

    @Override
    public void putAll(Map<? extends Long, ? extends T> m) {map.putAll(m);
    }

    @Override
    public T remove(Object key) {return map.remove(key);
    }

    @Override
    public boolean remove(Object key, Object value) {return map.remove(key, value);
    }

    @Override
    public boolean replace(Long key, T oldValue, T newValue) {return map.replace(key, oldValue, newValue);
    }

    @Override
    public T replace(Long key, T value) {return map.replace(key, value);
    }

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

    @Override
    public Set<Long> keySet() {return map.keySet();
    }

    @Override
    public Collection<T> values() {return map.values();
    }

    @Override
    public Set<Entry<Long, T>> entrySet() {return map.entrySet();
    }

    @Override
    public boolean equals(Object o) {return map.equals(o);
    }

    @Override
    public int hashCode() {return map.hashCode();
    }

    @Override
    public String toString() {return map.toString();
    }
}
  • ConcurrentHashMapLong 实现了 ConcurrentMapLong 接口,它内部使用 ConcurrentMap 实现

ConcurrentCollections

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/ConcurrentCollections.java

public abstract class ConcurrentCollections {

    static final int aggressiveConcurrencyLevel;

    static {aggressiveConcurrencyLevel = Math.max(Runtime.getRuntime().availableProcessors() * 2, 16);
    }

    //......

    public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {return new ConcurrentHashMap<>();
    }

    public static <V> ConcurrentMapLong<V> newConcurrentMapLong() {return new ConcurrentHashMapLong<>(ConcurrentCollections.<Long, V>newConcurrentMap());
    }

    public static <V> ConcurrentMapLong<V> newConcurrentMapLongWithAggressiveConcurrency() {return new ConcurrentHashMapLong<>(ConcurrentCollections.<Long, V>newConcurrentMapWithAggressiveConcurrency());
    }

    public static <K, V> ConcurrentMap<K, V> newConcurrentMapWithAggressiveConcurrency() {return newConcurrentMapWithAggressiveConcurrency(16);
    }

    public static <K, V> ConcurrentMap<K, V> newConcurrentMapWithAggressiveConcurrency(int initalCapacity) {return new ConcurrentHashMap<>(initalCapacity, 0.75f, aggressiveConcurrencyLevel);
    }    
    //......
}
  • ConcurrentCollections 提供了 newConcurrentMapLong 及 newConcurrentMapLongWithAggressiveConcurrency 两个静态方法用于创建 ConcurrentMapLong;其中 newConcurrentMapLongWithAggressiveConcurrency 方法创建 initalCapacity 为 16,loadFactor 为 0.75f,concurrencyLevel 为 aggressiveConcurrencyLevel(Math.max(Runtime.getRuntime().availableProcessors() * 2, 16)) 的 ConcurrentHashMap

小结

  • ConcurrentMapLong 继承了 ConcurrentMap 接口,并指定 key 类型为 Long
  • ConcurrentHashMapLong 实现了 ConcurrentMapLong 接口,它内部使用 ConcurrentMap 实现
  • ConcurrentCollections 提供了 newConcurrentMapLong 及 newConcurrentMapLongWithAggressiveConcurrency 两个静态方法用于创建 ConcurrentMapLong;其中 newConcurrentMapLongWithAggressiveConcurrency 方法创建 initalCapacity 为 16,loadFactor 为 0.75f,concurrencyLevel 为 aggressiveConcurrencyLevel(Math.max(Runtime.getRuntime().availableProcessors() * 2, 16)) 的 ConcurrentHashMap

doc

  • ConcurrentMapLong
  • ConcurrentHashMapLong
  • ConcurrentCollections

正文完
 0