Package org.cache2k.core
Class Hash2<K,V>
- java.lang.Object
-
- org.cache2k.core.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 Summary
Constructors Constructor Description Hash2(org.cache2k.Cache cache)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
calcEntryCount()
Count the entries in the hash table, by scanning through the hash table.void
checkExpand(int hash)
Checks whether expansion is needed and expand wheninsertWithinLock(Entry, int, int)
is used.void
clearWhenLocked()
void
close()
Close the cache by discarding the entry table.int
getClearOrCloseCount()
Entry<K,V>[]
getEntries()
Entry table used by the iterator.long
getEntryCapacity()
StampedLock
getSegmentLock(int hash)
long
getSegmentMaxFill()
For testinglong
getSize()
Entry<K,V>
insertWithinLock(Entry<K,V> e, int hash, int keyValue)
Insert an entry.protected boolean
keyObjIsEqual(K key, Entry e)
Entry<K,V>
lookup(K key, int hash, int keyValue)
Lookup the entry in the hash table and return it.protected int
modifiedHashCode(int hc)
boolean
remove(Entry<K,V> e)
Remove existing entry from the hash.boolean
removeWithinLock(Entry<K,V> e, int hash)
<T> T
runTotalLocked(Supplier<T> j)
Lock all segments and run the job.
-
-
-
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.
-
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.
-
-