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:
ReadWriteLock, Map<K,V>, HashingMap<K, V>, MapInternals, StorageEngine.Owner
- Direct Known Subclasses:
AbstractLockedOffHeapHashMap
public class OffHeapHashMap<K,V>
extends 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
StorageEngine
instance provided at construction time.
This Map implementation is not thread-safe and does not support null
keys or values.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) classprotected classprotected classprotected classprotected classprotected classprotected classprotected class(package private) static classNested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate floatprivate static final IntBufferprotected static final intprotected static final intprotected static final intSize of a table entry in primitiveintunitsprotected IntBufferThe current hash-table.protected Pageprivate booleanprivate static final intprivate static final intprivate final intprotected static final intprivate static final org.slf4j.Loggerprotected intprivate final WeakIdentityHashMap<IntBuffer, OffHeapHashMap.PendingPage> protected intprivate static final intprotected intstatic final intprotected intprotected static final intprotected static final intprotected static final intprotected final StorageEngine<? super K, ? super V> private static final floatprivate static final floatprivate final booleanprivate final ThreadLocal<Boolean> protected final PageSource -
Constructor Summary
ConstructorsModifierConstructorDescriptionOffHeapHashMap(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
Modifier and TypeMethodDescriptionprotected voidprivate voidallocateOrClearTable(int size) private PageallocateTable(int size) private booleanbinaryKeyEquals(ByteBuffer binaryProbeKey, int probeHash, long targetEncoding, int targetHash) voidclear()computeIfAbsentWithMetadata(K key, Function<? super K, ? extends MetadataTuple<V>> mappingFunction) computeIfPresentWithMetadata(K key, BiFunction<? super K, ? super MetadataTuple<V>, ? extends MetadataTuple<V>> remappingFunction) computeWithMetadata(K key, BiFunction<? super K, ? super MetadataTuple<V>, ? extends MetadataTuple<V>> remappingFunction) booleancontainsKey(Object key) private static int[]createEntry(int hash, long encoding, int metadata) voiddestroy()entrySet()booleanevict(int index, boolean shrink) private voidexpand(int start, int length) private PageexpandTable(int scale) Associates the specified value with the specified key in this map.protected final Vprotected voidprivate voidprivate voidlonggetAndSetMetadata(Object key, int mask, int values) protected VgetAtTableOffset(int offset) longlonglonglonggetEncodingForHashAndBinary(int hash, ByteBuffer binaryKey) getEntryAtTableOffset(int offset) getMetadata(Object key, int mask) longlongintlonggetSize()getSlotForHashAndEncoding(int hash, long encoding, long mask) StorageEngine<? super K, ? super V> longlonggetValueAndSetMetadata(Object key, int mask, int values) longprotected voidprotected intindexFor(int hash) protected static intprivate int[]installEntry(ByteBuffer offheapBinaryKey, int pojoHash, ByteBuffer offheapBinaryValue, int metadata) longinstallMappingForHashAndEncoding(int pojoHash, ByteBuffer offheapBinaryKey, ByteBuffer offheapBinaryValue, int metadata) protected static booleanisAvailable(IntBuffer entry) protected static booleanprotected static booleanisRemoved(int entryStatus) protected static booleanprotected static booleanisTerminating(int entryStatus) protected static booleanisTerminating(IntBuffer entry) booleanprivate booleankeySet()readLock()protected static longreadLong(int[] array, int offset) protected static longremoveAllWithHash(int hash) Remove all keys having a specific hashcode.protected voidremoveAtTableOffset(int offset, boolean shrink) protected voidprotected booleanbooleanremoveNoReturn(Object key) private intprivate voidshrink()protected voidprivate voidprivate PageshrinkTableImpl(int scale) intsize()private voidprivate voidslotRemoved(int position, IntBuffer entry) private voidslotUpdated(int position, IntBuffer entry, long oldEncoding) protected static intspread(int hash) protected voidstorageEngineFailure(Object failure) protected voidtableExpansionFailure(int start, int length) private booleanprivate booleanprotected booleanprivate int[]tryInstallEntry(ByteBuffer offheapBinaryKey, int pojoHash, ByteBuffer offheapBinaryValue, int metadata) private int[]tryWriteEntry(K key, int hash, V value, int metadata) protected voidbooleanupdateEncoding(int hash, long oldEncoding, long newEncoding, long mask) private static booleanupdateEncodingInTable(IntBuffer table, int limit, int hash, long oldEncoding, long newEncoding, long mask) private voidupdatePendingTables(int hash, long oldEncoding, IntBuffer newEntry) private voidprivate booleanwriteEntry(IntBuffer table, IntBuffer entry) private int[]writeEntry(K key, int hash, V value, int metadata) Methods inherited from class AbstractMap
clone, containsValue, equals, hashCode, isEmpty, putAll, toString, valuesMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, containsValue, equals, forEach, getOrDefault, hashCode, isEmpty, merge, putAll, putIfAbsent, remove, replace, replace, replaceAll, values
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER -
INITIAL_TABLE_SIZE
private static final int INITIAL_TABLE_SIZE- See Also:
-
TABLE_RESIZE_THRESHOLD
private static final float TABLE_RESIZE_THRESHOLD- See Also:
-
TABLE_SHRINK_THRESHOLD
private static final float TABLE_SHRINK_THRESHOLD- See Also:
-
INITIAL_REPROBE_LENGTH
private static final int INITIAL_REPROBE_LENGTH- See Also:
-
REPROBE_WARNING_THRESHOLD
private static final int REPROBE_WARNING_THRESHOLD- See Also:
-
ALLOCATE_ON_CLEAR_THRESHOLD_RATIO
private static final int ALLOCATE_ON_CLEAR_THRESHOLD_RATIO- See Also:
-
DESTROYED_TABLE
-
ENTRY_SIZE
protected static final int ENTRY_SIZESize of a table entry in primitiveintunits- See Also:
-
ENTRY_BIT_SHIFT
protected static final int ENTRY_BIT_SHIFT -
STATUS
protected static final int STATUS- See Also:
-
KEY_HASHCODE
protected static final int KEY_HASHCODE- See Also:
-
ENCODING
protected static final int ENCODING- See Also:
-
STATUS_USED
protected static final int STATUS_USED- See Also:
-
STATUS_REMOVED
protected static final int STATUS_REMOVED- See Also:
-
RESERVED_STATUS_BITS
public static final int RESERVED_STATUS_BITS- See Also:
-
storageEngine
-
tableSource
-
pendingTableFrees
-
initialTableSize
private final int initialTableSize -
tableAllocationsSteal
private final boolean tableAllocationsSteal -
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
The current hash-table.A list of:
int[] {status, hashCode, encoding-high, encoding-low} -
hashTablePage
-
entrySet
-
keySet
-
encodingSet
-
removedSlots
protected volatile int removedSlots
-
-
Constructor Details
-
OffHeapHashMap
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 Details
-
size
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V>
-
get
-
getEncodingForHashAndBinary
- Specified by:
getEncodingForHashAndBinaryin interfaceStorageEngine.Owner
-
installMappingForHashAndEncoding
public long installMappingForHashAndEncoding(int pojoHash, ByteBuffer offheapBinaryKey, ByteBuffer offheapBinaryValue, int metadata) - Specified by:
installMappingForHashAndEncodingin interfaceStorageEngine.Owner
-
getMetadata
-
getAndSetMetadata
-
getValueAndSetMetadata
-
put
-
put
-
fill
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).
-
fill
-
fill
-
writeEntry
-
tryWriteEntry
-
installEntry
private int[] installEntry(ByteBuffer offheapBinaryKey, int pojoHash, ByteBuffer offheapBinaryValue, int metadata) -
tryInstallEntry
private int[] tryInstallEntry(ByteBuffer offheapBinaryKey, int pojoHash, ByteBuffer offheapBinaryValue, int metadata) -
createEntry
private static int[] createEntry(int hash, long encoding, int metadata) -
remove
-
removeAllWithHash
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
-
clear
-
destroy
public void destroy() -
allocateOrClearTable
private void allocateOrClearTable(int size) -
entrySet
-
encodingSet
- Specified by:
encodingSetin interfaceStorageEngine.Owner
-
keySet
-
createEntrySet
-
createEncodingSet
-
createKeySet
-
isPresent
-
isAvailable
-
isTerminating
-
isTerminating
protected static boolean isTerminating(int entryStatus) -
isRemoved
-
isRemoved
protected static boolean isRemoved(int entryStatus) -
readLong
protected static long readLong(int[] array, int offset) -
readLong
-
indexFor
protected int indexFor(int hash) -
indexFor
-
keyEquals
-
binaryKeyEquals
private boolean binaryKeyEquals(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
-
tryIncreaseReprobe
protected boolean tryIncreaseReprobe() -
shrinkTable
protected void shrinkTable() -
shrink
private void shrink() -
shrinkTableImpl
private void shrinkTableImpl() -
shrinkTableImpl
-
writeEntry
-
spread
protected static int spread(int hash) -
allocateTable
-
freeTable
-
freeTable
-
reprobeLimit
private int reprobeLimit() -
freePendingTables
protected void freePendingTables() -
updatePendingTables
-
wipePendingTables
private void wipePendingTables() -
removeMapping
-
evict
public boolean evict(int index, boolean shrink) - Specified by:
evictin interfaceStorageEngine.Owner
-
removeAtTableOffset
protected void removeAtTableOffset(int offset, boolean shrink) -
getAtTableOffset
-
getEntryAtTableOffset
-
getSlotForHashAndEncoding
- 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(IntBuffer table, int limit, int hash, long oldEncoding, long newEncoding, long mask) -
slotRemoved
-
slotAdded
-
slotUpdated
-
added
-
hit
-
removed
-
updated
-
tableExpansionFailure
protected void tableExpansionFailure(int start, int length) -
storageEngineFailure
-
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
- Specified by:
readLockin interfaceReadWriteLock
-
writeLock
- Specified by:
writeLockin interfaceReadWriteLock
-
getStorageEngine
-
computeWithMetadata
public MetadataTuple<V> computeWithMetadata(K key, BiFunction<? super K, ? super MetadataTuple<V>, ? extends MetadataTuple<V>> remappingFunction) -
computeIfAbsentWithMetadata
public MetadataTuple<V> computeIfAbsentWithMetadata(K key, Function<? super K, ? extends MetadataTuple<V>> mappingFunction) -
computeIfPresentWithMetadata
public MetadataTuple<V> computeIfPresentWithMetadata(K key, BiFunction<? super K, ? super MetadataTuple<V>, ? extends MetadataTuple<V>> remappingFunction)
-