public class Segment
extends java.util.concurrent.locks.ReentrantReadWriteLock
The segment extends ReentrantReadWriteLock to allow read locking on read operations. In addition to the typical CHM-like methods, this classes additionally supports replacement under a read lock - which is accomplished using an atomic CAS on the associated HashEntry.
Modifier and Type | Class and Description |
---|---|
(package private) class |
Segment.HashIterator
An iterator over the HashEntry objects within this Segment.
|
Modifier and Type | Field and Description |
---|---|
private RegisteredEventListeners |
cacheEventNotificationService |
private boolean |
cachePinned |
protected int |
count
Count of elements in the map.
|
private DiskStorageFactory |
disk
The primary substitute factory.
|
private RateStatistic |
diskHitRate |
private RateStatistic |
diskMissRate |
private static float |
LOAD_FACTOR |
private static org.slf4j.Logger |
LOG |
private static int |
MAXIMUM_CAPACITY |
protected int |
modCount
Mod-count used to track concurrent modifications when doing size calculations or iterating over the store.
|
private static HashEntry |
NULL_HASH_ENTRY |
private PoolAccessor |
onDiskPoolAccessor |
private PoolAccessor |
onHeapPoolAccessor |
private HashEntry[] |
table
Table of HashEntry linked lists, indexed by the least-significant bits of the spread-hash value.
|
private int |
threshold
Size at which the next rehashing of this Segment should occur
|
Constructor and Description |
---|
Segment(int initialCapacity,
float loadFactor,
DiskStorageFactory primary,
CacheConfiguration cacheConfiguration,
PoolAccessor onHeapPoolAccessor,
PoolAccessor onDiskPoolAccessor,
RegisteredEventListeners cacheEventNotificationService)
Create a Segment with the given initial capacity, load-factor, primary element substitute factory, and identity element substitute factory.
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
addRandomSample(ElementSubstituteFilter filter,
int sampleSize,
java.util.Collection<DiskStorageFactory.DiskSubstitute> sampled,
int seed)
Select a random sample of elements generated by the supplied factory.
|
(package private) boolean |
cleanUpFailedMarker(java.io.Serializable key,
int hash)
Will check whether a Placeholder that failed to flush to disk is lying around
If so, it'll try to evict it
|
(package private) void |
clear()
Removes all mappings from this segment.
|
(package private) boolean |
containsKey(java.lang.Object key,
int hash)
Return true if this segment contains a mapping for this key
|
private Element |
decode(java.lang.Object object)
Decode the possible DiskSubstitute
|
private Element |
decodeHit(java.lang.Object object)
Decode the possible DiskSubstitute, updating the statistics
|
private static boolean |
determineCachePinned(CacheConfiguration cacheConfiguration) |
protected void |
diskHit()
Record a hit in the disk tier
|
(package private) Element |
evict(java.lang.Object key,
int hash,
DiskStorageFactory.DiskSubstitute value)
Remove the matching mapping.
|
(package private) Element |
evict(java.lang.Object key,
int hash,
DiskStorageFactory.DiskSubstitute value,
boolean notify)
Remove the matching mapping.
|
(package private) boolean |
fault(java.lang.Object key,
int hash,
DiskStorageFactory.Placeholder expect,
DiskStorageFactory.DiskMarker fault)
Try to atomically switch (CAS) the
expect representation of this element for the
fault representation. |
private void |
free(java.lang.Object object)
Free the DiskSubstitute
|
private void |
free(java.lang.Object object,
boolean faultFailure)
Free the DiskSubstitute indicating if it could not be faulted
|
(package private) Element |
get(java.lang.Object key,
int hash)
Get the element mapped to this key (or null if there is no mapping for this key)
|
float |
getDiskHitRate()
Return the disk hit rate
|
float |
getDiskMissRate()
Return the disk miss rate
|
private HashEntry |
getFirst(int hash) |
(package private) java.util.Iterator<HashEntry> |
hashIterator()
Creates an iterator over the HashEntry objects within this Segment.
|
protected void |
miss()
Record a miss in the disk tier
|
private void |
notifyEviction(Element evicted) |
(package private) Element |
put(java.lang.Object key,
int hash,
Element element,
boolean onlyIfAbsent)
Add the supplied mapping.
|
(package private) boolean |
putRawIfAbsent(java.lang.Object key,
int hash,
DiskStorageFactory.DiskMarker encoded)
Add the supplied pre-encoded mapping.
|
private void |
rehash() |
(package private) Element |
remove(java.lang.Object key,
int hash,
Element value,
ElementValueComparator comparator)
Remove the matching mapping.
|
(package private) void |
removeNoReturn(java.lang.Object key,
int hash)
Remove the matching mapping.
|
(package private) Element |
replace(java.lang.Object key,
int hash,
Element newElement)
Replace the entry for this key only if currently mapped to some element.
|
(package private) boolean |
replace(java.lang.Object key,
int hash,
Element oldElement,
Element newElement,
ElementValueComparator comparator)
Replace the element mapped to this key only if currently mapped to the given element.
|
java.lang.String |
toString() |
(package private) java.lang.Object |
unretrievedGet(java.lang.Object key,
int hash)
Return the unretrieved (undecoded) value for this key
|
getOwner, getQueuedReaderThreads, getQueuedThreads, getQueuedWriterThreads, getQueueLength, getReadHoldCount, getReadLockCount, getWaitingThreads, getWaitQueueLength, getWriteHoldCount, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isWriteLocked, isWriteLockedByCurrentThread, readLock, writeLock
private static final org.slf4j.Logger LOG
private static final HashEntry NULL_HASH_ENTRY
private static final float LOAD_FACTOR
private static final int MAXIMUM_CAPACITY
protected volatile int count
A volatile reference is needed here for the same reasons as in the table reference.
protected int modCount
Note that we don't actually have any iterators yet...
private final DiskStorageFactory disk
This is the substitute type used to store Element
s when they are first added to the store.
private volatile HashEntry[] table
A volatile reference is needed to ensure the visibility of table changes made during rehash operations to size operations. Key operations are done under read-locks so there is no need for volatility in that regard. Hence if we switched to read-locked size operations, we wouldn't need a volatile reference here.
private int threshold
private final RateStatistic diskHitRate
private final RateStatistic diskMissRate
private final PoolAccessor onHeapPoolAccessor
private final PoolAccessor onDiskPoolAccessor
private final RegisteredEventListeners cacheEventNotificationService
private volatile boolean cachePinned
public Segment(int initialCapacity, float loadFactor, DiskStorageFactory primary, CacheConfiguration cacheConfiguration, PoolAccessor onHeapPoolAccessor, PoolAccessor onDiskPoolAccessor, RegisteredEventListeners cacheEventNotificationService)
An identity element substitute factory is specified at construction time because only one subclass of IdentityElementProxyFactory
can be used with a Segment. Without this requirement the mapping between bare Element
instances and the factory
responsible for them would be ambiguous.
If a null
identity element substitute factory is specified then encountering a raw element (i.e. as a result of using an
identity element substitute factory) will result in a null pointer exception during decode.
initialCapacity
- initial capacity of storeloadFactor
- fraction of capacity at which rehash occursprimary
- primary element substitute factorycacheConfiguration
- the cache configurationonHeapPoolAccessor
- the pool tracking on-heap usageonDiskPoolAccessor
- the pool tracking on-disk usagecacheEventNotificationService
- private static boolean determineCachePinned(CacheConfiguration cacheConfiguration)
private HashEntry getFirst(int hash)
private Element decode(java.lang.Object object)
object
- the DiskSubstitute to decodeprivate Element decodeHit(java.lang.Object object)
object
- the DiskSubstitute to decodeprivate void free(java.lang.Object object)
object
- the DiskSubstitute to freeprivate void free(java.lang.Object object, boolean faultFailure)
object
- the DiskSubstitute to freefaultFailure
- true if the DiskSubstitute should be freed because of a fault failureElement get(java.lang.Object key, int hash)
key
- key to lookuphash
- spread-hash for this keyjava.lang.Object unretrievedGet(java.lang.Object key, int hash)
key
- key to lookuphash
- spread-hash for the keyboolean containsKey(java.lang.Object key, int hash)
key
- key to check forhash
- spread-hash for keytrue
if there is a mapping for this keyboolean replace(java.lang.Object key, int hash, Element oldElement, Element newElement, ElementValueComparator comparator)
key
- key to map the element tohash
- spread-hash for the keyoldElement
- expected elementnewElement
- element to addcomparator
- the comparator to use to compare valuestrue
on a successful replaceElement replace(java.lang.Object key, int hash, Element newElement)
key
- key to map the element tohash
- spread-hash for the keynewElement
- element to addElement put(java.lang.Object key, int hash, Element element, boolean onlyIfAbsent)
The supplied element is substituted using the primary element proxy factory
before being stored in the cache. If onlyIfAbsent
is set
then the mapping will only be added if no element is currently mapped
to that key.
key
- key to map the element tohash
- spread-hash for the keyelement
- element to storeonlyIfAbsent
- if true does not replace existing mappingsboolean putRawIfAbsent(java.lang.Object key, int hash, DiskStorageFactory.DiskMarker encoded) throws java.lang.IllegalArgumentException
The supplied encoded element is directly inserted into the segment if there is no other mapping for this key.
key
- key to map the element tohash
- spread-hash for the keyencoded
- encoded element to storetrue
if the encoded element was installedjava.lang.IllegalArgumentException
- if the supplied key is already presentprivate void rehash()
Element remove(java.lang.Object key, int hash, Element value, ElementValueComparator comparator)
If value
is null
then match on the key only,
else match on both the key and the value.
key
- key to match againsthash
- spread-hash for the keyvalue
- optional value to match againstcomparator
- the comparator to use to compare valuesvoid removeNoReturn(java.lang.Object key, int hash)
key
- key to match againsthash
- spread-hash for the keyvoid clear()
boolean fault(java.lang.Object key, int hash, DiskStorageFactory.Placeholder expect, DiskStorageFactory.DiskMarker fault)
expect
representation of this element for the
fault
representation.
A successful switch will return true
, and free the replaced element/element-proxy.
A failed switch will return false
and free the element/element-proxy which was not
installed. Unlike fault
this method can return false
if the object
could not be installed due to lock contention.
key
- key to which this element (proxy) is mappedhash
- the hash of the keyexpect
- element (proxy) expectedfault
- element (proxy) to installtrue
if fault
was installedprivate void notifyEviction(Element evicted)
Element evict(java.lang.Object key, int hash, DiskStorageFactory.DiskSubstitute value)
remove(Object, int, net.sf.ehcache.Element, net.sf.ehcache.store.ElementValueComparator)
method
evict does referential comparison of the unretrieved substitute against the argument value.key
- key to match againsthash
- spread-hash for the keyvalue
- optional value to match againsttrue
on a successful removeElement evict(java.lang.Object key, int hash, DiskStorageFactory.DiskSubstitute value, boolean notify)
remove(Object, int, net.sf.ehcache.Element, net.sf.ehcache.store.ElementValueComparator)
method
evict does referential comparison of the unretrieved substitute against the argument value.key
- key to match againsthash
- spread-hash for the keyvalue
- optional value to match againstnotify
- whether to notify if we evict somethingtrue
on a successful removevoid addRandomSample(ElementSubstituteFilter filter, int sampleSize, java.util.Collection<DiskStorageFactory.DiskSubstitute> sampled, int seed)
filter
- filter of substitute typessampleSize
- minimum number of elements to returnsampled
- collection in which to place the elementsseed
- random seed for the selectionjava.util.Iterator<HashEntry> hashIterator()
public java.lang.String toString()
toString
in class java.util.concurrent.locks.ReentrantReadWriteLock
boolean cleanUpFailedMarker(java.io.Serializable key, int hash)
key
- the keyhash
- the key's hashpublic float getDiskHitRate()
public float getDiskMissRate()
protected void diskHit()
protected void miss()