Class AtomicHashMap<K,V>
- java.lang.Object
-
- io.arxila.atomichash.AtomicHashMap<K,V>
-
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
java.io.Serializable,java.util.Map<K,V>
public final class AtomicHashMap<K,V> extends java.lang.Object implements java.util.Map<K,V>, java.io.SerializableA thread-safe implementation of theMapinterface providing advanced concurrency features.This implementation is thread-safe, atomic and non-blocking for all of its methods, including both reads and writes. This includes multi-element methods such as
putAll(Map)andgetAll(Object...)(the latter not a part of theMapinterface), as well as all other methods for retrieving, adding, modifying or removing mappings, iteration, etc.The map can therefore never be read in a partially modified state, and its exact snapshot state for an arbitrary number of mappings can be obtained at any time.
This is achieved by internally implementing an immutable variation of a CTRIE (Concurrent Hash-Trie). This structure is composed of a tree of compact (bitmap-managed) arrays that map keys to positions in each of the tree levels depending on the value of a range of bits of its (modified) hash code.
Key hash codes (32-bit ints) are divided into five 6-bit segments plus one final 2-bit segment. Each of these segments is used, at each level of depth, to compute the position assigned to the key in the compact array (node) living at that level of depth in the ctrie structure. These are compact arrays with a maximum of 64 positions (bitmaps are long values), each of which can contain either a data entry or a link to another node at level + 1. A maximum of 6 levels can exist (0 to 5), and hash collisions only need to be managed at the deepest level. All structures are kept immutable, so modifications in an array (node) at a specific level mean the creation of new nodes from that point up to the root of the tree, and the replacement of the old root with the new one using an atomic compare-and-swap operation.
Note that, given this implementation is based on immutable tree structures, modifications typically need a higher use of memory than other common implementations of the
Mapinterface.New instances of this class can be created by either calling its constructor
AtomicHashMap()or by calling any of its static convenience factory AtomicHashMap.of(...) methods: of(), of(k1, v1), of(k1, v1, k2, v2), of(k1, v1, k2, v2, k3, v3), etc.Note that this implementation does not keep the insertion order. Iteration order is not guaranteed to be consistent.
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicReference<Root>rootprivate static longserialVersionUID
-
Constructor Summary
Constructors Modifier Constructor Description AtomicHashMap()privateAtomicHashMap(Root root)AtomicHashMap(java.util.Map<? extends K,? extends V> map)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Vcompute(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)VcomputeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction)VcomputeIfPresent(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)private static Entryentry(int hash, java.lang.Object key, java.lang.Object value)private static Entryentry(java.lang.Object key, java.lang.Object value)java.util.Set<java.util.Map.Entry<K,V>>entrySet()booleanequals(java.lang.Object other)voidforEach(java.util.function.BiConsumer<? super K,? super V> action)Vget(java.lang.Object key)java.util.Map<K,V>getAll(java.lang.Object... keys)VgetOrDefault(java.lang.Object key, V defaultValue)inthashCode()(package private) RootinnerRoot()booleanisEmpty()java.util.Set<K>keySet()Vmerge(K key, V newValue, java.util.function.BiFunction<? super V,? super V,? extends V> remappingFunction)static <K,V>
AtomicHashMap<K,V>of()static <K,V>
AtomicHashMap<K,V>of(K k1, V v1)static <K,V>
AtomicHashMap<K,V>of(K k1, V v1, K k2, V v2)static <K,V>
AtomicHashMap<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3)static <K,V>
AtomicHashMap<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)static <K,V>
AtomicHashMap<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)static <K,V>
AtomicHashMap<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)static <K,V>
AtomicHashMap<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)static <K,V>
AtomicHashMap<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)static <K,V>
AtomicHashMap<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)static <K,V>
AtomicHashMap<K,V>of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)Vput(K key, V newValue)voidputAll(java.util.Map<? extends K,? extends V> newMappings)VputIfAbsent(K key, V newValue)Vremove(java.lang.Object key)booleanremove(java.lang.Object key, java.lang.Object oldValue)Vreplace(K key, V newValue)booleanreplace(K key, V oldValue, V newValue)voidreplaceAll(java.util.function.BiFunction<? super K,? super V,? extends V> function)intsize()AtomicHashStore<K,V>store()java.lang.StringtoString()java.util.Collection<V>values()
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
root
private final java.util.concurrent.atomic.AtomicReference<Root> root
-
-
Method Detail
-
entry
private static Entry entry(java.lang.Object key, java.lang.Object value)
-
entry
private static Entry entry(int hash, java.lang.Object key, java.lang.Object value)
-
of
public static <K,V> AtomicHashMap<K,V> of()
-
of
public static <K,V> AtomicHashMap<K,V> of(K k1, V v1)
-
of
public static <K,V> AtomicHashMap<K,V> of(K k1, V v1, K k2, V v2)
-
of
public static <K,V> AtomicHashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
-
of
public static <K,V> AtomicHashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
-
of
public static <K,V> AtomicHashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
-
of
public static <K,V> AtomicHashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
-
of
public static <K,V> AtomicHashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
-
of
public static <K,V> AtomicHashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
-
of
public static <K,V> AtomicHashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
-
of
public static <K,V> AtomicHashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
-
innerRoot
Root innerRoot()
-
store
public AtomicHashStore<K,V> store()
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
remove
public boolean remove(java.lang.Object key, java.lang.Object oldValue)
-
values
public java.util.Collection<V> values()
-
replaceAll
public void replaceAll(java.util.function.BiFunction<? super K,? super V,? extends V> function)
-
compute
public V compute(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
-
computeIfAbsent
public V computeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction)
-
computeIfPresent
public V computeIfPresent(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
-
merge
public V merge(K key, V newValue, java.util.function.BiFunction<? super V,? super V,? extends V> remappingFunction)
-
equals
public boolean equals(java.lang.Object other)
-
hashCode
public int hashCode()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-