Class StateTable<K,N,S>
- java.lang.Object
-
- org.apache.flink.runtime.state.heap.StateTable<K,N,S>
-
- Type Parameters:
K
- type of keyN
- type of namespaceS
- type of state
- All Implemented Interfaces:
Iterable<StateEntry<K,N,S>>
,StateSnapshotRestore
- Direct Known Subclasses:
CopyOnWriteStateTable
public abstract class StateTable<K,N,S> extends Object implements StateSnapshotRestore, Iterable<StateEntry<K,N,S>>
Base class for state tables. Accesses to state are typically scoped by the currently active key, as provided through theInternalKeyContext
.
-
-
Field Summary
Fields Modifier and Type Field Description protected InternalKeyContext<K>
keyContext
The key context view on the backend.protected StateMap<K,N,S>[]
keyGroupedStateMaps
Map for holding the actual state objects.protected KeyGroupRange
keyGroupRange
The current key group range.protected TypeSerializer<K>
keySerializer
The serializer of the key.protected RegisteredKeyValueStateBackendMetaInfo<N,S>
metaInfo
Combined meta information such as name and serializers for this state.
-
Constructor Summary
Constructors Constructor Description StateTable(InternalKeyContext<K> keyContext, RegisteredKeyValueStateBackendMetaInfo<N,S> metaInfo, TypeSerializer<K> keySerializer)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
containsKey(N namespace)
Returns whether this table contains a mapping for the composite of active key and given namespace.protected abstract StateMap<K,N,S>
createStateMap()
S
get(K key, N namespace)
Returns the state for the composite of active key and given namespace.S
get(N namespace)
Returns the state of the mapping for the composite of active key and given namespace.int
getKeyGroupOffset()
Stream<K>
getKeys(N namespace)
Stream<Tuple2<K,N>>
getKeysAndNamespaces()
TypeSerializer<K>
getKeySerializer()
StateMap<K,N,S>
getMapForKeyGroup(int keyGroupIndex)
RegisteredKeyValueStateBackendMetaInfo<N,S>
getMetaInfo()
TypeSerializer<N>
getNamespaceSerializer()
StateMap<K,N,S>[]
getState()
Returns the internal data structure.InternalKvState.StateIncrementalVisitor<K,N,S>
getStateIncrementalVisitor(int recommendedMaxNumberOfReturnedRecords)
TypeSerializer<S>
getStateSerializer()
boolean
isEmpty()
Returns whether thisStateTable
is empty.Iterator<StateEntry<K,N,S>>
iterator()
StateSnapshotKeyGroupReader
keyGroupReader(int readVersion)
This method returns aStateSnapshotKeyGroupReader
that can be used to restore the state on a per-key-group basis.void
put(K key, int keyGroup, N namespace, S state)
void
put(N namespace, S state)
Maps the composite of active key and given namespace to the specified state.void
remove(N namespace)
Removes the mapping for the composite of active key and given namespace.S
removeAndGetOld(N namespace)
Removes the mapping for the composite of active key and given namespace, returning the state that was found under the entry.void
setMetaInfo(RegisteredKeyValueStateBackendMetaInfo<N,S> metaInfo)
int
size()
Returns the total number of entries in thisStateTable
.int
sizeOfNamespace(Object namespace)
abstract IterableStateSnapshot<K,N,S>
stateSnapshot()
Returns a snapshot of the state.<T> void
transform(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, spliterator
-
-
-
-
Field Detail
-
keyContext
protected final InternalKeyContext<K> keyContext
The key context view on the backend. This provides information, such as the currently active key.
-
metaInfo
protected RegisteredKeyValueStateBackendMetaInfo<N,S> metaInfo
Combined meta information such as name and serializers for this state.
-
keySerializer
protected final TypeSerializer<K> keySerializer
The serializer of the key.
-
keyGroupRange
protected final KeyGroupRange keyGroupRange
The current key group range.
-
-
Constructor Detail
-
StateTable
public StateTable(InternalKeyContext<K> keyContext, RegisteredKeyValueStateBackendMetaInfo<N,S> metaInfo, TypeSerializer<K> keySerializer)
- Parameters:
keyContext
- the key context provides the key scope for all put/get/delete operations.metaInfo
- the meta information, including the type serializer for state copy-on-write.keySerializer
- the serializer of the key.
-
-
Method Detail
-
stateSnapshot
@Nonnull public abstract IterableStateSnapshot<K,N,S> stateSnapshot()
Description copied from interface:StateSnapshotRestore
Returns a snapshot of the state.- Specified by:
stateSnapshot
in interfaceStateSnapshotRestore
-
isEmpty
public boolean isEmpty()
Returns whether thisStateTable
is empty.- Returns:
true
if thisStateTable
has no elements,false
otherwise.- See Also:
size()
-
size
public int size()
Returns the total number of entries in thisStateTable
. This is the sum of both sub-tables.- Returns:
- the number of entries in this
StateTable
.
-
get
public S get(N namespace)
Returns the state of the mapping for the composite of active key and given namespace.- Parameters:
namespace
- the namespace. Not null.- Returns:
- the states of the mapping with the specified key/namespace composite key, or
null
if no mapping for the specified key is found.
-
containsKey
public boolean containsKey(N namespace)
Returns whether this table contains a mapping for the composite of active key and given namespace.- Parameters:
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 void put(N namespace, S state)
Maps the composite of active key and given namespace to the specified state.- Parameters:
namespace
- the namespace. Not null.state
- the state. Can be null.
-
remove
public void remove(N namespace)
Removes the mapping for the composite of active key and given namespace. This method should be preferred over#removeAndGetOld(N)
when the caller is not interested in the old state.- Parameters:
namespace
- the namespace of the mapping to remove. Not null.
-
removeAndGetOld
public S removeAndGetOld(N namespace)
Removes the mapping for the composite of active key and given namespace, returning the state that was found under the entry.- Parameters:
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 <T> void transform(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:
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.
-
get
public S get(K key, N namespace)
Returns the state for the composite of active key and given namespace. This is typically used by queryable state.- 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.
-
getStateIncrementalVisitor
public InternalKvState.StateIncrementalVisitor<K,N,S> getStateIncrementalVisitor(int recommendedMaxNumberOfReturnedRecords)
-
getState
@VisibleForTesting public StateMap<K,N,S>[] getState()
Returns the internal data structure.
-
getKeyGroupOffset
public int getKeyGroupOffset()
-
getMapForKeyGroup
@VisibleForTesting public StateMap<K,N,S> getMapForKeyGroup(int keyGroupIndex)
-
getKeySerializer
public TypeSerializer<K> getKeySerializer()
-
getStateSerializer
public TypeSerializer<S> getStateSerializer()
-
getNamespaceSerializer
public TypeSerializer<N> getNamespaceSerializer()
-
getMetaInfo
public RegisteredKeyValueStateBackendMetaInfo<N,S> getMetaInfo()
-
setMetaInfo
public void setMetaInfo(RegisteredKeyValueStateBackendMetaInfo<N,S> metaInfo)
-
iterator
public Iterator<StateEntry<K,N,S>> iterator()
-
sizeOfNamespace
@VisibleForTesting public int sizeOfNamespace(Object namespace)
-
keyGroupReader
@Nonnull public StateSnapshotKeyGroupReader keyGroupReader(int readVersion)
Description copied from interface:StateSnapshotRestore
This method returns aStateSnapshotKeyGroupReader
that can be used to restore the state on a per-key-group basis. This method tries to return a reader for the given version hint.- Specified by:
keyGroupReader
in interfaceStateSnapshotRestore
- Parameters:
readVersion
- the required version of the state to read.- Returns:
- a reader that reads state by key-groups, for the given read version.
-
-