Class JsonFileStateStore

java.lang.Object
io.cloudstub.core.internal.store.JsonFileStateStore
All Implemented Interfaces:
StateStore

public final class JsonFileStateStore extends Object implements StateStore
Persistent StateStore that serialises state to a JSON file on every mutation.

Reads the existing file on construction (if present) so state survives a CloudStub restart. Writes are flushed synchronously — put returns only once the value has been written via an atomic temp-file rename to {storeDir}/cloudstub-state.json, which prevents partial writes.

Type fidelity: the mapper records each value's concrete type, so a value stored as a Foo is read back as a Foo after a restart — not as a generic map. This matches the in-memory store's behaviour. Stored value types must be on the classpath when the store is reloaded. Default typing is safe here because the store only ever reads back its own locally-written file, never untrusted input.

Thread-safe: mutations are serialised through writeLock.

  • Constructor Details

    • JsonFileStateStore

      public JsonFileStateStore(Path storeDir)
  • Method Details

    • put

      public void put(String key, Object value)
      Description copied from interface: StateStore
      Store a value under key. Overwrites any existing entry at the same key.
      Specified by:
      put in interface StateStore
      Parameters:
      key - path-style key, e.g. "sqs/queues/my-queue/messages/abc-123"
      value - the value to store; must be JSON-serialisable
    • get

      public Object get(String key)
      Description copied from interface: StateStore
      Retrieve the value stored under key, or null if the key does not exist.
      Specified by:
      get in interface StateStore
      Parameters:
      key - path-style key
      Returns:
      the stored value, or null
    • list

      public List<String> list(String prefix)
      Description copied from interface: StateStore
      List all keys whose path begins with prefix. Passing "sqs/" lists every key the SQS module has written.
      Specified by:
      list in interface StateStore
      Parameters:
      prefix - key prefix to match
      Returns:
      sorted list of matching keys; empty if none match
    • delete

      public void delete(String key)
      Description copied from interface: StateStore
      Delete a single entry. No-op if the key does not exist.
      Specified by:
      delete in interface StateStore
      Parameters:
      key - the key to delete
    • clear

      public void clear(String prefix)
      Description copied from interface: StateStore
      Delete all entries whose key begins with prefix. Passing "sqs/" clears all SQS state.
      Specified by:
      clear in interface StateStore
      Parameters:
      prefix - the key prefix to clear
    • clearAll

      public void clearAll()
      Description copied from interface: StateStore
      Delete all entries in the store across all services. Called by the global reset endpoint.
      Specified by:
      clearAll in interface StateStore