K
- key typeV
- value typepublic abstract class NamedMap<K,V> extends Object implements 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.
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).
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(Object key)
Returns
true if this map contains a mapping for the specified
key. |
abstract boolean |
containsValue(Object value)
Returns
true if this map contains one or more keys which map to the
specified value. |
abstract Set<Map.Entry<K,V>> |
entrySet()
This implementation does not support this method.
|
abstract Iterable<K> |
executeParallelQuery(QueryCondition<K,V> condition)
Runs a parallel query on the map contents with the specified
QueryCondition . |
abstract Iterable<K> |
executeParallelQuery(QueryCondition<K,V> condition,
long timeout,
TimeUnit unit)
Runs a parallel query on the map contents with specified
QueryCondition . |
abstract V |
get(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 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 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. |
com.scaleoutsoftware.soss.client.util.SerializationMode |
getSerializationMode() |
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,
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 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(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(Object key)
Removes the mapping for a key from this map if it is present.
|
abstract boolean |
remove(Object key,
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(com.scaleoutsoftware.soss.client.util.SerializationMode sm)
Set the serialization mode.
|
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 Collection<V> |
values()
This implementation does not support this method.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
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 ConcurrentMap<K,V>
putIfAbsent
in interface 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.NullPointerException
- if the specified key or value is nullRuntimeException
- if a communication error occurredpublic abstract boolean remove(Object key, Object expectedValue)
remove
in interface ConcurrentMap<K,V>
remove
in interface 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 removedNullPointerException
- if the specified key or value is nullRuntimeException
- if a communication error occurredpublic abstract boolean replace(K key, V oldValue, V newValue)
replace
in interface ConcurrentMap<K,V>
replace
in interface 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 replacedNullPointerException
- if the specified key or value is nullRuntimeException
- if a communication error occurredpublic abstract V replace(K key, V value)
replace
in interface ConcurrentMap<K,V>
replace
in interface 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.NullPointerException
- if the specified key or value is nullRuntimeException
- if a communication error occurredpublic abstract boolean containsKey(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 Map<K,V>
key
- key whose presence in this map is to be testedtrue
if this map contains a mapping for the specified
keyNullPointerException
- if the specified key is nullRuntimeException
- if a communication error occurredpublic abstract V get(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 Map<K,V>
key
- the key whose associated value is to be returnednull
if this map contains no mapping for the keyNullPointerException
- if the specified key is nullRuntimeException
- 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 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
.NullPointerException
- if the specified key or value is nullRuntimeException
- if a communication error occurredpublic abstract V remove(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 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
.NullPointerException
- if the specified key is nullRuntimeException
- if a communication error occurredpublic abstract void clear()
clear
in interface Map<K,V>
RuntimeException
- if a communication error occurredpublic 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 mapRuntimeException
- 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 mapRuntimeException
- if a communication error occurredpublic abstract void putAll(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.putAll
in interface Map<K,V>
map
- map containing keys and values to be put into the named mapRuntimeException
- if a communication error occurredpublic abstract 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 Collection<V> values()
values
in interface Map<K,V>
UnsupportedOperationException
- this operation is not supportedpublic abstract Set<Map.Entry<K,V>> entrySet()
entrySet
in interface Map<K,V>
UnsupportedOperationException
- this operation is not supportedpublic abstract int size()
size
in interface Map<K,V>
RuntimeException
- when a communication error occurredpublic abstract boolean isEmpty()
true
if this map contains no key-value mappings.isEmpty
in interface Map<K,V>
true
if this map contains no key-value mappingsRuntimeException
- if a communication error occurredpublic 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 invocationRuntimeException
- if a communication error occurredpublic abstract <RESULT,PARAMETER> RESULT invoke(NamedMapInvokable<K,V,PARAMETER,RESULT> invokable, PARAMETER parameter, long timeout, 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 invocationRuntimeException
- if a communication error occurredpublic abstract 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 operationRuntimeException
- if a communication error occurredpublic abstract Iterable<K> executeParallelQuery(QueryCondition<K,V> condition, long timeout, 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 operationRuntimeException
- if a communication error occurredpublic abstract 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 withNullPointerException
- if the specified value is nullInvokeException
- if exception occurred during parallel operationRuntimeException
- if a communication error occurredpublic abstract boolean containsValue(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 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
NullPointerException
- if the specified value is nullRuntimeException
- 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 String getMapName()
public void setSerializationMode(com.scaleoutsoftware.soss.client.util.SerializationMode sm)
public com.scaleoutsoftware.soss.client.util.SerializationMode getSerializationMode()
Copyright (C) 2007-2014 ScaleOut Software, Inc.