org.terracotta.offheapstore
Class OffHeapHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by org.terracotta.offheapstore.OffHeapHashMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - 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:

Key and value representations are generated by the StorageEngine instance provided at construction time.

This Map implementation is not thread-safe and does not support null keys or values.

Author:
Chris Dennis

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected static int ENCODING
           
protected static int ENTRY_BIT_SHIFT
           
protected static int ENTRY_SIZE
          Size of a table entry in primitive int units
protected  IntBuffer hashtable
          The current hash-table.
protected  Page hashTablePage
           
protected static int KEY_HASHCODE
           
protected  int modCount
           
protected  int removedSlots
           
protected  int reprobeLimit
           
static int RESERVED_STATUS_BITS
           
protected  int size
           
protected static int STATUS
           
protected static int STATUS_REMOVED
           
protected static int STATUS_USED
           
protected  StorageEngine<? super K,? super V> storageEngine
           
protected  PageSource tableSource
           
 
Constructor Summary
  OffHeapHashMap(PageSource source, boolean tableAllocationsSteal, StorageEngine<? super K,? super V> storageEngine)
           
  OffHeapHashMap(PageSource source, boolean tableAllocationsSteal, StorageEngine<? super K,? super V> storageEngine, int tableSize)
           
protected OffHeapHashMap(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 custom BufferSource for 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 custom BufferSource for the hashtable and a custom initial table size.
 
Method Summary
protected  void added(IntBuffer entry)
           
 void clear()
           
 MetadataTuple<V> computeIfAbsentWithMetadata(K key, Function<? super K,? extends MetadataTuple<V>> mappingFunction)
           
 MetadataTuple<V> computeIfPresentWithMetadata(K key, BiFunction<? super K,? super MetadataTuple<V>,? extends MetadataTuple<V>> remappingFunction)
           
 MetadataTuple<V> computeWithMetadata(K key, BiFunction<? super K,? super MetadataTuple<V>,? extends MetadataTuple<V>> remappingFunction)
           
 boolean containsKey(Object key)
           
 void destroy()
           
 Set<Long> encodingSet()
           
 Set<Map.Entry<K,V>> entrySet()
           
 boolean evict(int index, boolean shrink)
           
 V fill(K key, V value)
          Associates the specified value with the specified key in this map.
 V fill(K key, V value, int metadata)
           
protected  V fill(K key, V value, int hash, int[] newEntry, int metadata)
           
protected  void freePendingTables()
           
 V get(Object key)
           
 long getAllocatedMemory()
           
 Integer getAndSetMetadata(Object key, int mask, int values)
           
protected  V getAtTableOffset(int offset)
           
 long getDataAllocatedMemory()
           
 long getDataOccupiedMemory()
           
 long getDataSize()
           
 long getDataVitalMemory()
           
 Long getEncodingForHashAndBinary(int hash, ByteBuffer binaryKey)
           
protected  Map.Entry<K,V> getEntryAtTableOffset(int offset)
           
 Integer getMetadata(Object key, int mask)
           
 long getOccupiedMemory()
           
 long getRemovedSlotCount()
           
 int getReprobeLength()
           
 long getSize()
           
 Integer getSlotForHashAndEncoding(int hash, long encoding, long mask)
           
 StorageEngine<? super K,? super V> getStorageEngine()
           
 long getTableCapacity()
           
 long getUsedSlotCount()
           
 V getValueAndSetMetadata(Object key, int mask, int values)
           
 long getVitalMemory()
           
protected  void hit(IntBuffer entry)
           
protected  int indexFor(int hash)
           
protected static int indexFor(int hash, IntBuffer table)
           
 long installMappingForHashAndEncoding(int pojoHash, ByteBuffer offheapBinaryKey, ByteBuffer offheapBinaryValue, int metadata)
           
protected static boolean isAvailable(IntBuffer entry)
           
protected static boolean isPresent(IntBuffer entry)
           
protected static boolean isRemoved(int entryStatus)
           
protected static boolean isRemoved(IntBuffer entry)
           
protected static boolean isTerminating(int entryStatus)
           
protected static boolean isTerminating(IntBuffer entry)
           
 boolean isThiefForTableAllocations()
           
 Set<K> keySet()
           
 V put(K key, V value)
           
 V put(K key, V value, int metadata)
           
 Lock readLock()
           
protected static long readLong(int[] array, int offset)
           
protected static long readLong(IntBuffer entry, int offset)
           
 V remove(Object key)
           
 Map<K,V> removeAllWithHash(int hash)
          Remove all keys having a specific hashcode.
protected  void removeAtTableOffset(int offset, boolean shrink)
           
protected  void removed(IntBuffer entry)
           
protected  boolean removeMapping(Object o)
           
 boolean removeNoReturn(Object key)
           
protected  void shrinkTable()
           
 int size()
           
protected static int spread(int hash)
           
protected  void storageEngineFailure(Object failure)
           
protected  void tableExpansionFailure(int start, int length)
           
protected  boolean tryIncreaseReprobe()
           
protected  void updated(IntBuffer entry)
           
 boolean updateEncoding(int hash, long oldEncoding, long newEncoding, long mask)
           
 Lock writeLock()
           
 
Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, isEmpty, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsValue, equals, hashCode, isEmpty, putAll, values
 

Field Detail

ENTRY_SIZE

protected static final int ENTRY_SIZE
Size of a table entry in primitive int units

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

size

protected volatile int size

modCount

protected volatile int modCount

reprobeLimit

protected int reprobeLimit

hashtable

protected volatile IntBuffer hashtable
The current hash-table.

A list of: int[] {status, hashCode, encoding-high, encoding-low}


hashTablePage

protected volatile Page hashTablePage

removedSlots

protected volatile int removedSlots
Constructor Detail

OffHeapHashMap

public OffHeapHashMap(PageSource source,
                      StorageEngine<? super K,? super V> storageEngine)
Construct an instance using a custom BufferSource for the hashtable.

Parameters:
source - source for the hashtable allocations
storageEngine - 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 custom BufferSource for the hashtable and a custom initial table size.

Parameters:
source - source for the hashtable allocations
storageEngine - engine used to encode the keys and values
tableSize - 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()
Specified by:
size in interface Map<K,V>
Overrides:
size in class AbstractMap<K,V>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey in class AbstractMap<K,V>

get

public V get(Object key)
Specified by:
get in interface Map<K,V>
Overrides:
get in class AbstractMap<K,V>

getEncodingForHashAndBinary

public Long getEncodingForHashAndBinary(int hash,
                                        ByteBuffer binaryKey)
Specified by:
getEncodingForHashAndBinary in interface StorageEngine.Owner

installMappingForHashAndEncoding

public long installMappingForHashAndEncoding(int pojoHash,
                                             ByteBuffer offheapBinaryKey,
                                             ByteBuffer offheapBinaryValue,
                                             int metadata)
Specified by:
installMappingForHashAndEncoding in interface StorageEngine.Owner

getMetadata

public Integer getMetadata(Object key,
                           int mask)

getAndSetMetadata

public Integer getAndSetMetadata(Object key,
                                 int mask,
                                 int values)

getValueAndSetMetadata

public V getValueAndSetMetadata(Object key,
                                int mask,
                                int values)

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>
Overrides:
put in class AbstractMap<K,V>

put

public V put(K key,
             V value,
             int metadata)

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 associated
value - 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

public V fill(K key,
              V value,
              int metadata)

fill

protected final V fill(K key,
                       V value,
                       int hash,
                       int[] newEntry,
                       int metadata)

remove

public V remove(Object key)
Specified by:
remove in interface Map<K,V>
Overrides:
remove in class AbstractMap<K,V>

removeAllWithHash

public Map<K,V> removeAllWithHash(int hash)
Description copied from interface: HashingMap
Remove all keys having a specific hashcode.

Specified by:
removeAllWithHash in interface HashingMap<K,V>
Parameters:
hash - the hashcode of the keys to be removed.
Returns:
a Map containing the removed mappings.

removeNoReturn

public boolean removeNoReturn(Object key)

clear

public void clear()
Specified by:
clear in interface Map<K,V>
Overrides:
clear in class AbstractMap<K,V>

destroy

public void destroy()

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>
Specified by:
entrySet in class AbstractMap<K,V>

encodingSet

public Set<Long> encodingSet()
Specified by:
encodingSet in interface StorageEngine.Owner

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>
Overrides:
keySet in class AbstractMap<K,V>

isPresent

protected static boolean isPresent(IntBuffer entry)

isAvailable

protected static boolean isAvailable(IntBuffer entry)

isTerminating

protected static boolean isTerminating(IntBuffer entry)

isTerminating

protected static boolean isTerminating(int entryStatus)

isRemoved

protected static boolean isRemoved(IntBuffer entry)

isRemoved

protected static boolean isRemoved(int entryStatus)

readLong

protected static long readLong(int[] array,
                               int offset)

readLong

protected static long readLong(IntBuffer entry,
                               int offset)

indexFor

protected int indexFor(int hash)

indexFor

protected static int indexFor(int hash,
                              IntBuffer table)

tryIncreaseReprobe

protected boolean tryIncreaseReprobe()

shrinkTable

protected void shrinkTable()

spread

protected static int spread(int hash)

freePendingTables

protected void freePendingTables()

removeMapping

protected boolean removeMapping(Object o)

evict

public boolean evict(int index,
                     boolean shrink)
Specified by:
evict in interface StorageEngine.Owner

removeAtTableOffset

protected void removeAtTableOffset(int offset,
                                   boolean shrink)

getAtTableOffset

protected V getAtTableOffset(int offset)

getEntryAtTableOffset

protected Map.Entry<K,V> getEntryAtTableOffset(int offset)

getSlotForHashAndEncoding

public Integer getSlotForHashAndEncoding(int hash,
                                         long encoding,
                                         long mask)
Specified by:
getSlotForHashAndEncoding in interface StorageEngine.Owner

updateEncoding

public boolean updateEncoding(int hash,
                              long oldEncoding,
                              long newEncoding,
                              long mask)
Specified by:
updateEncoding in interface StorageEngine.Owner

added

protected void added(IntBuffer entry)

hit

protected void hit(IntBuffer entry)

removed

protected void removed(IntBuffer entry)

updated

protected void updated(IntBuffer entry)

tableExpansionFailure

protected void tableExpansionFailure(int start,
                                     int length)

storageEngineFailure

protected void storageEngineFailure(Object failure)

getSize

public long getSize()
Specified by:
getSize in interface MapInternals
Specified by:
getSize in interface StorageEngine.Owner

getTableCapacity

public long getTableCapacity()
Specified by:
getTableCapacity in interface MapInternals

getUsedSlotCount

public long getUsedSlotCount()
Specified by:
getUsedSlotCount in interface MapInternals

getRemovedSlotCount

public long getRemovedSlotCount()
Specified by:
getRemovedSlotCount in interface MapInternals

getReprobeLength

public int getReprobeLength()
Specified by:
getReprobeLength in interface MapInternals

getAllocatedMemory

public long getAllocatedMemory()
Specified by:
getAllocatedMemory in interface MapInternals

getOccupiedMemory

public long getOccupiedMemory()
Specified by:
getOccupiedMemory in interface MapInternals

getVitalMemory

public long getVitalMemory()
Specified by:
getVitalMemory in interface MapInternals

getDataAllocatedMemory

public long getDataAllocatedMemory()
Specified by:
getDataAllocatedMemory in interface MapInternals

getDataOccupiedMemory

public long getDataOccupiedMemory()
Specified by:
getDataOccupiedMemory in interface MapInternals

getDataVitalMemory

public long getDataVitalMemory()
Specified by:
getDataVitalMemory in interface MapInternals

getDataSize

public long getDataSize()
Specified by:
getDataSize in interface MapInternals

isThiefForTableAllocations

public boolean isThiefForTableAllocations()
Specified by:
isThiefForTableAllocations in interface StorageEngine.Owner

readLock

public Lock readLock()
Specified by:
readLock in interface ReadWriteLock

writeLock

public Lock writeLock()
Specified by:
writeLock in interface ReadWriteLock

getStorageEngine

public StorageEngine<? super K,? super V> 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)


Copyright © 2016. All Rights Reserved.