Class StateTable<K,N,S>

java.lang.Object
org.apache.flink.runtime.state.heap.StateTable<K,N,S>
Type Parameters:
K - type of key
N - type of namespace
S - 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 the InternalKeyContext.
  • Field Details

    • keyContext

      protected final InternalKeyContext<K> keyContext
      The key context view on the backend. This provides information, such as the currently active key.
    • 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.
    • keyGroupedStateMaps

      protected final StateMap<K,N,S>[] keyGroupedStateMaps
      Map for holding the actual state objects. The outer array represents the key-groups. All array positions will be initialized with an empty state map.
  • Constructor Details

    • 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 Details

    • createStateMap

      protected abstract StateMap<K,N,S> createStateMap()
    • stateSnapshot

      @Nonnull public abstract IterableStateSnapshot<K,N,S> stateSnapshot()
      Description copied from interface: StateSnapshotRestore
      Returns a snapshot of the state.
      Specified by:
      stateSnapshot in interface StateSnapshotRestore
    • isEmpty

      public boolean isEmpty()
      Returns whether this StateTable is empty.
      Returns:
      true if this StateTable has no elements, false otherwise.
      See Also:
    • size

      public int size()
      Returns the total number of entries in this StateTable. 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 given StateTransformationFunction to the state (1st input argument), using the given value as second input argument. The result of StateTransformationFunction.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.
    • getKeys

      public Stream<K> getKeys(N namespace)
    • 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()
    • getMapForKeyGroup

      @VisibleForTesting public StateMap<K,N,S> getMapForKeyGroup(int keyGroupIndex)
    • 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

    • setMetaInfo

      public void setMetaInfo(RegisteredKeyValueStateBackendMetaInfo<N,S> metaInfo)
    • put

      public void put(K key, int keyGroup, N namespace, S state)
    • iterator

      public Iterator<StateEntry<K,N,S>> iterator()
      Specified by:
      iterator in interface Iterable<K>
    • sizeOfNamespace

      @VisibleForTesting public int sizeOfNamespace(Object namespace)
    • keyGroupReader

      @Nonnull public StateSnapshotKeyGroupReader keyGroupReader(int readVersion)
      Description copied from interface: StateSnapshotRestore
      This method returns a StateSnapshotKeyGroupReader 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 interface StateSnapshotRestore
      Parameters:
      readVersion - the required version of the state to read.
      Returns:
      a reader that reads state by key-groups, for the given read version.