public class HTreeMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>, Bind.MapWithModificationListener<K,V>, Closeable, Serializable
Thread safe concurrent HashMap
This map uses full 32bit hash from beginning, There is no initial load factor and rehash. Technically it is not hash table, but hash tree with nodes expanding when they become full.
This map is suitable for number of records 1e9 and over. Larger number of records will increase hash collisions and performance will degrade linearly with number of records (separate chaining).
Concurrent scalability is achieved by splitting HashMap into 16 segments, each with separate lock.
Very similar to ConcurrentHashMap
| Modifier and Type | Class and Description |
|---|---|
protected static class |
HTreeMap.ExpireLinkNode |
class |
HTreeMap.KeySet |
protected static class |
HTreeMap.LinkedNode<K,V>
node which holds key-value pair
|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>| Modifier and Type | Field and Description |
|---|---|
protected static int |
BUCKET_OVERFLOW |
protected boolean |
closeEngine |
protected boolean |
closeExecutor
Indicates if this collection collection was not made by DB by user.
|
protected Lock |
consistencyLock |
protected long[] |
counterRecids |
protected static Serializer<Object> |
DIR_SERIALIZER |
protected static int |
DIV8 |
protected Engine[] |
engines |
protected ScheduledExecutorService |
executor |
protected long |
expire |
protected long |
expireAccess |
protected boolean |
expireAccessFlag |
protected boolean |
expireFlag |
protected long[] |
expireHeads |
protected long |
expireLastTick |
protected long |
expireMaxSize |
protected boolean |
expireMaxSizeFlag |
protected boolean |
expireSingleThreadFlag |
protected long |
expireStoreSize |
protected long[] |
expireStoreSizes |
protected long[] |
expireStoreSizesCompact |
protected long[] |
expireTails |
protected long |
expireTick |
protected long |
expireTimeStart |
protected int |
hashSalt
Salt added to hash before rehashing, so it is harder to trigger hash collision attack.
|
protected boolean |
hasValues
is this a Map or Set? if false, entries do not have values, only keys are allowed
|
protected Serializer<K> |
keySerializer |
protected Serializer<HTreeMap.LinkedNode<K,V>> |
LN_SERIALIZER |
protected static Logger |
LOG |
protected static int |
MOD8 |
protected Bind.MapListener<K,V>[] |
modListeners |
protected Object |
modListenersLock |
protected ReentrantReadWriteLock[] |
segmentLocks |
protected long[] |
segmentRecids
list of segments, this is immutable
|
protected Fun.Function1<V,K> |
valueCreator |
protected Serializer<V> |
valueSerializer |
| Constructor and Description |
|---|
HTreeMap(Engine[] engines,
boolean closeEngine,
long[] counterRecids,
int hashSalt,
long[] segmentRecids,
Serializer<K> keySerializer,
Serializer<V> valueSerializer,
long expireTimeStart,
long expire,
long expireAccess,
long expireMaxSize,
long expireStoreSize,
long expireTick,
long[] expireHeads,
long[] expireTails,
Fun.Function1<V,K> valueCreator,
ScheduledExecutorService executor,
long executorPeriod,
boolean closeExecutor,
Lock consistencyLock)
Opens HTreeMap
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear() |
void |
close() |
boolean |
containsKey(Object o) |
boolean |
containsValue(Object value) |
protected void |
counter(int segment,
Engine engine,
int plus) |
protected static long |
dirGet(Object dir,
int pos) |
protected long |
dirGetSlot(Object dir,
int slot) |
protected static boolean |
dirIsEmpty(Object dir) |
protected static int |
dirLen(Object dir) |
protected static int |
dirOffsetFromSlot(int[] dir,
int slot)
converts hash slot into actual offset in dir array, using bitmap
|
protected static int |
dirOffsetFromSlot(long[] dir,
int slot)
converts hash slot into actual offset in dir array, using bitmap
|
protected static int |
dirOffsetFromSlot(Object dir,
int slot) |
protected static Object |
dirPut(Object dir,
int slot,
long newRecid) |
protected static Object |
dirRemove(Object dir,
int slot) |
protected static int |
dirStart(Object dir) |
Set<Map.Entry<K,V>> |
entrySet() |
protected void |
expireCheckSegment(int segment) |
protected void |
expireLinkAdd(int segment,
long expireNodeRecid,
long keyRecid,
int hash) |
protected void |
expireLinkBump(int segment,
long nodeRecid,
boolean access) |
protected HTreeMap.ExpireLinkNode |
expireLinkRemove(int segment,
long nodeRecid) |
protected HTreeMap.ExpireLinkNode |
expireLinkRemoveLast(int segment) |
protected void |
expirePurge() |
protected long |
expirePurgeSegment(int seg,
long removePerSegment) |
V |
get(Object o) |
Engine |
getEngine() |
protected HTreeMap.LinkedNode<K,V> |
getInner(Object o,
int h,
int segment) |
long |
getMaxExpireTime()
Returns maximal (newest) expiration timestamp
|
long |
getMinExpireTime()
Returns minimal (oldest) expiration timestamp
|
V |
getPeek(Object key)
Return given value, without updating cache statistics if
expireAccess() is true
It also does not use valueCreator if value is not found (always returns null if not found) |
protected int |
hash(Object key) |
boolean |
isEmpty() |
Set<K> |
keySet() |
long |
mappingCount() |
void |
modificationListenerAdd(Bind.MapListener<K,V> listener)
Add new modification listener notified when Map has been updated
|
void |
modificationListenerRemove(Bind.MapListener<K,V> listener)
Remove registered notification listener
|
protected void |
notify(K key,
V oldValue,
V newValue) |
protected static long[] |
preallocateSegments(Engine[] engines) |
V |
put(K key,
V value) |
V |
putIfAbsent(K key,
V value) |
V |
remove(Object key) |
boolean |
remove(Object key,
Object value) |
protected V |
removeInternal(Object key,
int segment,
int h,
boolean removeExpire) |
V |
replace(K key,
V value) |
boolean |
replace(K key,
V oldValue,
V newValue) |
int |
size() |
long |
sizeLong() |
Map<K,V> |
snapshot()
Make readonly snapshot view of current Map.
|
Collection<V> |
values() |
protected static final Logger LOG
protected static final int BUCKET_OVERFLOW
protected static final int DIV8
protected static final int MOD8
protected final boolean hasValues
protected final int hashSalt
protected final long[] counterRecids
protected final Serializer<K> keySerializer
protected final Serializer<V> valueSerializer
protected final Engine[] engines
protected final boolean closeEngine
protected final boolean expireFlag
protected final boolean expireSingleThreadFlag
protected final long expireTimeStart
protected final long expire
protected final boolean expireAccessFlag
protected final long expireAccess
protected final long expireMaxSize
protected final long expireStoreSize
protected final long expireTick
protected final boolean expireMaxSizeFlag
protected final long[] expireHeads
protected final long[] expireTails
protected final long[] expireStoreSizes
protected final long[] expireStoreSizesCompact
protected final Fun.Function1<V,K> valueCreator
protected final boolean closeExecutor
protected final ScheduledExecutorService executor
protected final Lock consistencyLock
protected volatile long expireLastTick
protected final Serializer<HTreeMap.LinkedNode<K,V>> LN_SERIALIZER
protected static final Serializer<Object> DIR_SERIALIZER
protected final long[] segmentRecids
protected final ReentrantReadWriteLock[] segmentLocks
protected final Object modListenersLock
protected Bind.MapListener<K,V>[] modListeners
public HTreeMap(Engine[] engines, boolean closeEngine, long[] counterRecids, int hashSalt, long[] segmentRecids, Serializer<K> keySerializer, Serializer<V> valueSerializer, long expireTimeStart, long expire, long expireAccess, long expireMaxSize, long expireStoreSize, long expireTick, long[] expireHeads, long[] expireTails, Fun.Function1<V,K> valueCreator, ScheduledExecutorService executor, long executorPeriod, boolean closeExecutor, Lock consistencyLock)
protected static long[] preallocateSegments(Engine[] engines)
public boolean containsKey(Object o)
containsKey in interface Map<K,V>containsKey in class AbstractMap<K,V>public int size()
public long sizeLong()
sizeLong in interface Bind.MapWithModificationListener<K,V>public long mappingCount()
public boolean isEmpty()
public V getPeek(Object key)
expireAccess() is true
It also does not use valueCreator if value is not found (always returns null if not found)key - key to lookupprotected HTreeMap.LinkedNode<K,V> getInner(Object o, int h, int segment)
protected static boolean dirIsEmpty(Object dir)
protected static int dirLen(Object dir)
protected static int dirStart(Object dir)
protected static long dirGet(Object dir, int pos)
protected long dirGetSlot(Object dir, int slot)
protected static int dirOffsetFromSlot(Object dir, int slot)
protected static final int dirOffsetFromSlot(int[] dir,
int slot)
protected static final int dirOffsetFromSlot(long[] dir,
int slot)
protected void counter(int segment,
Engine engine,
int plus)
public void clear()
public boolean containsValue(Object value)
containsValue in interface Map<K,V>containsValue in class AbstractMap<K,V>public Collection<V> values()
protected int hash(Object key)
public V putIfAbsent(K key, V value)
putIfAbsent in interface ConcurrentMap<K,V>public boolean remove(Object key, Object value)
remove in interface ConcurrentMap<K,V>public boolean replace(K key, V oldValue, V newValue)
replace in interface ConcurrentMap<K,V>protected void expireLinkAdd(int segment,
long expireNodeRecid,
long keyRecid,
int hash)
protected void expireLinkBump(int segment,
long nodeRecid,
boolean access)
protected HTreeMap.ExpireLinkNode expireLinkRemoveLast(int segment)
protected HTreeMap.ExpireLinkNode expireLinkRemove(int segment, long nodeRecid)
public long getMaxExpireTime()
public long getMinExpireTime()
protected void expirePurge()
protected long expirePurgeSegment(int seg,
long removePerSegment)
protected void expireCheckSegment(int segment)
public Map<K,V> snapshot()
Make readonly snapshot view of current Map. Snapshot is immutable and not affected by modifications made by other threads. Useful if you need consistent view on Map.
Maintaining snapshot have some overhead, underlying Engine is closed after Map view is GCed. Please make sure to release reference to this Map view, so snapshot view can be garbage collected.
public void modificationListenerAdd(Bind.MapListener<K,V> listener)
Bind.MapWithModificationListenermodificationListenerAdd in interface Bind.MapWithModificationListener<K,V>listener - callback interface notified when map changespublic void modificationListenerRemove(Bind.MapListener<K,V> listener)
Bind.MapWithModificationListenermodificationListenerRemove in interface Bind.MapWithModificationListener<K,V>listener - callback interface notified when map changespublic Engine getEngine()
public void close()
close in interface Closeableclose in interface AutoCloseableCopyright © 2015. All Rights Reserved.