Class AbstractStateStore

java.lang.Object
com.cognite.client.statestore.AbstractStateStore
All Implemented Interfaces:
StateStore, Closeable, AutoCloseable
Direct Known Subclasses:
LocalStateStore, MemoryStateStore, RawStateStore

public abstract class AbstractStateStore extends Object implements StateStore, Closeable
Abstract parent class for all state store implementations.
  • Field Details

    • COLUMN_KEY_LOW

      protected static final String COLUMN_KEY_LOW
      See Also:
    • COLUMN_KEY_HIGH

      protected static final String COLUMN_KEY_HIGH
      See Also:
    • MIN_MAX_COMMIT_INTERVAL

      protected static final Duration MIN_MAX_COMMIT_INTERVAL
    • DEFAULT_MAX_COMMIT_INTERVAL

      protected static final Duration DEFAULT_MAX_COMMIT_INTERVAL
    • MAX_MAX_COMMIT_INTERVAL

      protected static final Duration MAX_MAX_COMMIT_INTERVAL
    • LOG

      protected final org.slf4j.Logger LOG
    • stateMap

      protected ConcurrentMap<String,com.google.protobuf.Struct> stateMap
    • modifiedEntries

      protected Set<String> modifiedEntries
    • deletedEntries

      protected Set<String> deletedEntries
    • executor

      protected final ScheduledThreadPoolExecutor executor
    • recurringTask

      protected ScheduledFuture<?> recurringTask
  • Constructor Details

    • AbstractStateStore

      public AbstractStateStore()
  • Method Details

    • setHigh

      public void setHigh(String key, long value)
      Set/update the high watermark for a single id.
      Specified by:
      setHigh in interface StateStore
      Parameters:
      key - The id to store the state of.
      value - The value of the high watermark.
    • expandHigh

      public void expandHigh(String key, long value)
      Like StateStore.setHigh(String, long), but only sets the state if the proposed state is higher than the current state.
      Specified by:
      expandHigh in interface StateStore
      Parameters:
      key - key The id to store the state of.
      value - The value of the high watermark.
    • getHigh

      public OptionalLong getHigh(String key)
      Get the high watermark state for a single id.
      Specified by:
      getHigh in interface StateStore
      Parameters:
      key - The id to get the state of.
      Returns:
      The value of the high watermark. An empty optional in case the state does not exist.
    • setLow

      public void setLow(String key, long value)
      Set/update the low watermark for a single id.
      Specified by:
      setLow in interface StateStore
      Parameters:
      key - The id to store the state of.
      value - The value of the low watermark.
    • expandLow

      public void expandLow(String key, long value)
      Like StateStore.setLow(String, long), but only sets the state if the proposed state is lower than the current state.
      Specified by:
      expandLow in interface StateStore
      Parameters:
      key - key The id to store the state of.
      value - The value of the low watermark.
    • getLow

      public OptionalLong getLow(String key)
      Get the low watermark state for a single id.
      Specified by:
      getLow in interface StateStore
      Parameters:
      key - The id to get the state of.
      Returns:
      The value of the low watermark. An empty optional in case the state does not exist.
    • isOutsideState

      public boolean isOutsideState(String key, long value)
      Check if a state is outside the stored state interval. I.e. if a state is higher than the current high watermark or lower than the current low watermark. The method can be used for determining if a data object should be processed or not.
      Specified by:
      isOutsideState in interface StateStore
      Parameters:
      key - the id to test.
      value - the state to test.
      Returns:
      True if the state is outside of stored state or if the key is previously unseen.
    • getState

      public Optional<com.google.protobuf.Struct> getState(String key)
      Get the set of states for a single id. This will give you both the low and high watermark (if set) as a Struct. The returned Struct is a read-only view of the state values.
      Specified by:
      getState in interface StateStore
      Parameters:
      key - The id to get the state of.
      Returns:
      All the states in a Struct.
    • deleteState

      public void deleteState(String key)
      Delete the state(s) for a given id.
      Specified by:
      deleteState in interface StateStore
      Parameters:
      key - The id to delete the state(s) of.
    • keySet

      public Set<String> keySet()
      Returns a read-only set of all state keys.
      Specified by:
      keySet in interface StateStore
      Returns:
      An immutable Set of all state keys.
    • load

      public abstract void load() throws Exception
      Load the states from the persistent store.
      Specified by:
      load in interface StateStore
      Throws:
      Exception
    • commit

      public abstract void commit() throws Exception
      Commit the current states to the persistent store. Will overwrite/replace previously persisted states.
      Specified by:
      commit in interface StateStore
      Throws:
      Exception
    • verifyStateMap

      protected boolean verifyStateMap()
    • start

      public boolean start()
      Start a background thread to perform a commit every maxUploadInterval. The default upload interval is every 20 seconds. If the background thread has already been started (for example by an earlier call to start() then this method does nothing and returns false.
      Specified by:
      start in interface StateStore
      Returns:
      true if the upload thread started successfully, false if the background thread has already been started.
    • close

      public void close()
      A mirror of the stop() method to support auto close in a try-with-resources statement.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      See Also:
    • stop

      public boolean stop()
      Stops the background thread if it is running and ensures the upload queue is empty by calling upload() one last time after shutting down the thread.
      Specified by:
      stop in interface StateStore
      Returns:
      true if the upload thread stopped successfully, false if the upload thread was not started in the first place.