Class StateMap<K,N,S>

java.lang.Object
org.apache.flink.runtime.state.heap.StateMap<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>>
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 Details

    • StateMap

      public StateMap()
  • Method Details

    • isEmpty

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

      public abstract int size()
      Returns the total number of entries in this StateMap.
      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 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:
      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.
    • getKeys

      public abstract Stream<K> getKeys(N namespace)
    • 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 this StateMap, to be written in checkpointing. Users should call releaseSnapshot(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 this StateMap. 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)