Class OffHeapConcurrentMap
- All Implemented Interfaces:
AutoCloseable
,ConcurrentMap<WrappedBytes,
,InternalCacheEntry<WrappedBytes, WrappedBytes>> Map<WrappedBytes,
,InternalCacheEntry<WrappedBytes, WrappedBytes>> PeekableTouchableMap<WrappedBytes,
WrappedBytes>
ConcurrentMap
implementation that stores the keys and values off the JVM heap in native heap. This map
does not permit null for key or values.
The key and value are limited to objects that implement the WrappedBytes
interface. Currently this map only allows
for implementations that always return a backing array via the WrappedBytes.getBytes()
method.
For reference here is a list of commonly used terms:
bucket
: Can store multiple entries (normally via a forward only list)memory lookup
: Stores an array of buckets - used primarily to lookup the location a key would belock region
: The number of lock regions is fixed, and each region hasbucket count / lock count
buckets.
This implementation provides constant-time performance for the basic
operations (get
, put
, remove
and compute
), assuming the hash function
disperses the elements properly among the buckets. Iteration over
collection views requires time proportional to the number of buckets plus its size (the number
of key-value mappings). This map always assumes a load factor of .75 that is not changeable.
A map must be started after creating to create the initial memory lookup, which is also store in the native heap. When the size of the map reaches the load factor, that is .75 times the capacity, the map will attempt to resize by increasing its internal memory lookup to have an array of buckets twice as big. Normal operations can still proceed during this, allowing for minimal downtime during a resize.
This map is created assuming some knowledge of expiration in the Infinispan system. Thus operations that do not
expose this information via its APIs are not supported. These methods are keySet
, containsKey
and
containsValue
.
This map guarantees consistency under concurrent read ands writes through a StripedLock
where each
ReadWriteLock
instance protects an equivalent region of buckets in the underlying
memory lookup. Read operations, that is ones that only acquire the read lock for their specific lock region, are
(get
and peek
). Iteration on a returned entrySet or value collection will acquire only a single
read lock at a time while inspecting a given lock region for a valid value. Write operations, ones that acquire the
write lock for the lock region, are (put
, remove
, replace
, compute
. A clear
will acquire all write locks when invoked. This allows the clear to also resize the map down to the initial size.
When this map is constructed it is also possible to provide an OffHeapConcurrentMap.EntryListener
that is invoked when various
operations are performed in the map. Note that the various modification callbacks MUST free the old address,
or else a memory leak will occur. Please see the various methods for clarification on these methods.
Since this map is based on holding references to memory that lives outside of the scope of the JVM garbage collector
users need to ensure they properly invoke the close()
when the map is no longer in use to properly free
all allocated native memory.
- Since:
- 9.4
- Author:
- wburns
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Listener interface that is notified when certain operations occur for various memory addresses. -
Field Summary
-
Constructor Summary
ConstructorDescriptionOffHeapConcurrentMap
(OffHeapMemoryAllocator allocator, OffHeapEntryFactory offHeapEntryFactory, OffHeapConcurrentMap.EntryListener listener) -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
void
close()
compute
(WrappedBytes key, BiFunction<? super WrappedBytes, ? super InternalCacheEntry<WrappedBytes, WrappedBytes>, ? extends InternalCacheEntry<WrappedBytes, WrappedBytes>> remappingFunction) boolean
containsKey
(Object key) boolean
containsValue
(Object value) entrySet()
boolean
isEmpty()
keySet()
Peaks at a value for the given key.put
(WrappedBytes key, InternalCacheEntry<WrappedBytes, WrappedBytes> value) void
putAll
(Map<? extends WrappedBytes, ? extends InternalCacheEntry<WrappedBytes, WrappedBytes>> m) putIfAbsent
(WrappedBytes key, InternalCacheEntry<WrappedBytes, WrappedBytes> value) void
putNoReturn
(WrappedBytes key, InternalCacheEntry<WrappedBytes, WrappedBytes> value) Same asMap.put(Object, Object)
except that the map is not required to return a value.boolean
replace
(WrappedBytes key, InternalCacheEntry<WrappedBytes, WrappedBytes> value) boolean
replace
(WrappedBytes key, InternalCacheEntry<WrappedBytes, WrappedBytes> oldValue, InternalCacheEntry<WrappedBytes, WrappedBytes> newValue) int
size()
void
touchAll
(long currentTimeMillis) Touches all entries in the map setting the recency timestamps for both expiration eviction appropriately.boolean
Touches the entry for the given key in this map.values()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.concurrent.ConcurrentMap
computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
-
Field Details
-
INITIAL_SIZE
public static final int INITIAL_SIZE- See Also:
-
-
Constructor Details
-
OffHeapConcurrentMap
public OffHeapConcurrentMap(OffHeapMemoryAllocator allocator, OffHeapEntryFactory offHeapEntryFactory, OffHeapConcurrentMap.EntryListener listener)
-
-
Method Details
-
touchKey
Description copied from interface:PeekableTouchableMap
Touches the entry for the given key in this map. This method will update any recency timestamps for both expiration or eviction as needed.- Specified by:
touchKey
in interfacePeekableTouchableMap<WrappedBytes,
WrappedBytes> - Parameters:
k
- key to touchcurrentTimeMillis
- the recency timestamp to set- Returns:
- whether the entry was touched or not
-
touchAll
public void touchAll(long currentTimeMillis) Description copied from interface:PeekableTouchableMap
Touches all entries in the map setting the recency timestamps for both expiration eviction appropriately.- Specified by:
touchAll
in interfacePeekableTouchableMap<WrappedBytes,
WrappedBytes> - Parameters:
currentTimeMillis
- the recency timestamp to set
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-
size
public int size()- Specified by:
size
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
compute
public InternalCacheEntry<WrappedBytes,WrappedBytes> compute(WrappedBytes key, BiFunction<? super WrappedBytes, ? super InternalCacheEntry<WrappedBytes, WrappedBytes>, ? extends InternalCacheEntry<WrappedBytes, WrappedBytes>> remappingFunction) - Specified by:
compute
in interfaceConcurrentMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>> - Specified by:
compute
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
containsKey
- Specified by:
containsKey
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
containsValue
- Specified by:
containsValue
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
get
- Specified by:
get
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
peek
Description copied from interface:PeekableTouchableMap
Peaks at a value for the given key. Note that this does not update any expiration or eviction information when this is performed on the map, unlike the get method.- Specified by:
peek
in interfacePeekableTouchableMap<WrappedBytes,
WrappedBytes> - Parameters:
key
- The key to find the value for- Returns:
- The value mapping to this key
-
putNoReturn
Description copied from interface:PeekableTouchableMap
Same asMap.put(Object, Object)
except that the map is not required to return a value. This can be useful when retrieving a previous value may incur additional costs.- Specified by:
putNoReturn
in interfacePeekableTouchableMap<WrappedBytes,
WrappedBytes> - Parameters:
key
- key to insert for the valuevalue
- the value to insert into this map
-
put
public InternalCacheEntry<WrappedBytes,WrappedBytes> put(WrappedBytes key, InternalCacheEntry<WrappedBytes, WrappedBytes> value) - Specified by:
put
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
remove
- Specified by:
remove
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
putAll
public void putAll(Map<? extends WrappedBytes, ? extends InternalCacheEntry<WrappedBytes, WrappedBytes>> m) - Specified by:
putAll
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
clear
public void clear()- Specified by:
clear
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
putIfAbsent
public InternalCacheEntry<WrappedBytes,WrappedBytes> putIfAbsent(WrappedBytes key, InternalCacheEntry<WrappedBytes, WrappedBytes> value) - Specified by:
putIfAbsent
in interfaceConcurrentMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>> - Specified by:
putIfAbsent
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
remove
- Specified by:
remove
in interfaceConcurrentMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>> - Specified by:
remove
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
replace
public boolean replace(WrappedBytes key, InternalCacheEntry<WrappedBytes, WrappedBytes> oldValue, InternalCacheEntry<WrappedBytes, WrappedBytes> newValue) - Specified by:
replace
in interfaceConcurrentMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>> - Specified by:
replace
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
replace
public InternalCacheEntry<WrappedBytes,WrappedBytes> replace(WrappedBytes key, InternalCacheEntry<WrappedBytes, WrappedBytes> value) - Specified by:
replace
in interfaceConcurrentMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>> - Specified by:
replace
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
keySet
- Specified by:
keySet
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
values
- Specified by:
values
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-
entrySet
- Specified by:
entrySet
in interfaceMap<WrappedBytes,
InternalCacheEntry<WrappedBytes, WrappedBytes>>
-