Class StateMap<K,N,S>
- java.lang.Object
-
- org.apache.flink.runtime.state.heap.StateMap<K,N,S>
-
- Type Parameters:
K
- type of keyN
- type of namespaceS
- type of state
- All Implemented Interfaces:
Iterable<StateEntry<K,N,S>>
- Direct Known Subclasses:
CopyOnWriteStateMap
public abstract class StateMap<K,N,S> extends Object implements Iterable<StateEntry<K,N,S>>
Base class for state maps.
-
-
Constructor Summary
Constructors Constructor Description StateMap()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract boolean
containsKey(K key, N namespace)
Returns whether this map contains the specified key/namespace composite key.abstract S
get(K key, N namespace)
Returns the state for the composite of active key and given namespace.abstract Stream<K>
getKeys(N namespace)
abstract InternalKvState.StateIncrementalVisitor<K,N,S>
getStateIncrementalVisitor(int recommendedMaxNumberOfReturnedRecords)
boolean
isEmpty()
Returns whether thisStateMap
is empty.abstract void
put(K key, N namespace, S state)
Maps the specified key/namespace composite key to the specified value.abstract S
putAndGetOld(K key, N namespace, S state)
Maps the composite of active key and given namespace to the specified state.void
releaseSnapshot(StateMapSnapshot<K,N,S,? extends StateMap<K,N,S>> snapshotToRelease)
Releases a snapshot for thisStateMap
.abstract void
remove(K key, N namespace)
Removes the mapping for the composite of active key and given namespace.abstract S
removeAndGetOld(K key, N namespace)
Removes the mapping for the composite of active key and given namespace, returning the state that was found under the entry.abstract int
size()
Returns the total number of entries in thisStateMap
.abstract int
sizeOfNamespace(Object namespace)
abstract StateMapSnapshot<K,N,S,? extends StateMap<K,N,S>>
stateSnapshot()
Creates a snapshot of thisStateMap
, to be written in checkpointing.abstract <T> void
transform(K key, N namespace, T value, StateTransformationFunction<S,T> transformation)
Applies the givenStateTransformationFunction
to the state (1st input argument), using the given value as second input argument.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
-
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Returns whether thisStateMap
is empty.
-
size
public abstract int size()
Returns the total number of entries in thisStateMap
.- Returns:
- the number of entries in this
StateMap
.
-
get
public abstract S get(K key, N namespace)
Returns the state for the composite of active key and given namespace.- Parameters:
key
- the key. Not null.namespace
- the namespace. Not null.- Returns:
- the state of the mapping with the specified key/namespace composite key, or
null
if no mapping for the specified key is found.
-
containsKey
public abstract boolean containsKey(K key, N namespace)
Returns whether this map contains the specified key/namespace composite key.- Parameters:
key
- the key in the composite key to search for. Not null.namespace
- the namespace in the composite key to search for. Not null.- Returns:
true
if this map contains the specified key/namespace composite key,false
otherwise.
-
put
public abstract void put(K key, N namespace, S state)
Maps the specified key/namespace composite key to the specified value. This method should be preferred over#putAndGetOld(K, N, S)
(key, Namespace, State) when the caller is not interested in the old state.- Parameters:
key
- the key. Not null.namespace
- the namespace. Not null.state
- the state. Can be null.
-
putAndGetOld
public abstract S putAndGetOld(K key, N namespace, S state)
Maps the composite of active key and given namespace to the specified state. Returns the previous state that was registered under the composite key.- Parameters:
key
- the key. Not null.namespace
- the namespace. Not null.state
- the state. Can be null.- Returns:
- the state of any previous mapping with the specified key or
null
if there was no such mapping.
-
remove
public abstract void remove(K key, N namespace)
Removes the mapping for the composite of active key and given namespace. This method should be preferred over#removeAndGetOld(K, N)
when the caller is not interested in the old state.- Parameters:
key
- the key of the mapping to remove. Not null.namespace
- the namespace of the mapping to remove. Not null.
-
removeAndGetOld
public abstract S removeAndGetOld(K key, N namespace)
Removes the mapping for the composite of active key and given namespace, returning the state that was found under the entry.- Parameters:
key
- the key of the mapping to remove. Not null.namespace
- the namespace of the mapping to remove. Not null.- Returns:
- the state of the removed mapping or
null
if no mapping for the specified key was found.
-
transform
public abstract <T> void transform(K key, N namespace, T value, StateTransformationFunction<S,T> transformation) throws Exception
Applies the givenStateTransformationFunction
to the state (1st input argument), using the given value as second input argument. The result ofStateTransformationFunction.apply(Object, Object)
is then stored as the new state. This function is basically an optimization for get-update-put pattern.- Parameters:
key
- the key. Not null.namespace
- the namespace. Not null.value
- the value to use in transforming the state. Can be null.transformation
- the transformation function.- Throws:
Exception
- if some exception happens in the transformation function.
-
getStateIncrementalVisitor
public abstract InternalKvState.StateIncrementalVisitor<K,N,S> getStateIncrementalVisitor(int recommendedMaxNumberOfReturnedRecords)
-
stateSnapshot
@Nonnull public abstract StateMapSnapshot<K,N,S,? extends StateMap<K,N,S>> stateSnapshot()
Creates a snapshot of thisStateMap
, to be written in checkpointing. Users should callreleaseSnapshot(StateMapSnapshot)
after using the returned object.- Returns:
- a snapshot from this
StateMap
, for checkpointing.
-
releaseSnapshot
public void releaseSnapshot(StateMapSnapshot<K,N,S,? extends StateMap<K,N,S>> snapshotToRelease)
Releases a snapshot for thisStateMap
. This method should be called once a snapshot is no more needed.- Parameters:
snapshotToRelease
- the snapshot to release, which was previously created by this state map.
-
sizeOfNamespace
@VisibleForTesting public abstract int sizeOfNamespace(Object namespace)
-
-