K
- key typeV
- value typepublic abstract class NamedMap<K,V>
extends java.lang.Object
implements java.util.concurrent.ConcurrentMap<K,V>
An implementation of the ConcurrentMap
which uses ScaleOut StateServer to store keys
and values. The map contents are coherent across the in-memory data grid.
The NamedMap class allows you to efficiently store large numbers of keys and values (typically millions) in the distributed in-memory data grid in such a way that they can be analyzed in bulk very efficiently.
Features:
To efficiently put or remove a large number of keys, use a BulkLoader
view
of the map which can be obtained by calling the getBulkLoader()
method. This way, multiple map operations
are combined into chunks which provides higher overall bandwidth. putAll(java.util.Map)
will
provide the same performance gain if the keys and values are precomputed and put into an intermediate map.
To maximize access performance, the NamedMap maintains an internal client-side cache that contains deserialized versions of the most recently accessed objects. When reading objects, the client-side cache reduces access response time by eliminating data motion and deserialization overhead.
A configurable number of recently accessed values can be stored in the client application's memory using the client-side cache.
On subsequent reads, cached values for keys are returned if they are not older than what is allowed by a configurable
coherency interval (NamedMap.setCoherencyIntervalMilliseconds(int)
). A coherency interval of 0 disables the
client-side cache and causes read operations to directly access the StateServer service.
The client-side cache size can be configured by calling the setClientCacheSize(int)
function with a new value. The default size is
10,000 entries.
If the client cache is enabled, object changes will be visible to all threads using the same NamedMap instance. The client cache is an in-process reference cache, so, if the client cache is enabled, the application may need to make deep copies of objects in the map if the objects will be simultaneously modified by multiple threads. Deep copying the object will prevent the client-side cache from being corrupted.
By default, the client cache is turned off (coherency interval is 0).
Parallel Method Invocation (PMI)
The ScaleOut StateServer service hosts a powerful parallel execution engine that allows you to efficiently analyze
all of the objects in a NamedMap by utilizing all of the hosts in your data grid. Each host simultaneously performs
parallel operations on its local subset of keys and values, which boosts performance by avoiding the movement of data
across the network. An invocation is initiated by calling the invoke(NamedMapInvokable, Object)
.
Prior to calling the Invoke method, a Parallel Method Invocation (PMI) operation must be defined by subclassing
the NamedMapInvokable
. PMI calls require an invocation grid (InvocationGrid)
to be assigned to the named map via setInvocationGrid(com.scaleoutsoftware.soss.client.InvocationGrid)
Queries are parallel operations which run simultaneously on all hosts in the data grid and return a list of matching keys.
To query a map, use executeParallelQuery(com.scaleoutsoftware.soss.client.map.QueryCondition)
with a
QueryCondition
implementation as a parameter. If this query condition
argument is null then all of the map's keys will be returned.
Query operations require an invocation grid (InvocationGrid
) to be assigned
to the named map via setInvocationGrid(com.scaleoutsoftware.soss.client.InvocationGrid)
Custom serialization can be used to efficiently store keys and values in memory. To use a custom serializer, the
user must subclass CustomSerializer
and implement the serialize and deserialize
methods. The custom serializer class can then be provided to the NamedMap factory method:
NamedMapFactory.getMap(String, com.scaleoutsoftware.soss.client.CustomSerializer, com.scaleoutsoftware.soss.client.CustomSerializer)
.
Each instance of the client application across the grid should have the same custom serializers assigned to the map to prevent serialization exceptions.
For Parallel Method Invocation (PMI) operations, if a PMI parameter or result object requires custom serialization,
then it can be set by implementing the NamedMapInvokable
.getParameterCustomSerializer()
and/or NamedMapInvokable
.getResultCustomSerializer() methods.
Object.equals(Object)
method of the objects.put(Object, Object)
and remove(Object)
always return null
in this
implementation to optimize performance by reducing the number of calls to the ScaleOut StateServer service.
These methods can be configured to return the previous value by setting the
setReturnBehavior(ReturnBehavior)
property to ReturnBehavior.STANDARD
.
entrySet()
and values()
are not supported by this implementation because they require moving
all map data to one host, which is extremely inefficient in a distributed environment.
containsValue(Object)
requires an invocation grid (InvocationGrid
)
to be specified for the map(setInvocationGrid(com.scaleoutsoftware.soss.client.InvocationGrid)
).
keySet()
, because it requires moving all key data to the calling host,
leading to higher memory and network utilization.
Constructor and Description |
---|
NamedMap() |
Modifier and Type | Method and Description |
---|---|
abstract void |
clear()
Removes all entries from the map.
|
abstract boolean |
containsKey(java.lang.Object key)
Returns
true if this map contains a mapping for the specified
key. |
abstract boolean |
containsValue(java.lang.Object value)
Returns
true if this map contains one or more keys which map to the
specified value. |
abstract java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
This implementation does not support this method.
|
abstract java.lang.Iterable<K> |
executeParallelQuery(QueryCondition<K,V> condition)
Runs a parallel query on the map contents with the specified
QueryCondition . |
abstract java.lang.Iterable<K> |
executeParallelQuery(QueryCondition<K,V> condition,
long timeout,
java.util.concurrent.TimeUnit unit)
Runs a parallel query on the map contents with specified
QueryCondition . |
abstract V |
get(java.lang.Object key)
Returns the value mapped by the specified key,
or
null if this map contains no mapping for the key. |
abstract AvailabilityMode |
getAvailabilityMode()
Gets whether object replication is enabled for the named map.
|
abstract BulkLoader<K,V> |
getBulkLoader()
Gets the
BulkLoader view of the NamedMap which can be used to efficiently put
a large number of keys in the map. |
abstract int |
getCoherencyIntervalMilliseconds()
Returns the coherency interval in milliseconds.
|
abstract InvocationGrid |
getInvocationGrid()
Gets the currently assigned
InvocationGrid . |
abstract CustomSerializer<K> |
getKeySerializer()
Gets the
CustomSerializer used to serialize and deserialize
keys. |
abstract java.lang.Iterable<K> |
getKeysForValue(V value)
Searches for a specific value in the map and returns all the keys
this value is associated with.
|
abstract int |
getMapId()
Gets the map ID.
|
abstract java.lang.String |
getMapName()
Gets the map name.
|
abstract int |
getMaxServerRetries()
Returns the number of server retries when a race condition between client requests occurs.
|
abstract ReturnBehavior |
getReturnBehavior()
Gets the return behavior setting for
put(Object, Object)
and remove(Object) methods. |
SerializationMode |
getSerializationMode()
Gets the serialization mode associated with this named map.
|
abstract CustomSerializer<V> |
getValueSerializer()
Gets the
CustomSerializer used to serialize and deserialize
values. |
abstract boolean |
insert(K key,
V value)
Inserts the key and value into the map if the key does not
already exist in the map.
|
abstract <RESULT,PARAMETER> |
invoke(NamedMapInvokable<K,V,PARAMETER,RESULT> invokable,
PARAMETER parameter)
Runs a parallel method invocation (
NamedMapInvokable ) on the map contents and returns the result. |
abstract <RESULT,PARAMETER> |
invoke(NamedMapInvokable<K,V,PARAMETER,RESULT> invokable,
PARAMETER parameter,
long timeout,
java.util.concurrent.TimeUnit unit)
Runs a parallel method invocation (
NamedMapInvokable ) on the map contents and returns the result. |
abstract boolean |
isEmpty()
Returns
true if this map contains no key-value mappings. |
abstract java.util.Set<K> |
keySet()
Returns the set of keys in the map.
|
abstract V |
put(K key,
V value)
Associates the specified value with the specified key in this map.
|
abstract void |
putAll(java.util.Map<? extends K,? extends V> map)
Efficiently puts contents of the specified map into the named map.
|
abstract V |
putIfAbsent(K key,
V value)
Puts the value in the map if the key is absent, otherwise returns
the existing value.
|
abstract V |
remove(java.lang.Object key)
Removes the mapping for a key from this map if it is present.
|
abstract boolean |
remove(java.lang.Object key,
java.lang.Object expectedValue)
Removes the entry from the map only if currently mapped to a given value.
|
abstract V |
replace(K key,
V value)
Replaces the entry for a key only if currently mapped to a value.
|
abstract boolean |
replace(K key,
V oldValue,
V newValue)
Replaces the entry for a key only if currently mapped to a given value.
|
abstract void |
setAvailabilityMode(AvailabilityMode availabilityMode)
Sets the @link {AvailabilityMode} which controls whether the in-memory data grid should create
object replicas of the named map contents on multiple hosts.
|
abstract void |
setClientCacheSize(int cacheSize)
Sets the maximum number of entries (i.e., key-value mappings) in the client cache.
|
abstract void |
setCoherencyIntervalMilliseconds(int coherencyIntervalMilliseconds)
Sets the coherency interval, i.e., the maximum allowed time
between the last server read request and a
get(Object) request
before the client will retrieve the object from the server
instead of the client cache (if present). |
abstract void |
setInvocationGrid(InvocationGrid invocationGrid)
Assigns the
InvocationGrid to the map. |
abstract void |
setMaxServerRetries(int maxServerRetries)
Sets the maximum number of server request retries when a race condition between client requests occur.
|
abstract void |
setReturnBehavior(ReturnBehavior behavior)
Sets whether
put(Object, Object) and remove(Object) methods
should return the previous value associated with the key. |
void |
setSerializationMode(SerializationMode sm)
Sets the serialization mode to use.
|
abstract int |
size()
Returns the number of key-value mappings in this map.
|
abstract boolean |
update(K key,
V value)
Updates the key with the new value if the key already exists in the map.
|
abstract java.util.Collection<V> |
values()
This implementation does not support this method.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
public abstract V putIfAbsent(K key, V value)
Puts the value in the map if the key is absent, otherwise returns the existing value.
Note: if the previous value is not required, use
insert(Object, Object)
for better performance.
putIfAbsent
in interface java.util.concurrent.ConcurrentMap<K,V>
putIfAbsent
in interface java.util.Map<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keynull
if there was no mapping for the key.java.lang.NullPointerException
- if the specified key or value is nulljava.lang.RuntimeException
- if a communication error occurredpublic abstract boolean remove(java.lang.Object key, java.lang.Object expectedValue)
remove
in interface java.util.concurrent.ConcurrentMap<K,V>
remove
in interface java.util.Map<K,V>
key
- key with which the specified value is associatedexpectedValue
- value expected to be associated with the specified keytrue
if the value was removedjava.lang.NullPointerException
- if the specified key or value is nulljava.lang.RuntimeException
- if a communication error occurredpublic abstract boolean replace(K key, V oldValue, V newValue)
replace
in interface java.util.concurrent.ConcurrentMap<K,V>
replace
in interface java.util.Map<K,V>
key
- key with which the specified value is associatedoldValue
- value expected to be associated with the specified keynewValue
- value to be associated with the specified keytrue
if the value was replacedjava.lang.NullPointerException
- if the specified key or value is nulljava.lang.RuntimeException
- if a communication error occurredpublic abstract V replace(K key, V value)
replace
in interface java.util.concurrent.ConcurrentMap<K,V>
replace
in interface java.util.Map<K,V>
key
- key with which the specified value is associatedvalue
- value to be associated with the specified keynull
if there was no mapping for the key.java.lang.NullPointerException
- if the specified key or value is nulljava.lang.RuntimeException
- if a communication error occurredpublic abstract boolean containsKey(java.lang.Object key)
Returns true
if this map contains a mapping for the specified
key.
Note: keys are compared by a byte-wise comparison of their serialized
representations, not by Object.equals(Object)
.
containsKey
in interface java.util.Map<K,V>
key
- key whose presence in this map is to be testedtrue
if this map contains a mapping for the specified
keyjava.lang.NullPointerException
- if the specified key is nulljava.lang.RuntimeException
- if a communication error occurredpublic abstract V get(java.lang.Object key)
Returns the value mapped by the specified key,
or null
if this map contains no mapping for the key.
The returned value is an application-local clone object (i.e., changes made to this value do not affect the globally available value stored in the map until the map is updated by a put operation). If the client cache is enabled, object changes will be visible to all threads using the same cached object (i.e., the client cache is a reference cache). For that reason, if the client cache is enabled, the application should make a deep copy of the value before making modifications to avoid race conditions.
If the client cache size(setClientCacheSize(int)
)
and coherency interval(setCoherencyIntervalMilliseconds(int)
)
are greater than zero, client cached value may be returned if
it is not older than the coherency interval.
get
in interface java.util.Map<K,V>
key
- the key whose associated value is to be returnednull
if this map contains no mapping for the keyjava.lang.NullPointerException
- if the specified key is nulljava.lang.RuntimeException
- if a communication error occurredpublic abstract V put(K key, V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced by the specified value.
Note: For optimal performance, this implementation this method
always returns null
by default. To return the previous
value, set setReturnBehavior(ReturnBehavior)
to
ReturnBehavior.STANDARD
.
put
in interface java.util.Map<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keykey
, or
null
if there was no mapping for key
or setReturnBehavior(ReturnBehavior)
is set to
ReturnBehavior.RETURN_NULL
.java.lang.NullPointerException
- if the specified key or value is nulljava.lang.RuntimeException
- if a communication error occurredpublic abstract V remove(java.lang.Object key)
Removes the mapping for a key from this map if it is present.
Note: For optimal performance, this implementation this method
always returns null
by default. To return the previous
value, set setReturnBehavior(ReturnBehavior)
to
ReturnBehavior.STANDARD
.
remove
in interface java.util.Map<K,V>
key
- key whose mapping is to be removed from the mapkey
, or
null
if there was no mapping for key
or setReturnBehavior(ReturnBehavior)
is set to
ReturnBehavior.RETURN_NULL
.java.lang.NullPointerException
- if the specified key is nulljava.lang.RuntimeException
- if a communication error occurredpublic abstract void clear()
public abstract boolean insert(K key, V value)
key
- key to insertvalue
- value for the keytrue
if insert succeeded, false
if the key already exists in the mapjava.lang.RuntimeException
- if a communication error occurredpublic abstract boolean update(K key, V value)
key
- key to updatevalue
- new value for the keytrue
if update succeeded, false
if the key is absent in the mapjava.lang.RuntimeException
- if a communication error occurredpublic abstract void putAll(java.util.Map<? extends K,? extends V> map)
put(Object, Object)
when updating the map with a large
number of keys and values. If the named map already contains a mapping for a key,
its value will be replaced with the value in the input map.public abstract java.util.Set<K> keySet()
Returns the set of keys in the map.
This method should be used with caution since it requires copying all map keys
to a single host. Parallel method invocations(invoke(NamedMapInvokable, Object)
)
are the preferred way to iterate through map contents if the
size of the map is significant.
public abstract java.util.Collection<V> values()
public abstract java.util.Set<java.util.Map.Entry<K,V>> entrySet()
public abstract int size()
public abstract boolean isEmpty()
true
if this map contains no key-value mappings.public abstract InvocationGrid getInvocationGrid()
InvocationGrid
.public abstract void setInvocationGrid(InvocationGrid invocationGrid)
InvocationGrid
to the map. The invocation grid
is used to perform queries and parallel invocations across all nodes
in the data grid. To construct an invocation grid, use
InvocationGridBuilder
.invocationGrid
- invocation grid to assign to this mappublic abstract <RESULT,PARAMETER> RESULT invoke(NamedMapInvokable<K,V,PARAMETER,RESULT> invokable, PARAMETER parameter) throws InvokeException
Runs a parallel method invocation (NamedMapInvokable
) on the map contents and returns the result.
Note: This method requires that an invocation grid is assigned
to this map (setInvocationGrid(com.scaleoutsoftware.soss.client.InvocationGrid)
).
RESULT
- result typePARAMETER
- invocation parameter typeinvokable
- parallel operation to run.parameter
- invocation parameterInvokeException
- if exception occurred during parallel method invocationjava.lang.RuntimeException
- if a communication error occurredpublic abstract <RESULT,PARAMETER> RESULT invoke(NamedMapInvokable<K,V,PARAMETER,RESULT> invokable, PARAMETER parameter, long timeout, java.util.concurrent.TimeUnit unit) throws InvokeException
Runs a parallel method invocation (NamedMapInvokable
) on the map contents and returns the result.
Note: This method requires that an invocation grid is assigned
to this map (setInvocationGrid(com.scaleoutsoftware.soss.client.InvocationGrid)
).
RESULT
- result typePARAMETER
- invocation parameter typeinvokable
- parallel operation to run.parameter
- invocation parametertimeout
- invocation timeoutunit
- time unit for invocation timeoutInvokeException
- if exception occurred during parallel method invocationjava.lang.RuntimeException
- if a communication error occurredpublic abstract java.lang.Iterable<K> executeParallelQuery(QueryCondition<K,V> condition) throws InvokeException
Runs a parallel query on the map contents with the specified QueryCondition
.
Note: This method requires that an invocation grid is assigned
to this map (setInvocationGrid(com.scaleoutsoftware.soss.client.InvocationGrid)
).
condition
- condition used to select entriesIterable
collection of all matching keysInvokeException
- if exception occurred during parallel operationjava.lang.RuntimeException
- if a communication error occurredpublic abstract java.lang.Iterable<K> executeParallelQuery(QueryCondition<K,V> condition, long timeout, java.util.concurrent.TimeUnit unit) throws InvokeException
Runs a parallel query on the map contents with specified QueryCondition
.
Note: This method requires that an invocation grid is assigned
to this map (setInvocationGrid(com.scaleoutsoftware.soss.client.InvocationGrid)
).
condition
- condition used to select entriestimeout
- maximum query run timeunit
- time units for the query timeoutIterable
collection of all matching keysInvokeException
- if exception occurred during parallel operationjava.lang.RuntimeException
- if a communication error occurredpublic abstract java.lang.Iterable<K> getKeysForValue(V value) throws InvokeException
Searches for a specific value in the map and returns all the keys this value is associated with.
Note: This method requires that an invocation grid is assigned
to this map (setInvocationGrid(com.scaleoutsoftware.soss.client.InvocationGrid)
).
value
- value to search for in the mapIterable
collection of all keys which the provided value is associated withjava.lang.NullPointerException
- if the specified value is nullInvokeException
- if exception occurred during parallel operationjava.lang.RuntimeException
- if a communication error occurredpublic abstract boolean containsValue(java.lang.Object value)
Returns true
if this map contains one or more keys which map to the
specified value.
Note: This method requires that an invocation grid is assigned
to this map (setInvocationGrid(com.scaleoutsoftware.soss.client.InvocationGrid)
).
containsValue
in interface java.util.Map<K,V>
value
- value whose presence in this map is to be testedtrue
if this map contains one or more keys which map to the
specified value, otherwise false
java.lang.NullPointerException
- if the specified value is nulljava.lang.RuntimeException
- if a communication error occurredpublic abstract BulkLoader<K,V> getBulkLoader()
BulkLoader
view of the NamedMap
which can be used to efficiently put
a large number of keys in the map.public abstract ReturnBehavior getReturnBehavior()
put(Object, Object)
and remove(Object)
methods.ReturnBehavior
settingpublic abstract void setReturnBehavior(ReturnBehavior behavior)
put(Object, Object)
and remove(Object)
methods
should return the previous value associated with the key.behavior
- the return behaviorpublic abstract int getMaxServerRetries()
Returns the number of server retries when a race condition between client requests occurs.
The maxServerRetries
property is used when server requests lose a race with another server request that attempts
to modify the same object. This race condition can occur in the put
and putIfAbsent
methods, and both the remove
methods. The number of server request retries in these methods are
limited by this property, and a RuntimeException
is thrown if the race is not resolved after that
number of retries. maxServerRetries is set to a reasonable default value of 1000 retries, and under normal
operation does not need to be changed.
public abstract void setMaxServerRetries(int maxServerRetries)
Sets the maximum number of server request retries when a race condition between client requests occur.
The maxServerRetries
property is used when server requests lose a race with another server request that attempts
to modify the same object. This race condition can occur in the put
and putIfAbsent
methods, and both the remove
methods. The number of server request retries in these methods are
limited by this property, and a RuntimeException
is thrown if the race is not resolved after that
number of retries. maxServerRetries is set to a reasonable default value of 1000 retries, and under normal
operation does not need to be changed.
maxServerRetries
can be increased to a bigger value (e.g. 10000) in case of larger
number of concurrent threads accessing the object, to allow race
conditions to be successfully resolved.
maxServerRetries
- the number of server retriespublic abstract int getCoherencyIntervalMilliseconds()
public abstract void setCoherencyIntervalMilliseconds(int coherencyIntervalMilliseconds)
Sets the coherency interval, i.e., the maximum allowed time
between the last server read request and a get(Object)
request
before the client will retrieve the object from the server
instead of the client cache (if present).
A coherency interval of 0 means that the values are never served
out of the client cache.
Coherency interval is set to 0 by default, so get(Object)
always retrieves latest value from the server.
coherencyIntervalMilliseconds
- coherency interval in millisecondspublic abstract void setClientCacheSize(int cacheSize)
cacheSize
- number of entries (i.e., key-value mappings) in the client cachepublic abstract CustomSerializer<K> getKeySerializer()
Gets the CustomSerializer
used to serialize and deserialize
keys.
NamedMap
with
NamedMapFactory.getMap(String, com.scaleoutsoftware.soss.client.CustomSerializer, com.scaleoutsoftware.soss.client.CustomSerializer)
.public abstract CustomSerializer<V> getValueSerializer()
Gets the CustomSerializer
used to serialize and deserialize
values.
NamedMap
with
NamedMapFactory.getMap(String, com.scaleoutsoftware.soss.client.CustomSerializer, com.scaleoutsoftware.soss.client.CustomSerializer)
.public abstract AvailabilityMode getAvailabilityMode()
Gets whether object replication is enabled for the named map.
Enabling object replication provides fault tolerance, i.e., it ensures that no data is lost
in case of host failure at the expense of increased memory and network utilization.
If replication is enabled, the number of replicas
created is controlled by the max_replicas
parameter in the ScaleOut StateServer
configuration file (soss_params.txt).
true
if replication is enabled, false
otherwisepublic abstract void setAvailabilityMode(AvailabilityMode availabilityMode)
Sets the @link {AvailabilityMode} which controls whether the in-memory data grid should create object replicas of the named map contents on multiple hosts.
Enabling object replication provides fault tolerance, i.e., it ensures that no data is lost
in case of host failure at the expense of increased memory and network utilization.
If replication is enabled, the number of replicas
created is controlled by the max_replicas
parameter in the ScaleOut StateServer
configuration.
availabilityMode
- availability modepublic abstract int getMapId()
public abstract java.lang.String getMapName()
public void setSerializationMode(SerializationMode sm)
sm
- the SM to usepublic SerializationMode getSerializationMode()