Package org.terracotta.offheapstore
Class OffHeapHashMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- org.terracotta.offheapstore.OffHeapHashMap<K,V>
-
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
java.util.concurrent.locks.ReadWriteLock,java.util.Map<K,V>,HashingMap<K,V>,MapInternals,StorageEngine.Owner
- Direct Known Subclasses:
AbstractLockedOffHeapHashMap
public class OffHeapHashMap<K,V> extends java.util.AbstractMap<K,V> implements MapInternals, StorageEngine.Owner, HashingMap<K,V>
A hash-table implementation whose table is stored in an NIO direct buffer.The map stores keys and values encoded as integers, in an open-addressed linear-reprobing hashtable. Entries are 16-bytes wide, and consist of:
intstatus markerintcached key hashcodelong{key:value} representation
StorageEngineinstance provided at construction time.This
Mapimplementation is not thread-safe and does not support null keys or values.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classOffHeapHashMap.DirectEntryprotected classOffHeapHashMap.EncodingIteratorprotected classOffHeapHashMap.EncodingSetprotected classOffHeapHashMap.EntryIteratorprotected classOffHeapHashMap.EntrySetprotected classOffHeapHashMap.HashIterator<T>protected classOffHeapHashMap.KeyIteratorprotected classOffHeapHashMap.KeySet(package private) static classOffHeapHashMap.PendingPage
-
Field Summary
Fields Modifier and Type Field Description private static intALLOCATE_ON_CLEAR_THRESHOLD_RATIOprivate floatcurrentTableShrinkThresholdprivate static java.nio.IntBufferDESTROYED_TABLEprotected static intENCODINGprivate java.util.Set<java.lang.Long>encodingSetprotected static intENTRY_BIT_SHIFTprotected static intENTRY_SIZESize of a table entry in primitiveintunitsprivate java.util.Set<java.util.Map.Entry<K,V>>entrySetprotected java.nio.IntBufferhashtableThe current hash-table.protected PagehashTablePageprivate booleanhasUsedIteratorsprivate static intINITIAL_REPROBE_LENGTHprivate static intINITIAL_TABLE_SIZEprivate intinitialTableSizeprotected static intKEY_HASHCODEprivate java.util.Set<K>keySetprivate static org.slf4j.LoggerLOGGERprotected intmodCountprivate WeakIdentityHashMap<java.nio.IntBuffer,OffHeapHashMap.PendingPage>pendingTableFreesprotected intremovedSlotsprivate static intREPROBE_WARNING_THRESHOLDprotected intreprobeLimitstatic intRESERVED_STATUS_BITSprotected intsizeprotected static intSTATUSprotected static intSTATUS_REMOVEDprotected static intSTATUS_USEDprotected StorageEngine<? super K,? super V>storageEngineprivate static floatTABLE_RESIZE_THRESHOLDprivate static floatTABLE_SHRINK_THRESHOLDprivate booleantableAllocationsStealprivate java.lang.ThreadLocal<java.lang.Boolean>tableResizingprotected PageSourcetableSource
-
Constructor Summary
Constructors Modifier Constructor Description OffHeapHashMap(PageSource source, boolean tableAllocationsSteal, StorageEngine<? super K,? super V> storageEngine)OffHeapHashMap(PageSource source, boolean tableAllocationsSteal, StorageEngine<? super K,? super V> storageEngine, int tableSize)protectedOffHeapHashMap(PageSource source, boolean tableAllocationsSteal, StorageEngine<? super K,? super V> storageEngine, int tableSize, boolean bootstrap)OffHeapHashMap(PageSource source, StorageEngine<? super K,? super V> storageEngine)Construct an instance using a customBufferSourcefor the hashtable.OffHeapHashMap(PageSource source, StorageEngine<? super K,? super V> storageEngine, boolean bootstrap)OffHeapHashMap(PageSource source, StorageEngine<? super K,? super V> storageEngine, int tableSize)Construct an instance using a customBufferSourcefor the hashtable and a custom initial table size.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidadded(int position, java.nio.IntBuffer entry)private voidallocateOrClearTable(int size)private PageallocateTable(int size)private booleanbinaryKeyEquals(java.nio.ByteBuffer binaryProbeKey, int probeHash, long targetEncoding, int targetHash)voidclear()MetadataTuple<V>computeIfAbsentWithMetadata(K key, java.util.function.Function<? super K,? extends MetadataTuple<V>> mappingFunction)MetadataTuple<V>computeIfPresentWithMetadata(K key, java.util.function.BiFunction<? super K,? super MetadataTuple<V>,? extends MetadataTuple<V>> remappingFunction)MetadataTuple<V>computeWithMetadata(K key, java.util.function.BiFunction<? super K,? super MetadataTuple<V>,? extends MetadataTuple<V>> remappingFunction)booleancontainsKey(java.lang.Object key)protected java.util.Set<java.lang.Long>createEncodingSet()private static int[]createEntry(int hash, long encoding, int metadata)protected java.util.Set<java.util.Map.Entry<K,V>>createEntrySet()protected java.util.Set<K>createKeySet()voiddestroy()java.util.Set<java.lang.Long>encodingSet()java.util.Set<java.util.Map.Entry<K,V>>entrySet()booleanevict(int index, boolean shrink)private voidexpand(int start, int length)private PageexpandTable(int scale)Vfill(K key, V value)Associates the specified value with the specified key in this map.Vfill(K key, V value, int metadata)protected Vfill(K key, V value, int hash, int[] newEntry, int metadata)protected voidfreePendingTables()private voidfreeTable(Page tablePage)private voidfreeTable(Page tablePage, java.nio.IntBuffer table, int finalReprobe)Vget(java.lang.Object key)longgetAllocatedMemory()java.lang.IntegergetAndSetMetadata(java.lang.Object key, int mask, int values)protected VgetAtTableOffset(int offset)longgetDataAllocatedMemory()longgetDataOccupiedMemory()longgetDataSize()longgetDataVitalMemory()java.lang.LonggetEncodingForHashAndBinary(int hash, java.nio.ByteBuffer binaryKey)protected java.util.Map.Entry<K,V>getEntryAtTableOffset(int offset)java.lang.IntegergetMetadata(java.lang.Object key, int mask)longgetOccupiedMemory()longgetRemovedSlotCount()intgetReprobeLength()longgetSize()java.lang.IntegergetSlotForHashAndEncoding(int hash, long encoding, long mask)StorageEngine<? super K,? super V>getStorageEngine()longgetTableCapacity()longgetUsedSlotCount()VgetValueAndSetMetadata(java.lang.Object key, int mask, int values)longgetVitalMemory()protected voidhit(int position, java.nio.IntBuffer entry)protected intindexFor(int hash)protected static intindexFor(int hash, java.nio.IntBuffer table)private int[]installEntry(java.nio.ByteBuffer offheapBinaryKey, int pojoHash, java.nio.ByteBuffer offheapBinaryValue, int metadata)longinstallMappingForHashAndEncoding(int pojoHash, java.nio.ByteBuffer offheapBinaryKey, java.nio.ByteBuffer offheapBinaryValue, int metadata)protected static booleanisAvailable(java.nio.IntBuffer entry)protected static booleanisPresent(java.nio.IntBuffer entry)protected static booleanisRemoved(int entryStatus)protected static booleanisRemoved(java.nio.IntBuffer entry)protected static booleanisTerminating(int entryStatus)protected static booleanisTerminating(java.nio.IntBuffer entry)booleanisThiefForTableAllocations()private booleankeyEquals(java.lang.Object probeKey, int probeHash, long targetEncoding, int targetHash)java.util.Set<K>keySet()Vput(K key, V value)Vput(K key, V value, int metadata)java.util.concurrent.locks.LockreadLock()protected static longreadLong(int[] array, int offset)protected static longreadLong(java.nio.IntBuffer entry, int offset)Vremove(java.lang.Object key)java.util.Map<K,V>removeAllWithHash(int hash)Remove all keys having a specific hashcode.protected voidremoveAtTableOffset(int offset, boolean shrink)protected voidremoved(int position, java.nio.IntBuffer entry)protected booleanremoveMapping(java.lang.Object o)booleanremoveNoReturn(java.lang.Object key)private intreprobeLimit()private voidshrink()protected voidshrinkTable()private voidshrinkTableImpl()private PageshrinkTableImpl(int scale)intsize()private voidslotAdded(int position, java.nio.IntBuffer entry)private voidslotRemoved(int position, java.nio.IntBuffer entry)private voidslotUpdated(int position, java.nio.IntBuffer entry, long oldEncoding)protected static intspread(int hash)protected voidstorageEngineFailure(java.lang.Object failure)protected voidtableExpansionFailure(int start, int length)private booleantryExpand()private booleantryExpandTable()protected booleantryIncreaseReprobe()private int[]tryInstallEntry(java.nio.ByteBuffer offheapBinaryKey, int pojoHash, java.nio.ByteBuffer offheapBinaryValue, int metadata)private int[]tryWriteEntry(K key, int hash, V value, int metadata)protected voidupdated(int position, java.nio.IntBuffer entry)booleanupdateEncoding(int hash, long oldEncoding, long newEncoding, long mask)private static booleanupdateEncodingInTable(java.nio.IntBuffer table, int limit, int hash, long oldEncoding, long newEncoding, long mask)private voidupdatePendingTables(int hash, long oldEncoding, java.nio.IntBuffer newEntry)private voidwipePendingTables()private booleanwriteEntry(java.nio.IntBuffer table, java.nio.IntBuffer entry)private int[]writeEntry(K key, int hash, V value, int metadata)java.util.concurrent.locks.LockwriteLock()-
Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, isEmpty, putAll, toString, values
-
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
INITIAL_TABLE_SIZE
private static final int INITIAL_TABLE_SIZE
- See Also:
- Constant Field Values
-
TABLE_RESIZE_THRESHOLD
private static final float TABLE_RESIZE_THRESHOLD
- See Also:
- Constant Field Values
-
TABLE_SHRINK_THRESHOLD
private static final float TABLE_SHRINK_THRESHOLD
- See Also:
- Constant Field Values
-
INITIAL_REPROBE_LENGTH
private static final int INITIAL_REPROBE_LENGTH
- See Also:
- Constant Field Values
-
REPROBE_WARNING_THRESHOLD
private static final int REPROBE_WARNING_THRESHOLD
- See Also:
- Constant Field Values
-
ALLOCATE_ON_CLEAR_THRESHOLD_RATIO
private static final int ALLOCATE_ON_CLEAR_THRESHOLD_RATIO
- See Also:
- Constant Field Values
-
DESTROYED_TABLE
private static final java.nio.IntBuffer DESTROYED_TABLE
-
ENTRY_SIZE
protected static final int ENTRY_SIZE
Size of a table entry in primitiveintunits- See Also:
- Constant Field Values
-
ENTRY_BIT_SHIFT
protected static final int ENTRY_BIT_SHIFT
-
STATUS
protected static final int STATUS
- See Also:
- Constant Field Values
-
KEY_HASHCODE
protected static final int KEY_HASHCODE
- See Also:
- Constant Field Values
-
ENCODING
protected static final int ENCODING
- See Also:
- Constant Field Values
-
STATUS_USED
protected static final int STATUS_USED
- See Also:
- Constant Field Values
-
STATUS_REMOVED
protected static final int STATUS_REMOVED
- See Also:
- Constant Field Values
-
RESERVED_STATUS_BITS
public static final int RESERVED_STATUS_BITS
- See Also:
- Constant Field Values
-
storageEngine
protected final StorageEngine<? super K,? super V> storageEngine
-
tableSource
protected final PageSource tableSource
-
pendingTableFrees
private final WeakIdentityHashMap<java.nio.IntBuffer,OffHeapHashMap.PendingPage> pendingTableFrees
-
initialTableSize
private final int initialTableSize
-
tableAllocationsSteal
private final boolean tableAllocationsSteal
-
tableResizing
private final java.lang.ThreadLocal<java.lang.Boolean> tableResizing
-
size
protected volatile int size
-
modCount
protected volatile int modCount
-
reprobeLimit
protected int reprobeLimit
-
currentTableShrinkThreshold
private float currentTableShrinkThreshold
-
hasUsedIterators
private volatile boolean hasUsedIterators
-
hashtable
protected volatile java.nio.IntBuffer hashtable
The current hash-table.A list of:
int[] {status, hashCode, encoding-high, encoding-low}
-
hashTablePage
protected volatile Page hashTablePage
-
keySet
private java.util.Set<K> keySet
-
encodingSet
private java.util.Set<java.lang.Long> encodingSet
-
removedSlots
protected volatile int removedSlots
-
-
Constructor Detail
-
OffHeapHashMap
public OffHeapHashMap(PageSource source, StorageEngine<? super K,? super V> storageEngine)
Construct an instance using a customBufferSourcefor the hashtable.- Parameters:
source- source for the hashtable allocationsstorageEngine- engine used to encode the keys and values
-
OffHeapHashMap
public OffHeapHashMap(PageSource source, boolean tableAllocationsSteal, StorageEngine<? super K,? super V> storageEngine)
-
OffHeapHashMap
public OffHeapHashMap(PageSource source, StorageEngine<? super K,? super V> storageEngine, boolean bootstrap)
-
OffHeapHashMap
public OffHeapHashMap(PageSource source, StorageEngine<? super K,? super V> storageEngine, int tableSize)
Construct an instance using a customBufferSourcefor the hashtable and a custom initial table size.- Parameters:
source- source for the hashtable allocationsstorageEngine- engine used to encode the keys and valuestableSize- the initial table size
-
OffHeapHashMap
public OffHeapHashMap(PageSource source, boolean tableAllocationsSteal, StorageEngine<? super K,? super V> storageEngine, int tableSize)
-
OffHeapHashMap
protected OffHeapHashMap(PageSource source, boolean tableAllocationsSteal, StorageEngine<? super K,? super V> storageEngine, int tableSize, boolean bootstrap)
-
-
Method Detail
-
size
public int size()
-
containsKey
public boolean containsKey(java.lang.Object key)
-
get
public V get(java.lang.Object key)
-
getEncodingForHashAndBinary
public java.lang.Long getEncodingForHashAndBinary(int hash, java.nio.ByteBuffer binaryKey)- Specified by:
getEncodingForHashAndBinaryin interfaceStorageEngine.Owner
-
installMappingForHashAndEncoding
public long installMappingForHashAndEncoding(int pojoHash, java.nio.ByteBuffer offheapBinaryKey, java.nio.ByteBuffer offheapBinaryValue, int metadata)- Specified by:
installMappingForHashAndEncodingin interfaceStorageEngine.Owner
-
getMetadata
public java.lang.Integer getMetadata(java.lang.Object key, int mask)
-
getAndSetMetadata
public java.lang.Integer getAndSetMetadata(java.lang.Object key, int mask, int values)
-
getValueAndSetMetadata
public V getValueAndSetMetadata(java.lang.Object key, int mask, int values)
-
fill
public V fill(K key, V value)
Associates the specified value with the specified key in this map. If the map does not contain a mapping for the key, the new mapping is only installed if there is room. If the map previously contained a mapping for the key, the old value is replaced by the specified value even if this results in a failure or eviction.- Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key- Returns:
- the previous value associated with key, or null if there was no mapping for key (irrespective of whether the value was successfully installed).
-
installEntry
private int[] installEntry(java.nio.ByteBuffer offheapBinaryKey, int pojoHash, java.nio.ByteBuffer offheapBinaryValue, int metadata)
-
tryInstallEntry
private int[] tryInstallEntry(java.nio.ByteBuffer offheapBinaryKey, int pojoHash, java.nio.ByteBuffer offheapBinaryValue, int metadata)
-
createEntry
private static int[] createEntry(int hash, long encoding, int metadata)
-
remove
public V remove(java.lang.Object key)
-
removeAllWithHash
public java.util.Map<K,V> removeAllWithHash(int hash)
Description copied from interface:HashingMapRemove all keys having a specific hashcode.- Specified by:
removeAllWithHashin interfaceHashingMap<K,V>- Parameters:
hash- the hashcode of the keys to be removed.- Returns:
- a
Mapcontaining the removed mappings.
-
removeNoReturn
public boolean removeNoReturn(java.lang.Object key)
-
clear
public void clear()
-
destroy
public void destroy()
-
allocateOrClearTable
private void allocateOrClearTable(int size)
-
encodingSet
public final java.util.Set<java.lang.Long> encodingSet()
- Specified by:
encodingSetin interfaceStorageEngine.Owner
-
keySet
public final java.util.Set<K> keySet()
-
createEncodingSet
protected java.util.Set<java.lang.Long> createEncodingSet()
-
createKeySet
protected java.util.Set<K> createKeySet()
-
isPresent
protected static boolean isPresent(java.nio.IntBuffer entry)
-
isAvailable
protected static boolean isAvailable(java.nio.IntBuffer entry)
-
isTerminating
protected static boolean isTerminating(java.nio.IntBuffer entry)
-
isTerminating
protected static boolean isTerminating(int entryStatus)
-
isRemoved
protected static boolean isRemoved(java.nio.IntBuffer entry)
-
isRemoved
protected static boolean isRemoved(int entryStatus)
-
readLong
protected static long readLong(int[] array, int offset)
-
readLong
protected static long readLong(java.nio.IntBuffer entry, int offset)
-
indexFor
protected int indexFor(int hash)
-
indexFor
protected static int indexFor(int hash, java.nio.IntBuffer table)
-
keyEquals
private boolean keyEquals(java.lang.Object probeKey, int probeHash, long targetEncoding, int targetHash)
-
binaryKeyEquals
private boolean binaryKeyEquals(java.nio.ByteBuffer binaryProbeKey, int probeHash, long targetEncoding, int targetHash)
-
expand
private void expand(int start, int length)
-
tryExpand
private boolean tryExpand()
-
tryExpandTable
private boolean tryExpandTable()
-
expandTable
private Page expandTable(int scale)
-
tryIncreaseReprobe
protected boolean tryIncreaseReprobe()
-
shrinkTable
protected void shrinkTable()
-
shrink
private void shrink()
-
shrinkTableImpl
private void shrinkTableImpl()
-
shrinkTableImpl
private Page shrinkTableImpl(int scale)
-
writeEntry
private boolean writeEntry(java.nio.IntBuffer table, java.nio.IntBuffer entry)
-
spread
protected static int spread(int hash)
-
allocateTable
private Page allocateTable(int size)
-
freeTable
private void freeTable(Page tablePage, java.nio.IntBuffer table, int finalReprobe)
-
freeTable
private void freeTable(Page tablePage)
-
reprobeLimit
private int reprobeLimit()
-
freePendingTables
protected void freePendingTables()
-
updatePendingTables
private void updatePendingTables(int hash, long oldEncoding, java.nio.IntBuffer newEntry)
-
wipePendingTables
private void wipePendingTables()
-
removeMapping
protected boolean removeMapping(java.lang.Object o)
-
evict
public boolean evict(int index, boolean shrink)- Specified by:
evictin interfaceStorageEngine.Owner
-
removeAtTableOffset
protected void removeAtTableOffset(int offset, boolean shrink)
-
getAtTableOffset
protected V getAtTableOffset(int offset)
-
getSlotForHashAndEncoding
public java.lang.Integer getSlotForHashAndEncoding(int hash, long encoding, long mask)- Specified by:
getSlotForHashAndEncodingin interfaceStorageEngine.Owner
-
updateEncoding
public boolean updateEncoding(int hash, long oldEncoding, long newEncoding, long mask)- Specified by:
updateEncodingin interfaceStorageEngine.Owner
-
updateEncodingInTable
private static boolean updateEncodingInTable(java.nio.IntBuffer table, int limit, int hash, long oldEncoding, long newEncoding, long mask)
-
slotRemoved
private void slotRemoved(int position, java.nio.IntBuffer entry)
-
slotAdded
private void slotAdded(int position, java.nio.IntBuffer entry)
-
slotUpdated
private void slotUpdated(int position, java.nio.IntBuffer entry, long oldEncoding)
-
added
protected void added(int position, java.nio.IntBuffer entry)
-
hit
protected void hit(int position, java.nio.IntBuffer entry)
-
removed
protected void removed(int position, java.nio.IntBuffer entry)
-
updated
protected void updated(int position, java.nio.IntBuffer entry)
-
tableExpansionFailure
protected void tableExpansionFailure(int start, int length)
-
storageEngineFailure
protected void storageEngineFailure(java.lang.Object failure)
-
getSize
public long getSize()
- Specified by:
getSizein interfaceMapInternals- Specified by:
getSizein interfaceStorageEngine.Owner
-
getTableCapacity
public long getTableCapacity()
- Specified by:
getTableCapacityin interfaceMapInternals
-
getUsedSlotCount
public long getUsedSlotCount()
- Specified by:
getUsedSlotCountin interfaceMapInternals
-
getRemovedSlotCount
public long getRemovedSlotCount()
- Specified by:
getRemovedSlotCountin interfaceMapInternals
-
getReprobeLength
public int getReprobeLength()
- Specified by:
getReprobeLengthin interfaceMapInternals
-
getAllocatedMemory
public long getAllocatedMemory()
- Specified by:
getAllocatedMemoryin interfaceMapInternals
-
getOccupiedMemory
public long getOccupiedMemory()
- Specified by:
getOccupiedMemoryin interfaceMapInternals
-
getVitalMemory
public long getVitalMemory()
- Specified by:
getVitalMemoryin interfaceMapInternals
-
getDataAllocatedMemory
public long getDataAllocatedMemory()
- Specified by:
getDataAllocatedMemoryin interfaceMapInternals
-
getDataOccupiedMemory
public long getDataOccupiedMemory()
- Specified by:
getDataOccupiedMemoryin interfaceMapInternals
-
getDataVitalMemory
public long getDataVitalMemory()
- Specified by:
getDataVitalMemoryin interfaceMapInternals
-
getDataSize
public long getDataSize()
- Specified by:
getDataSizein interfaceMapInternals
-
isThiefForTableAllocations
public boolean isThiefForTableAllocations()
- Specified by:
isThiefForTableAllocationsin interfaceStorageEngine.Owner
-
readLock
public java.util.concurrent.locks.Lock readLock()
- Specified by:
readLockin interfacejava.util.concurrent.locks.ReadWriteLock
-
writeLock
public java.util.concurrent.locks.Lock writeLock()
- Specified by:
writeLockin interfacejava.util.concurrent.locks.ReadWriteLock
-
getStorageEngine
public StorageEngine<? super K,? super V> getStorageEngine()
-
computeWithMetadata
public MetadataTuple<V> computeWithMetadata(K key, java.util.function.BiFunction<? super K,? super MetadataTuple<V>,? extends MetadataTuple<V>> remappingFunction)
-
computeIfAbsentWithMetadata
public MetadataTuple<V> computeIfAbsentWithMetadata(K key, java.util.function.Function<? super K,? extends MetadataTuple<V>> mappingFunction)
-
computeIfPresentWithMetadata
public MetadataTuple<V> computeIfPresentWithMetadata(K key, java.util.function.BiFunction<? super K,? super MetadataTuple<V>,? extends MetadataTuple<V>> remappingFunction)
-
-