Class Hash2<K,​V>


  • public class Hash2<K,​V>
    extends Object
    Simple concurrent hash table implementation using optimistic locking for the segments locks.
    Author:
    Jens Wilke
    • Constructor Detail

      • Hash2

        public Hash2​(org.cache2k.Cache cache)
        Parameters:
        cache - Cache reference only needed for the cache name in case of an exception
    • Method Detail

      • getEntryCapacity

        public long getEntryCapacity()
      • getSegmentMaxFill

        public long getSegmentMaxFill()
        For testing
      • lookup

        public Entry<K,​V> lookup​(K key,
                                       int hash,
                                       int keyValue)
        Lookup the entry in the hash table and return it. First tries an optimistic read.
      • keyObjIsEqual

        protected boolean keyObjIsEqual​(K key,
                                        Entry e)
      • insertWithinLock

        public Entry<K,​V> insertWithinLock​(Entry<K,​V> e,
                                                 int hash,
                                                 int keyValue)
        Insert an entry. Checks if an entry already exists.
      • checkExpand

        public void checkExpand​(int hash)
        Checks whether expansion is needed and expand when insertWithinLock(Entry, int, int) is used. No lock may be hold when calling this method, since the table must be locked completely using the proper lock order.

        Need for expansion is only checked by comparing whether the associated segment is full. Should be called after insert after giving up the lock.

      • getSegmentLock

        public StampedLock getSegmentLock​(int hash)
      • remove

        public boolean remove​(Entry<K,​V> e)
        Remove existing entry from the hash.
        Returns:
        true, if entry was found and removed.
      • removeWithinLock

        public boolean removeWithinLock​(Entry<K,​V> e,
                                        int hash)
      • modifiedHashCode

        protected int modifiedHashCode​(int hc)
      • getSize

        public long getSize()
      • runTotalLocked

        public <T> T runTotalLocked​(Supplier<T> j)
        Lock all segments and run the job.
      • clearWhenLocked

        public void clearWhenLocked()
      • getClearOrCloseCount

        public int getClearOrCloseCount()
      • close

        public void close()
        Close the cache by discarding the entry table. Assumes total lock.

        Closing will be visible to other threads, because of the guarantees of the locking. Using the entry table for closing has the advantage that the close check collapses with the implicit null check and has no additional overhead.

      • calcEntryCount

        public long calcEntryCount()
        Count the entries in the hash table, by scanning through the hash table. This is used for integrity checks.
      • getEntries

        public Entry<K,​V>[] getEntries()
        Entry table used by the iterator.