Class ConcurrentSkipListIntObjMultimap<V>
- java.lang.Object
-
- io.netty.util.concurrent.ConcurrentSkipListIntObjMultimap<V>
-
- Type Parameters:
V- the type of mapped values
- All Implemented Interfaces:
java.lang.Iterable<ConcurrentSkipListIntObjMultimap.IntEntry<V>>
public class ConcurrentSkipListIntObjMultimap<V> extends java.lang.Object implements java.lang.Iterable<ConcurrentSkipListIntObjMultimap.IntEntry<V>>
A scalable concurrent multimap implementation. The map is sorted according to the natural ordering of itsintkeys.This class implements a concurrent variant of SkipLists providing expected average log(n) time cost for the
containsKey,get,putandremoveoperations and their variants. Insertion, removal, update, and access operations safely execute concurrently by multiple threads.This class is a multimap, which means the same key can be associated with multiple values. Each such instance will be represented by a separate
IntEntry. There is no defined ordering for the values mapped to the same key.As a multimap, certain atomic operations like
putIfPresent,compute, orcomputeIfPresent, cannot be supported. Likewise, some get-like operations cannot be supported.Iterators and spliterators are weakly consistent.
All
IntEntrypairs returned by methods in this class represent snapshots of mappings at the time they were produced. They do not support theEntry.setValuemethod. (Note however that it is possible to change mappings in the associated map usingput,putIfAbsent, orreplace, depending on exactly which effect you need.)Beware that bulk operations
putAll,equals,toArray,containsValue, andclearare not guaranteed to be performed atomically. For example, an iterator operating concurrently with aputAlloperation might view only some of the added elements.This class does not permit the use of
nullvalues because some null return values cannot be reliably distinguished from the absence of elements.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classConcurrentSkipListIntObjMultimap.EntryIterator(package private) static classConcurrentSkipListIntObjMultimap.Index<V>Index nodes represent the levels of the skip list.static classConcurrentSkipListIntObjMultimap.IntEntry<V>The multimap entry type with primitiveintkeys.(package private) classConcurrentSkipListIntObjMultimap.Iter<T>Base of iterator classes(package private) static classConcurrentSkipListIntObjMultimap.Node<V>Nodes hold keys and values, and are singly linked in sorted order, possibly with some intervening marker nodes.
-
Field Summary
Fields Modifier and Type Field Description private static intacquireFenceVariableprivate LongCounteradderElement countprivate static intEQprivate static intGTprivate ConcurrentSkipListIntObjMultimap.Index<V>headLazily initialized topmost index of the skiplist.private static java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap<?>,ConcurrentSkipListIntObjMultimap.Index<?>>HEADprivate static intLTprivate static java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Node<?>,ConcurrentSkipListIntObjMultimap.Node<?>>NEXTprivate intnoKeyNo-key sentinel valueprivate static java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Index<?>,ConcurrentSkipListIntObjMultimap.Index<?>>RIGHTprivate static java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Node<?>,java.lang.Object>VAL
-
Constructor Summary
Constructors Constructor Description ConcurrentSkipListIntObjMultimap(int noKey)Constructs a new, empty map, sorted according to the natural ordering of the keys.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static voidacquireFence()Orders LOADS before the fence, with LOADS and STORES after the fence.private voidaddCount(long c)Adds to element count, initializing adder if necessary(package private) static <V> booleanaddIndices(ConcurrentSkipListIntObjMultimap.Index<V> q, int skips, ConcurrentSkipListIntObjMultimap.Index<V> x, int noKey)Add indices after an insertion.(package private) ConcurrentSkipListIntObjMultimap.Node<V>baseHead()Returns the header for base node list, or null if uninitializedConcurrentSkipListIntObjMultimap.IntEntry<V>ceilingEntry(int key)Returns a key-value mapping associated with the least key greater than or equal to the given key, ornullif there is no such entry.intceilingKey(int key)voidclear()Removes all of the mappings from this map.private static <T> java.lang.Class<T>cls(java.lang.Class<?> cls)booleancontainsKey(int key)Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(java.lang.Object value)Returnstrueif this map maps one or more keys to the specified value.(package private) static intcpr(int x, int y)Compares using comparator or natural ordering if null.private VdoGet(int key)Gets value for key.private VdoPut(int key, V value, boolean onlyIfAbsent)Main insertion method.(package private) VdoRemove(int key, java.lang.Object value)Main deletion method.private ConcurrentSkipListIntObjMultimap.IntEntry<V>doRemoveFirstEntry()Removes first entry; returns its snapshot.private ConcurrentSkipListIntObjMultimap.IntEntry<V>doRemoveLastEntry()Removes last entry; returns its snapshot.(package private) ConcurrentSkipListIntObjMultimap.Node<V>findFirst()Gets first valid node, unlinking deleted nodes if encountered.(package private) ConcurrentSkipListIntObjMultimap.IntEntry<V>findFirstEntry()Entry snapshot version of findFirst(package private) ConcurrentSkipListIntObjMultimap.Node<V>findLast()Specialized version of find to get last valid node.(package private) ConcurrentSkipListIntObjMultimap.IntEntry<V>findLastEntry()Entry version of findLast(package private) ConcurrentSkipListIntObjMultimap.Node<V>findNear(int key, int rel)Utility for ceiling, floor, lower, higher methods.(package private) ConcurrentSkipListIntObjMultimap.IntEntry<V>findNearEntry(int key, int rel)Variant of findNear returning IntEntryprivate ConcurrentSkipListIntObjMultimap.Node<V>findNode(int key)Returns node holding key or null if no such, clearing out any deleted nodes seen along the way.private ConcurrentSkipListIntObjMultimap.Node<V>findPredecessor(int key)Returns an index node with key strictly less than given key.ConcurrentSkipListIntObjMultimap.IntEntry<V>firstEntry()Returns a key-value mapping associated with the least key in this map, ornullif the map is empty.intfirstKey()ConcurrentSkipListIntObjMultimap.IntEntry<V>floorEntry(int key)Returns a key-value mapping associated with the greatest key less than or equal to the given key, ornullif there is no such key.intfloorKey(int key)Vget(int key)Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.(package private) longgetAdderCount()Returns element count, initializing adder if necessary.VgetOrDefault(int key, V defaultValue)Returns the value to which the specified key is mapped, or the given defaultValue if this map contains no mapping for the key.ConcurrentSkipListIntObjMultimap.IntEntry<V>higherEntry(int key)Returns a key-value mapping associated with the least key strictly greater than the given key, ornullif there is no such key.inthigherKey(int key)booleanisEmpty()Check if the collection is empty.java.util.Iterator<ConcurrentSkipListIntObjMultimap.IntEntry<V>>iterator()ConcurrentSkipListIntObjMultimap.IntEntry<V>lastEntry()Returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty.intlastKey()ConcurrentSkipListIntObjMultimap.IntEntry<V>lowerEntry(int key)Returns a key-value mapping associated with the greatest key strictly less than the given key, ornullif there is no such key.intlowerKey(int key)ConcurrentSkipListIntObjMultimap.IntEntry<V>pollCeilingEntry(int key)ConcurrentSkipListIntObjMultimap.IntEntry<V>pollFirstEntry()Removes and returns a key-value mapping associated with the least key in this map, ornullif the map is empty.ConcurrentSkipListIntObjMultimap.IntEntry<V>pollLastEntry()Removes and returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty.voidput(int key, V value)Associates the specified value with the specified key in this map.Vremove(int key)Removes the mapping for the specified key from this map if present.booleanremove(int key, java.lang.Object value)Remove the specific entry with the given key and value, if it exist.booleanreplace(int key, V oldValue, V newValue)Replace the specific entry with the given key and value, with the given replacement value, if such an entry exist.intsize()Get the approximate size of the collection.private voidtryReduceLevel()Possibly reduce head level if it has no nodes.(package private) static <V> voidunlinkNode(ConcurrentSkipListIntObjMultimap.Node<V> b, ConcurrentSkipListIntObjMultimap.Node<V> n, int noKey)Tries to unlink deleted node n from predecessor b (if both exist), by first splicing in a marker if not already present.
-
-
-
Field Detail
-
noKey
private final int noKey
No-key sentinel value
-
head
private volatile ConcurrentSkipListIntObjMultimap.Index<V> head
Lazily initialized topmost index of the skiplist.
-
adder
private final LongCounter adder
Element count
-
EQ
private static final int EQ
- See Also:
- Constant Field Values
-
LT
private static final int LT
- See Also:
- Constant Field Values
-
GT
private static final int GT
- See Also:
- Constant Field Values
-
HEAD
private static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap<?>,ConcurrentSkipListIntObjMultimap.Index<?>> HEAD
-
NEXT
private static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Node<?>,ConcurrentSkipListIntObjMultimap.Node<?>> NEXT
-
VAL
private static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Node<?>,java.lang.Object> VAL
-
RIGHT
private static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ConcurrentSkipListIntObjMultimap.Index<?>,ConcurrentSkipListIntObjMultimap.Index<?>> RIGHT
-
acquireFenceVariable
private static volatile int acquireFenceVariable
-
-
Method Detail
-
cpr
static int cpr(int x, int y)Compares using comparator or natural ordering if null. Called only by methods that have performed required type checks.
-
baseHead
final ConcurrentSkipListIntObjMultimap.Node<V> baseHead()
Returns the header for base node list, or null if uninitialized
-
unlinkNode
static <V> void unlinkNode(ConcurrentSkipListIntObjMultimap.Node<V> b, ConcurrentSkipListIntObjMultimap.Node<V> n, int noKey)
Tries to unlink deleted node n from predecessor b (if both exist), by first splicing in a marker if not already present. Upon return, node n is sure to be unlinked from b, possibly via the actions of some other thread.- Parameters:
b- if nonnull, predecessorn- if nonnull, node known to be deleted
-
addCount
private void addCount(long c)
Adds to element count, initializing adder if necessary- Parameters:
c- count to add
-
getAdderCount
final long getAdderCount()
Returns element count, initializing adder if necessary.
-
findPredecessor
private ConcurrentSkipListIntObjMultimap.Node<V> findPredecessor(int key)
Returns an index node with key strictly less than given key. Also unlinks indexes to deleted nodes found along the way. Callers rely on this side-effect of clearing indices to deleted nodes.- Parameters:
key- if nonnull the key- Returns:
- a predecessor node of key, or null if uninitialized or null key
-
findNode
private ConcurrentSkipListIntObjMultimap.Node<V> findNode(int key)
Returns node holding key or null if no such, clearing out any deleted nodes seen along the way. Repeatedly traverses at base-level looking for key starting at predecessor returned from findPredecessor, processing base-level deletions as encountered. Restarts occur, at traversal step encountering node n, if n's key field is null, indicating it is a marker, so its predecessor is deleted before continuing, which we help do by re-finding a valid predecessor. The traversal loops in doPut, doRemove, and findNear all include the same checks.- Parameters:
key- the key- Returns:
- node holding key, or null if no such
-
doGet
private V doGet(int key)
Gets value for key. Same idea as findNode, except skips over deletions and markers, and returns first encountered value to avoid possibly inconsistent rereads.- Parameters:
key- the key- Returns:
- the value, or null if absent
-
doPut
private V doPut(int key, V value, boolean onlyIfAbsent)
Main insertion method. Adds element if not present, or replaces value if present and onlyIfAbsent is false.- Parameters:
key- the keyvalue- the value that must be associated with keyonlyIfAbsent- if should not insert if already present
-
addIndices
static <V> boolean addIndices(ConcurrentSkipListIntObjMultimap.Index<V> q, int skips, ConcurrentSkipListIntObjMultimap.Index<V> x, int noKey)
Add indices after an insertion. Descends iteratively to the highest level of insertion, then recursively, to chain index nodes to lower ones. Returns null on (staleness) failure, disabling higher-level insertions. Recursion depths are exponentially less probable.- Parameters:
q- starting index for current levelskips- levels to skip before insertingx- index for this insertion
-
doRemove
final V doRemove(int key, java.lang.Object value)
Main deletion method. Locates node, nulls value, appends a deletion marker, unlinks predecessor, removes associated index nodes, and possibly reduces head index level.- Parameters:
key- the keyvalue- if non-null, the value that must be associated with key- Returns:
- the node, or null if not found
-
tryReduceLevel
private void tryReduceLevel()
Possibly reduce head level if it has no nodes. This method can (rarely) make mistakes, in which case levels can disappear even though they are about to contain index nodes. This impacts performance, not correctness. To minimize mistakes as well as to reduce hysteresis, the level is reduced by one only if the topmost three levels look empty. Also, if the removed level looks non-empty after CAS, we try to change it back quick before anyone notices our mistake! (This trick works pretty well because this method will practically never make mistakes unless current thread stalls immediately before first CAS, in which case it is very unlikely to stall again immediately afterwards, so will recover.)We put up with all this rather than just let levels grow because otherwise, even a small map that has undergone a large number of insertions and removals will have a lot of levels, slowing down access more than would an occasional unwanted reduction.
-
findFirst
final ConcurrentSkipListIntObjMultimap.Node<V> findFirst()
Gets first valid node, unlinking deleted nodes if encountered.- Returns:
- first node or null if empty
-
findFirstEntry
final ConcurrentSkipListIntObjMultimap.IntEntry<V> findFirstEntry()
Entry snapshot version of findFirst
-
doRemoveFirstEntry
private ConcurrentSkipListIntObjMultimap.IntEntry<V> doRemoveFirstEntry()
Removes first entry; returns its snapshot.- Returns:
- null if empty, else snapshot of first entry
-
findLast
final ConcurrentSkipListIntObjMultimap.Node<V> findLast()
Specialized version of find to get last valid node.- Returns:
- last node or null if empty
-
findLastEntry
final ConcurrentSkipListIntObjMultimap.IntEntry<V> findLastEntry()
Entry version of findLast- Returns:
- Entry for last node or null if empty
-
doRemoveLastEntry
private ConcurrentSkipListIntObjMultimap.IntEntry<V> doRemoveLastEntry()
Removes last entry; returns its snapshot. Specialized variant of doRemove.- Returns:
- null if empty, else snapshot of last entry
-
findNearEntry
final ConcurrentSkipListIntObjMultimap.IntEntry<V> findNearEntry(int key, int rel)
Variant of findNear returning IntEntry- Parameters:
key- the keyrel- the relation -- OR'ed combination of EQ, LT, GT- Returns:
- Entry fitting relation, or null if no such
-
findNear
final ConcurrentSkipListIntObjMultimap.Node<V> findNear(int key, int rel)
Utility for ceiling, floor, lower, higher methods.- Parameters:
key- the keyrel- the relation -- OR'ed combination of EQ, LT, GT- Returns:
- nearest node fitting relation, or null if no such
-
containsKey
public boolean containsKey(int key)
Returnstrueif this map contains a mapping for the specified key.- Parameters:
key- key whose presence in this map is to be tested- Returns:
trueif this map contains a mapping for the specified key- Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null
-
get
public V get(int key)
Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.More formally, if this map contains a mapping from a key
kto a valuevsuch thatkeycompares equal tokaccording to the map's ordering, then this method returnsv; otherwise it returnsnull. (There can be at most one such mapping.)- Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null
-
getOrDefault
public V getOrDefault(int key, V defaultValue)
Returns the value to which the specified key is mapped, or the given defaultValue if this map contains no mapping for the key.- Parameters:
key- the keydefaultValue- the value to return if this map contains no mapping for the given key- Returns:
- the mapping for the key, if present; else the defaultValue
- Throws:
java.lang.NullPointerException- if the specified key is null- Since:
- 1.8
-
put
public void put(int key, V value)Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.- Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key- Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key or value is null
-
remove
public V remove(int key)
Removes the mapping for the specified key from this map if present.- Parameters:
key- key for which mapping should be removed- Returns:
- the previous value associated with the specified key, or
nullif there was no mapping for the key - Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null
-
containsValue
public boolean containsValue(java.lang.Object value)
Returnstrueif this map maps one or more keys to the specified value. This operation requires time linear in the map size. Additionally, it is possible for the map to change during execution of this method, in which case the returned result may be inaccurate.- Parameters:
value- value whose presence in this map is to be tested- Returns:
trueif a mapping tovalueexists;falseotherwise- Throws:
java.lang.NullPointerException- if the specified value is null
-
size
public int size()
Get the approximate size of the collection.
-
isEmpty
public boolean isEmpty()
Check if the collection is empty.
-
clear
public void clear()
Removes all of the mappings from this map.
-
remove
public boolean remove(int key, java.lang.Object value)Remove the specific entry with the given key and value, if it exist.- Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if the specified key is null
-
replace
public boolean replace(int key, V oldValue, V newValue)Replace the specific entry with the given key and value, with the given replacement value, if such an entry exist.- Throws:
java.lang.ClassCastException- if the specified key cannot be compared with the keys currently in the mapjava.lang.NullPointerException- if any of the arguments are null
-
firstKey
public int firstKey()
-
lastKey
public int lastKey()
-
lowerEntry
public ConcurrentSkipListIntObjMultimap.IntEntry<V> lowerEntry(int key)
Returns a key-value mapping associated with the greatest key strictly less than the given key, ornullif there is no such key. The returned entry does not support theEntry.setValuemethod.- Throws:
java.lang.NullPointerException- if the specified key is null
-
lowerKey
public int lowerKey(int key)
- Throws:
java.lang.NullPointerException- if the specified key is null
-
floorEntry
public ConcurrentSkipListIntObjMultimap.IntEntry<V> floorEntry(int key)
Returns a key-value mapping associated with the greatest key less than or equal to the given key, ornullif there is no such key. The returned entry does not support theEntry.setValuemethod.- Parameters:
key- the key- Throws:
java.lang.NullPointerException- if the specified key is null
-
floorKey
public int floorKey(int key)
- Parameters:
key- the key- Throws:
java.lang.NullPointerException- if the specified key is null
-
ceilingEntry
public ConcurrentSkipListIntObjMultimap.IntEntry<V> ceilingEntry(int key)
Returns a key-value mapping associated with the least key greater than or equal to the given key, ornullif there is no such entry. The returned entry does not support theEntry.setValuemethod.- Throws:
java.lang.NullPointerException- if the specified key is null
-
ceilingKey
public int ceilingKey(int key)
- Throws:
java.lang.NullPointerException- if the specified key is null
-
higherEntry
public ConcurrentSkipListIntObjMultimap.IntEntry<V> higherEntry(int key)
Returns a key-value mapping associated with the least key strictly greater than the given key, ornullif there is no such key. The returned entry does not support theEntry.setValuemethod.- Parameters:
key- the key- Throws:
java.lang.NullPointerException- if the specified key is null
-
higherKey
public int higherKey(int key)
- Parameters:
key- the key- Throws:
java.lang.NullPointerException- if the specified key is null
-
firstEntry
public ConcurrentSkipListIntObjMultimap.IntEntry<V> firstEntry()
Returns a key-value mapping associated with the least key in this map, ornullif the map is empty. The returned entry does not support theEntry.setValuemethod.
-
lastEntry
public ConcurrentSkipListIntObjMultimap.IntEntry<V> lastEntry()
Returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty. The returned entry does not support theEntry.setValuemethod.
-
pollFirstEntry
public ConcurrentSkipListIntObjMultimap.IntEntry<V> pollFirstEntry()
Removes and returns a key-value mapping associated with the least key in this map, ornullif the map is empty. The returned entry does not support theEntry.setValuemethod.
-
pollLastEntry
public ConcurrentSkipListIntObjMultimap.IntEntry<V> pollLastEntry()
Removes and returns a key-value mapping associated with the greatest key in this map, ornullif the map is empty. The returned entry does not support theEntry.setValuemethod.
-
pollCeilingEntry
public ConcurrentSkipListIntObjMultimap.IntEntry<V> pollCeilingEntry(int key)
-
iterator
public java.util.Iterator<ConcurrentSkipListIntObjMultimap.IntEntry<V>> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<V>
-
cls
private static <T> java.lang.Class<T> cls(java.lang.Class<?> cls)
-
acquireFence
private static void acquireFence()
Orders LOADS before the fence, with LOADS and STORES after the fence.
-
-