Package org.cache2k.core
Class StampedHash<K,V>
- java.lang.Object
-
- org.cache2k.core.StampedHash<K,V>
-
public class StampedHash<K,V> extends Object
Simple concurrent hash table implementation using optimistic locking via StampedLock for the segments locks.- Author:
- Jens Wilke
-
-
Constructor Summary
Constructors Constructor Description StampedHash(org.cache2k.Cache<?,?> cache)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longcalcEntryCount()Count the entries in the hash table, by scanning through the hash table.voidcheckExpand(int hash)Checks whether expansion is needed and expand wheninsertWithinLock(Entry, int, int)is used.voidclearWhenLocked()voidclose()Close the cache by discarding the entry table.intgetClearOrCloseCount()Entry<K,V>[]getEntries()Entry table used by the iterator.longgetEntryCapacity()StampedLockgetSegmentLock(int hash)longgetSegmentMaxFill()For testinglonggetSize()Number of hash table entries.longgetSizeWithGlobalLock()Separate version of getSize expected all segments are locked.Entry<K,V>insertWithinLock(Entry<K,V> e, int hash, int keyValue)Insert an entry.protected booleankeyObjIsEqual(K key, Entry e)Entry<K,V>lookup(K key, int hash, int keyValue)Lookup the entry in the hash table and return it.booleanremove(Entry<K,V> e)Remove existing entry from the hash.booleanremoveWithinLock(Entry<K,V> e, int hash)<T> TrunTotalLocked(Supplier<T> j)Lock all segments and run the job.protected intspreadHashFromEntry(int hc)Return the spreaded hash code from the hash code that is stored in the entry.
-
-
-
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.
-
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 wheninsertWithinLock(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.
-
spreadHashFromEntry
protected int spreadHashFromEntry(int hc)
Return the spreaded hash code from the hash code that is stored in the entry. For integer keys we store the key directly, so we need to calculate the spread again.
-
getSize
public long getSize()
Number of hash table entries. Uses locking to read the latest changes and consistent long values for 23 bit systems. May not be called when lock is held.
-
getSizeWithGlobalLock
public long getSizeWithGlobalLock()
Separate version of getSize expected all segments are locked.
-
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.
-
-