Class ReadWriteMapImpl<K,V>
- java.lang.Object
-
- org.infinispan.functional.impl.ReadWriteMapImpl<K,V>
-
- All Implemented Interfaces:
AutoCloseable,FunctionalMap<K,V>,FunctionalMap.ReadWriteMap<K,V>
@Experimental public final class ReadWriteMapImpl<K,V> extends Object implements FunctionalMap.ReadWriteMap<K,V>
Read-write map implementation.- Since:
- 8.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.infinispan.functional.FunctionalMap
FunctionalMap.ReadOnlyMap<K,V>, FunctionalMap.ReadWriteMap<K,V>, FunctionalMap.WriteOnlyMap<K,V>
-
-
Field Summary
Fields Modifier and Type Field Description protected FunctionalMapImpl<K,V>fmapprotected DataConversionkeyDataConversionprotected Paramsparamsprotected DataConversionvalueDataConversion
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()static <K,V>
FunctionalMap.ReadWriteMap<K,V>create(FunctionalMapImpl<K,V> functionalMap)protected Map<?,?>encodeEntries(Map<? extends K,?> entries)protected Set<?>encodeKeys(Set<? extends K> keys)<R> CompletableFuture<R>eval(K key, Function<EntryView.ReadWriteEntryView<K,V>,R> f)Evaluate a read-write function on the value and metadata associated with the key and return aCompletableFuturewith the return type of the function.<T,R>
CompletableFuture<R>eval(K key, T argument, BiFunction<T,EntryView.ReadWriteEntryView<K,V>,R> f)Evaluate a read-write function, with an argument passed in and aEntryView.WriteEntryViewof the value associated with the key, and return aCompletableFuturewhich will be completed with the returned value by the function.<R> Traversable<R>evalAll(Function<EntryView.ReadWriteEntryView<K,V>,R> f)Evaluate a read-writeFunctionoperation with theEntryView.ReadWriteEntryViewof the value associated with the key, for all existing keys, and returns aTraversableto navigate each of theFunctioninvocation returns.<T,R>
Traversable<R>evalMany(Map<? extends K,? extends T> arguments, BiFunction<T,EntryView.ReadWriteEntryView<K,V>,R> f)Evaluate a read-writeBiFunction, with an argument passed in and aEntryView.ReadWriteEntryViewof the value associated with the key, for each of the keys in the set passed in, and returns anTraversableto navigate each of theBiFunctioninvocation returns.<R> Traversable<R>evalMany(Set<? extends K> keys, Function<EntryView.ReadWriteEntryView<K,V>,R> f)Evaluate a read-writeFunctionoperation with theEntryView.ReadWriteEntryViewof the value associated with the key, for each of the keys in the set passed in, and returns aTraversableto navigate each of theFunctioninvocation returns.protected InvocationContextgetInvocationContext(boolean isWrite, int keyCount)StringgetName()Functional map's name.ComponentStatusgetStatus()Functional map's status.protected <T> CompletableFuture<T>invokeAsync(InvocationContext ctx, VisitableCommand cmd)Listeners.ReadWriteListeners<K,V>listeners()Allows to read-write listeners to be registered.FunctionalMap.ReadWriteMap<K,V>withParams(Param<?>... ps)Tweak read-write functional map executions providingParaminstances.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.AutoCloseable
close
-
Methods inherited from interface org.infinispan.functional.FunctionalMap
getName, getStatus, isEncoded
-
-
-
-
Field Detail
-
fmap
protected final FunctionalMapImpl<K,V> fmap
-
params
protected final Params params
-
keyDataConversion
protected final DataConversion keyDataConversion
-
valueDataConversion
protected final DataConversion valueDataConversion
-
-
Method Detail
-
create
public static <K,V> FunctionalMap.ReadWriteMap<K,V> create(FunctionalMapImpl<K,V> functionalMap)
-
eval
public <R> CompletableFuture<R> eval(K key, Function<EntryView.ReadWriteEntryView<K,V>,R> f)
Description copied from interface:FunctionalMap.ReadWriteMapEvaluate a read-write function on the value and metadata associated with the key and return aCompletableFuturewith the return type of the function. If the user is not sure if the key is present,EntryView.ReadEntryView.find()can be used to find out for sure. This method can be used to implement single-key read-write operations inConcurrentMapandjavax.cache.Cachethat do not depend on value information given by the user such as:Map.remove(Object)javax.cache.Cache#remove(Object)javax.cache.Cache#getAndRemove(Object)javax.cache.Cache#invoke(Object, EntryProcessor, Object...)
The function must not mutate neither the key returned through
EntryView.ReadEntryView.key()nor the internally stored value provided throughEntryView.ReadEntryView.get()orEntryView.ReadEntryView.find().- Specified by:
evalin interfaceFunctionalMap.ReadWriteMap<K,V>- Type Parameters:
R- function return type- Parameters:
key- the key associated with theEntryView.ReadWriteEntryViewto be passed to the function.f- function that takes aEntryView.ReadWriteEntryViewassociated with the key, and returns a value.- Returns:
- a
CompletableFuturewhich will be completed with the returned value from the function
-
eval
public <T,R> CompletableFuture<R> eval(K key, T argument, BiFunction<T,EntryView.ReadWriteEntryView<K,V>,R> f)
Description copied from interface:FunctionalMap.ReadWriteMapEvaluate a read-write function, with an argument passed in and aEntryView.WriteEntryViewof the value associated with the key, and return aCompletableFuturewhich will be completed with the returned value by the function.This method provides the the capability to both update the value and metadata associated with that key, and return previous value or metadata.
This method can be used to implement the vast majority of single-key read-write operations in
ConcurrentMapandjavax.cache.Cachesuch as:Map.put(Object, Object)ConcurrentMap.putIfAbsent(Object, Object)ConcurrentMap.replace(Object, Object)ConcurrentMap.replace(Object, Object, Object)ConcurrentMap.remove(Object, Object)javax.cache.Cache#getAndPut(Object, Object)javax.cache.Cache#putIfAbsent(Object, Object)javax.cache.Cache#remove(Object, Object)javax.cache.Cache#replace(Object, Object, Object)javax.cache.Cache#replace(Object, Object)javax.cache.Cache#getAndReplace(Object, Object)
The functionality provided by this function could indeed be implemented with
FunctionalMap.ReadWriteMap.eval(Object, Function), but there's a crucial difference. If you want to store a value and reference the value to be stored from the passed in operation,FunctionalMap.ReadWriteMap.eval(Object, Function)needs to capture that value. Capturing means that each time the operation is called, a new lambda needs to be instantiated. By offering aBiFunctionthat takes user provided value as first parameter, the operation does not capture any external objects when implementing simple operations such asjavax.cache.Cache#getAndPut(Object, Object), and hence, theBiFunctioncould be cached and reused each time it's invoked.Note that when
encodersare in place despite the argument type and value type don't have to match the argument will use value encoding.The function must not mutate neither the key returned through
EntryView.ReadEntryView.key()nor the internally stored value provided throughEntryView.ReadEntryView.get()orEntryView.ReadEntryView.find().- Specified by:
evalin interfaceFunctionalMap.ReadWriteMap<K,V>R- type of the function's return- Parameters:
key- the key associated with theEntryView.ReadWriteEntryViewto be passed to the operationargument- argument passed in as first parameter to theBiFunction.f- operation that takes a user defined value, and aEntryView.ReadWriteEntryViewassociated with the key, and writes to theEntryView.ReadWriteEntryViewpassed in, possibly returning previously stored value or metadata information- Returns:
- a
CompletableFuturewhich will be completed with the returned value from the function
-
evalMany
public <T,R> Traversable<R> evalMany(Map<? extends K,? extends T> arguments, BiFunction<T,EntryView.ReadWriteEntryView<K,V>,R> f)
Description copied from interface:FunctionalMap.ReadWriteMapEvaluate a read-writeBiFunction, with an argument passed in and aEntryView.ReadWriteEntryViewof the value associated with the key, for each of the keys in the set passed in, and returns anTraversableto navigate each of theBiFunctioninvocation returns.This method can be used to implement operations that store a set of keys and return previous values or metadata parameters.
These kind of operations are preferred to traditional end user iterations because the internal logic can often iterate more efficiently since it knows more about the system.
Note that when
encodersare in place despite the argument type and value type don't have to match the argument will use value encoding.The function must not mutate neither the key returned through
EntryView.ReadEntryView.key()nor the internally stored value provided throughEntryView.ReadEntryView.get()orEntryView.ReadEntryView.find().- Specified by:
evalManyin interfaceFunctionalMap.ReadWriteMap<K,V>- Parameters:
arguments- the key/value pairs associated with each of theEntryView.ReadWriteEntryViewpassed in the function callbacksf- function that takes in a value associated with a key in the entries collection and theEntryView.ReadWriteEntryViewassociated with that key in the cache- Returns:
- a
Traversableto navigate eachBiFunctionreturn
-
evalMany
public <R> Traversable<R> evalMany(Set<? extends K> keys, Function<EntryView.ReadWriteEntryView<K,V>,R> f)
Description copied from interface:FunctionalMap.ReadWriteMapEvaluate a read-writeFunctionoperation with theEntryView.ReadWriteEntryViewof the value associated with the key, for each of the keys in the set passed in, and returns aTraversableto navigate each of theFunctioninvocation returns.This method can be used to implement operations such as
javax.cache.Cache#invokeAll(Set, EntryProcessor, Object...), or a remove a set of keys returning previous values or metadata parameters.The function must not mutate neither the key returned through
EntryView.ReadEntryView.key()nor the internally stored value provided throughEntryView.ReadEntryView.get()orEntryView.ReadEntryView.find().- Specified by:
evalManyin interfaceFunctionalMap.ReadWriteMap<K,V>- Parameters:
keys- the keys associated with each of theEntryView.ReadWriteEntryViewpassed in the function callbacksf- function that theEntryView.ReadWriteEntryViewassociated with one of the keys passed in, and returns a value- Returns:
- a
Traversableto navigate eachFunctionreturn
-
evalAll
public <R> Traversable<R> evalAll(Function<EntryView.ReadWriteEntryView<K,V>,R> f)
Description copied from interface:FunctionalMap.ReadWriteMapEvaluate a read-writeFunctionoperation with theEntryView.ReadWriteEntryViewof the value associated with the key, for all existing keys, and returns aTraversableto navigate each of theFunctioninvocation returns.This method can be used to an operation that removes all cached entries individually, and returns previous value and/or metadata parameters.
The function must not mutate neither the key returned through
EntryView.ReadEntryView.key()nor the internally stored value provided throughEntryView.ReadEntryView.get()orEntryView.ReadEntryView.find().- Specified by:
evalAllin interfaceFunctionalMap.ReadWriteMap<K,V>- Returns:
- a
Traversableto navigate eachFunctionreturn
-
listeners
public Listeners.ReadWriteListeners<K,V> listeners()
Description copied from interface:FunctionalMap.ReadWriteMapAllows to read-write listeners to be registered.- Specified by:
listenersin interfaceFunctionalMap.ReadWriteMap<K,V>
-
withParams
public FunctionalMap.ReadWriteMap<K,V> withParams(Param<?>... ps)
Description copied from interface:FunctionalMap.ReadWriteMapTweak read-write functional map executions providingParaminstances.- Specified by:
withParamsin interfaceFunctionalMap<K,V>- Specified by:
withParamsin interfaceFunctionalMap.ReadWriteMap<K,V>
-
getName
public String getName()
Description copied from interface:FunctionalMapFunctional map's name.- Specified by:
getNamein interfaceFunctionalMap<K,V>
-
getStatus
public ComponentStatus getStatus()
Description copied from interface:FunctionalMapFunctional map's status.- Specified by:
getStatusin interfaceFunctionalMap<K,V>
-
close
public void close() throws Exception- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
getInvocationContext
protected InvocationContext getInvocationContext(boolean isWrite, int keyCount)
-
invokeAsync
protected <T> CompletableFuture<T> invokeAsync(InvocationContext ctx, VisitableCommand cmd)
-
-