Class MapWithOrdinal<K,V>

java.lang.Object
org.apache.drill.common.collections.MapWithOrdinal<K,V>
Type Parameters:
K - key type
V - value type
All Implemented Interfaces:
Map<K,V>

public class MapWithOrdinal<K,V> extends Object implements Map<K,V>
An implementation of map that supports constant time look-up by a generic key or an ordinal. This class extends the functionality a regular Map with ordinal lookup support. Upon insertion an unused ordinal is assigned to the inserted (key, value) tuple. Upon update the same ordinal id is re-used while value is replaced. Upon deletion of an existing item, its corresponding ordinal is recycled and could be used by another item. For any instance with N items, this implementation guarantees that ordinals are in the range of [0, N). However, the ordinal assignment is dynamic and may change after an insertion or deletion. Consumers of this class are responsible for explicitly checking the ordinal corresponding to a key via getOrdinal(Object) before attempting to execute a lookup with an ordinal.
  • Constructor Details

    • MapWithOrdinal

      public MapWithOrdinal()
  • Method Details

    • getByOrdinal

      public V getByOrdinal(int id)
      Returns the value corresponding to the given ordinal
      Parameters:
      id - ordinal value for lookup
      Returns:
      an instance of V
    • getOrdinal

      public int getOrdinal(K key)
      Returns the ordinal corresponding to the given key.
      Parameters:
      key - key for ordinal lookup
      Returns:
      ordinal value corresponding to key if it exists or -1
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
    • put

      public V put(K key, V value)
      Inserts the tuple (key, value) into the map extending the semantics of Map.put(K, V) with automatic ordinal assignment. A new ordinal is assigned if key does not exists. Otherwise the same ordinal is re-used but the value is replaced.
      Specified by:
      put in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • remove

      public V remove(Object key)
      Removes the element corresponding to the key if exists extending the semantics of Map.remove(java.lang.Object) with ordinal re-cycling. The ordinal corresponding to the given key may be re-assigned to another tuple. It is important that consumer checks the ordinal value via getOrdinal(Object) before attempting to look-up by ordinal.
      Specified by:
      remove in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>