Class ConcurrentSkipListIntObjMultimap<V>

java.lang.Object
io.netty.util.concurrent.ConcurrentSkipListIntObjMultimap<V>
Type Parameters:
V - the type of mapped values
All Implemented Interfaces:
Iterable<ConcurrentSkipListIntObjMultimap.IntEntry<V>>

public class ConcurrentSkipListIntObjMultimap<V> extends Object implements Iterable<ConcurrentSkipListIntObjMultimap.IntEntry<V>>
A scalable concurrent multimap implementation. The map is sorted according to the natural ordering of its int keys.

This class implements a concurrent variant of SkipLists providing expected average log(n) time cost for the containsKey, get, put and remove operations 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, or computeIfPresent, cannot be supported. Likewise, some get-like operations cannot be supported.

Iterators and spliterators are weakly consistent.

All IntEntry pairs returned by methods in this class represent snapshots of mappings at the time they were produced. They do not support the Entry.setValue method. (Note however that it is possible to change mappings in the associated map using put, putIfAbsent, or replace, depending on exactly which effect you need.)

Beware that bulk operations putAll, equals, toArray, containsValue, and clear are not guaranteed to be performed atomically. For example, an iterator operating concurrently with a putAll operation might view only some of the added elements.

This class does not permit the use of null values because some null return values cannot be reliably distinguished from the absence of elements.

  • Field Details

  • Constructor Details

    • ConcurrentSkipListIntObjMultimap

      public ConcurrentSkipListIntObjMultimap(int noKey)
      Constructs a new, empty map, sorted according to the natural ordering of the keys.
      Parameters:
      noKey - The value to use as a sentinel for signaling the absence of a key.
  • Method Details

    • 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

      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, predecessor
      n - 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 key
      value - the value that must be associated with key
      onlyIfAbsent - 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 level
      skips - levels to skip before inserting
      x - index for this insertion
    • doRemove

      final V doRemove(int key, 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 key
      value - 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

      Gets first valid node, unlinking deleted nodes if encountered.
      Returns:
      first node or null if empty
    • 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

      Specialized version of find to get last valid node.
      Returns:
      last node or null if empty
    • 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 key
      rel - 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 key
      rel - 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)
      Returns true if this map contains a mapping for the specified key.
      Parameters:
      key - key whose presence in this map is to be tested
      Returns:
      true if this map contains a mapping for the specified key
      Throws:
      ClassCastException - if the specified key cannot be compared with the keys currently in the map
      NullPointerException - if the specified key is null
    • get

      public V get(int key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

      More formally, if this map contains a mapping from a key k to a value v such that key compares equal to k according to the map's ordering, then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

      Throws:
      ClassCastException - if the specified key cannot be compared with the keys currently in the map
      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 key
      defaultValue - 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:
      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 associated
      value - value to be associated with the specified key
      Throws:
      ClassCastException - if the specified key cannot be compared with the keys currently in the map
      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 null if there was no mapping for the key
      Throws:
      ClassCastException - if the specified key cannot be compared with the keys currently in the map
      NullPointerException - if the specified key is null
    • containsValue

      public boolean containsValue(Object value)
      Returns true if 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:
      true if a mapping to value exists; false otherwise
      Throws:
      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, Object value)
      Remove the specific entry with the given key and value, if it exist.
      Throws:
      ClassCastException - if the specified key cannot be compared with the keys currently in the map
      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:
      ClassCastException - if the specified key cannot be compared with the keys currently in the map
      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, or null if there is no such key. The returned entry does not support the Entry.setValue method.
      Throws:
      NullPointerException - if the specified key is null
    • lowerKey

      public int lowerKey(int key)
      Throws:
      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, or null if there is no such key. The returned entry does not support the Entry.setValue method.
      Parameters:
      key - the key
      Throws:
      NullPointerException - if the specified key is null
    • floorKey

      public int floorKey(int key)
      Parameters:
      key - the key
      Throws:
      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, or null if there is no such entry. The returned entry does not support the Entry.setValue method.
      Throws:
      NullPointerException - if the specified key is null
    • ceilingKey

      public int ceilingKey(int key)
      Throws:
      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, or null if there is no such key. The returned entry does not support the Entry.setValue method.
      Parameters:
      key - the key
      Throws:
      NullPointerException - if the specified key is null
    • higherKey

      public int higherKey(int key)
      Parameters:
      key - the key
      Throws:
      NullPointerException - if the specified key is null
    • firstEntry

      Returns a key-value mapping associated with the least key in this map, or null if the map is empty. The returned entry does not support the Entry.setValue method.
    • lastEntry

      Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty. The returned entry does not support the Entry.setValue method.
    • pollFirstEntry

      public ConcurrentSkipListIntObjMultimap.IntEntry<V> pollFirstEntry()
      Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty. The returned entry does not support the Entry.setValue method.
    • pollLastEntry

      Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty. The returned entry does not support the Entry.setValue method.
    • pollCeilingEntry

      public ConcurrentSkipListIntObjMultimap.IntEntry<V> pollCeilingEntry(int key)
    • iterator

      Specified by:
      iterator in interface Iterable<V>
    • cls

      private static <T> Class<T> cls(Class<?> cls)
    • acquireFence

      private static void acquireFence()
      Orders LOADS before the fence, with LOADS and STORES after the fence.