Class MapMakerInternalMap.Segment<K,V,E extends MapMakerInternalMap.InternalEntry<K,V,E>,S extends MapMakerInternalMap.Segment<K,V,E,S>>

java.lang.Object
java.util.concurrent.locks.ReentrantLock
com.google.common.collect.MapMakerInternalMap.Segment<K,V,E,S>
All Implemented Interfaces:
Serializable, Lock
Direct Known Subclasses:
MapMakerInternalMap.StrongKeyDummyValueSegment, MapMakerInternalMap.StrongKeyStrongValueSegment, MapMakerInternalMap.StrongKeyWeakValueSegment, MapMakerInternalMap.WeakKeyDummyValueSegment, MapMakerInternalMap.WeakKeyStrongValueSegment, MapMakerInternalMap.WeakKeyWeakValueSegment
Enclosing class:
MapMakerInternalMap<K,V,E extends MapMakerInternalMap.InternalEntry<K,V,E>,S extends MapMakerInternalMap.Segment<K,V,E,S>>

abstract static class MapMakerInternalMap.Segment<K,V,E extends MapMakerInternalMap.InternalEntry<K,V,E>,S extends MapMakerInternalMap.Segment<K,V,E,S>> extends ReentrantLock
Segments are specialized versions of hash tables. This subclass inherits from ReentrantLock opportunistically, just to simplify some locking and avoid separate construction.
  • Field Details

    • map

    • count

      volatile int count
      The number of live elements in this segment's region. This does not include unset elements which are awaiting cleanup.
    • modCount

      int modCount
      Number of updates that alter the size of the table. This is used during bulk-read methods to make sure they see a consistent snapshot: If modCounts change during a traversal of segments computing size or checking containsValue, then we might have an inconsistent view of state so (usually) must retry.
    • threshold

      int threshold
      The table is expanded when its size exceeds this threshold. (The value of this field is always (int) (capacity * 0.75).)
    • table

      @CheckForNull volatile AtomicReferenceArray<E extends MapMakerInternalMap.InternalEntry<K,V,E>> table
      The per-segment table.
    • readCount

      final AtomicInteger readCount
      A counter of the number of reads since the last write, used to drain queues on a small fraction of read operations.
  • Constructor Details

  • Method Details

    • self

      abstract S self()
      Returns this up-casted to the specific MapMakerInternalMap.Segment implementation type S.

      This method exists so that the MapMakerInternalMap.Segment code can be generic in terms of S, the type of the concrete implementation.

    • maybeDrainReferenceQueues

      void maybeDrainReferenceQueues()
      Drains the reference queues used by this segment, if any.
    • maybeClearReferenceQueues

      void maybeClearReferenceQueues()
      Clears the reference queues used by this segment, if any.
    • setValue

      void setValue(E entry, V value)
      Sets the value of the given entry.
    • copyEntry

      @CheckForNull E copyEntry(E original, E newNext)
      Returns a copy of the given entry.
    • newEntryArray

      AtomicReferenceArray<E> newEntryArray(int size)
    • initTable

      void initTable(AtomicReferenceArray<E> newTable)
    • castForTesting

      abstract E castForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
      Unsafe cast of the given entry to E, the type of the specific MapMakerInternalMap.InternalEntry implementation type.

      This method is provided as a convenience for tests. Otherwise they'd need to be knowledgeable about all the implementation details of our type system trickery.

    • getKeyReferenceQueueForTesting

      ReferenceQueue<K> getKeyReferenceQueueForTesting()
      Unsafely extracts the key reference queue used by this segment.
    • getValueReferenceQueueForTesting

      ReferenceQueue<V> getValueReferenceQueueForTesting()
      Unsafely extracts the value reference queue used by this segment.
    • getWeakValueReferenceForTesting

      MapMakerInternalMap.WeakValueReference<K,V,E> getWeakValueReferenceForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
      Unsafely extracts the weak value reference inside of the given entry.
    • newWeakValueReferenceForTesting

      MapMakerInternalMap.WeakValueReference<K,V,E> newWeakValueReferenceForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry, V value)
      Unsafely creates of a fresh MapMakerInternalMap.WeakValueReference, referencing the given value, for the given entry
    • setWeakValueReferenceForTesting

      void setWeakValueReferenceForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry, MapMakerInternalMap.WeakValueReference<K,V,? extends MapMakerInternalMap.InternalEntry<K,V,?>> valueReference)
      Unsafely sets the weak value reference inside the given entry to be the given valueReference
    • setTableEntryForTesting

      void setTableEntryForTesting(int i, MapMakerInternalMap.InternalEntry<K,V,?> entry)
      Unsafely sets the given index of this segment's internal hash table to be the given entry.
    • copyForTesting

      E copyForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry, @CheckForNull MapMakerInternalMap.InternalEntry<K,V,?> newNext)
      Unsafely returns a copy of the given entry.
    • setValueForTesting

      void setValueForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry, V value)
      Unsafely sets the value of the given entry.
    • newEntryForTesting

      E newEntryForTesting(K key, int hash, @CheckForNull MapMakerInternalMap.InternalEntry<K,V,?> next)
      Unsafely returns a fresh entry.
    • removeTableEntryForTesting

      boolean removeTableEntryForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
      Unsafely removes the given entry from this segment's hash table.
    • removeFromChainForTesting

      @CheckForNull E removeFromChainForTesting(MapMakerInternalMap.InternalEntry<K,V,?> first, MapMakerInternalMap.InternalEntry<K,V,?> entry)
      Unsafely removes the given entry from the given chain in this segment's hash table.
    • getLiveValueForTesting

      @CheckForNull V getLiveValueForTesting(MapMakerInternalMap.InternalEntry<K,V,?> entry)
      Unsafely returns the value of the given entry if it's still live, or null otherwise.
    • tryDrainReferenceQueues

      void tryDrainReferenceQueues()
      Cleanup collected entries when the lock is available.
    • drainKeyReferenceQueue

      void drainKeyReferenceQueue(ReferenceQueue<K> keyReferenceQueue)
    • drainValueReferenceQueue

      void drainValueReferenceQueue(ReferenceQueue<V> valueReferenceQueue)
    • clearReferenceQueue

      <T> void clearReferenceQueue(ReferenceQueue<T> referenceQueue)
    • getFirst

      @CheckForNull E getFirst(int hash)
      Returns first entry of bin for given hash.
    • getEntry

      @CheckForNull E getEntry(Object key, int hash)
    • getLiveEntry

      @CheckForNull E getLiveEntry(Object key, int hash)
    • get

      @CheckForNull V get(Object key, int hash)
    • containsKey

      boolean containsKey(Object key, int hash)
    • containsValue

      boolean containsValue(Object value)
      This method is a convenience for testing. Code should call MapMakerInternalMap.containsValue(java.lang.Object) directly.
    • put

      @CheckForNull V put(K key, int hash, V value, boolean onlyIfAbsent)
    • expand

      void expand()
      Expands the table if possible.
    • replace

      boolean replace(K key, int hash, V oldValue, V newValue)
    • replace

      @CheckForNull V replace(K key, int hash, V newValue)
    • remove

      @CheckForNull V remove(Object key, int hash)
    • remove

      boolean remove(Object key, int hash, Object value)
    • clear

      void clear()
    • removeFromChain

      @CheckForNull E removeFromChain(E first, E entry)
      Removes an entry from within a table. All entries following the removed node can stay, but all preceding ones need to be cloned.

      This method does not decrement count for the removed entry, but does decrement count for all partially collected entries which are skipped. As such callers which are modifying count must re-read it after calling removeFromChain.

      Parameters:
      first - the first entry of the table
      entry - the entry being removed from the table
      Returns:
      the new first entry for the table
    • reclaimKey

      boolean reclaimKey(E entry, int hash)
      Removes an entry whose key has been garbage collected.
    • reclaimValue

      boolean reclaimValue(K key, int hash, MapMakerInternalMap.WeakValueReference<K,V,E> valueReference)
      Removes an entry whose value has been garbage collected.
    • clearValueForTesting

      boolean clearValueForTesting(K key, int hash, MapMakerInternalMap.WeakValueReference<K,V,? extends MapMakerInternalMap.InternalEntry<K,V,?>> valueReference)
      Clears a value that has not yet been set, and thus does not require count to be modified.
    • removeEntryForTesting

      boolean removeEntryForTesting(E entry)
    • isCollected

      static <K, V, E extends MapMakerInternalMap.InternalEntry<K, V, E>> boolean isCollected(E entry)
      Returns true if the value has been partially collected, meaning that the value is null.
    • getLiveValue

      @CheckForNull V getLiveValue(E entry)
      Gets the value from an entry. Returns null if the entry is invalid or partially-collected.
    • postReadCleanup

      void postReadCleanup()
      Performs routine cleanup following a read. Normally cleanup happens during writes, or from the cleanupExecutor. If cleanup is not observed after a sufficient number of reads, try cleaning up from the read thread.
    • preWriteCleanup

      void preWriteCleanup()
      Performs routine cleanup prior to executing a write. This should be called every time a write thread acquires the segment lock, immediately after acquiring the lock.
    • runCleanup

      void runCleanup()
    • runLockedCleanup

      void runLockedCleanup()