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>keyContextThe key context view on the backend.protected StateMap<K,N,S>[]keyGroupedStateMapsMap for holding the actual state objects.protected KeyGroupRangekeyGroupRangeThe current key group range.protected org.apache.flink.api.common.typeutils.TypeSerializer<K>keySerializerThe serializer of the key.protected RegisteredKeyValueStateBackendMetaInfo<N,S>metaInfoCombined meta information such as name and serializers for this state.
-
Constructor Summary
Constructors Constructor Description StateTable(InternalKeyContext<K> keyContext, RegisteredKeyValueStateBackendMetaInfo<N,S> metaInfo, org.apache.flink.api.common.typeutils.TypeSerializer<K> keySerializer)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleancontainsKey(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()Sget(K key, N namespace)Returns the state for the composite of active key and given namespace.Sget(N namespace)Returns the state of the mapping for the composite of active key and given namespace.intgetKeyGroupOffset()Stream<K>getKeys(N namespace)Stream<org.apache.flink.api.java.tuple.Tuple2<K,N>>getKeysAndNamespaces()org.apache.flink.api.common.typeutils.TypeSerializer<K>getKeySerializer()StateMap<K,N,S>getMapForKeyGroup(int keyGroupIndex)RegisteredKeyValueStateBackendMetaInfo<N,S>getMetaInfo()org.apache.flink.api.common.typeutils.TypeSerializer<N>getNamespaceSerializer()StateMap<K,N,S>[]getState()Returns the internal data structure.InternalKvState.StateIncrementalVisitor<K,N,S>getStateIncrementalVisitor(int recommendedMaxNumberOfReturnedRecords)org.apache.flink.api.common.typeutils.TypeSerializer<S>getStateSerializer()booleanisEmpty()Returns whether thisStateTableis empty.Iterator<StateEntry<K,N,S>>iterator()StateSnapshotKeyGroupReaderkeyGroupReader(int readVersion)This method returns aStateSnapshotKeyGroupReaderthat can be used to restore the state on a per-key-group basis.voidput(K key, int keyGroup, N namespace, S state)voidput(N namespace, S state)Maps the composite of active key and given namespace to the specified state.voidremove(N namespace)Removes the mapping for the composite of active key and given namespace.SremoveAndGetOld(N namespace)Removes the mapping for the composite of active key and given namespace, returning the state that was found under the entry.voidsetMetaInfo(RegisteredKeyValueStateBackendMetaInfo<N,S> metaInfo)intsize()Returns the total number of entries in thisStateTable.intsizeOfNamespace(Object namespace)abstract IterableStateSnapshot<K,N,S>stateSnapshot()Returns a snapshot of the state.<T> voidtransform(N namespace, T value, StateTransformationFunction<S,T> transformation)Applies the givenStateTransformationFunctionto 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 org.apache.flink.api.common.typeutils.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, org.apache.flink.api.common.typeutils.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:StateSnapshotRestoreReturns a snapshot of the state.- Specified by:
stateSnapshotin interfaceStateSnapshotRestore
-
isEmpty
public boolean isEmpty()
Returns whether thisStateTableis empty.- Returns:
trueif thisStateTablehas no elements,falseotherwise.- 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
nullif 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:
trueif this map contains the specified key/namespace composite key,falseotherwise.
-
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
nullif 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 givenStateTransformationFunctionto 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
nullif no mapping for the specified key is found.
-
getKeysAndNamespaces
public Stream<org.apache.flink.api.java.tuple.Tuple2<K,N>> getKeysAndNamespaces()
-
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()
-
getKeySerializer
public org.apache.flink.api.common.typeutils.TypeSerializer<K> getKeySerializer()
-
getStateSerializer
public org.apache.flink.api.common.typeutils.TypeSerializer<S> getStateSerializer()
-
getNamespaceSerializer
public org.apache.flink.api.common.typeutils.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:StateSnapshotRestoreThis method returns aStateSnapshotKeyGroupReaderthat 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:
keyGroupReaderin 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.
-
-