Class DynamoDbSessionStore<S>
- Type Parameters:
S- the type of custom session state
- All Implemented Interfaces:
SessionStore<S>,SnapshotReader<S>,SnapshotSubscriber,SnapshotWriter<S>
SessionStore and SnapshotSubscriber.
Persists session snapshots with the same sharded checkpoint + diff + pointer layout as the
Firestore backend (see FirestoreSessionStore), sharing the pure logic in SnapshotSharding.
Storage layout (single table)
All rows live in one table (default genkit-sessions) with a composite key: partition
key pk = the per-tenant prefix (default "global"), sort key sk:
SNAP#<snapshotId>— one metadata row per snapshot.kindis"checkpoint"or"diff"; carriescheckpointId,checkpointShardCount,segmentPath,statePatch(RFC-6902 as a JSON string for diffs),error(JSON string), and a numericversionfor optimistic concurrency.SHARD#<checkpointId>#<index>— a shard of the checkpoint state JSON.PTR#<sessionId>— the current leaf pointer for a session.
Concurrency
DynamoDB has no interactive read-then-write transaction, so the observable
saveSnapshot contract is preserved with idempotent unique-key writes plus a conditional,
monotonic pointer advance: shards are written first, then the snapshot row, then the pointer
flips — so a reader following the pointer always sees complete data. Updating an existing
snapshot id uses a version conditional put and re-applies the (pure) mutator on conflict.
-
Constructor Summary
ConstructorsConstructorDescriptionDynamoDbSessionStore(software.amazon.awssdk.services.dynamodb.DynamoDbClient db) Creates a store with default options.DynamoDbSessionStore(software.amazon.awssdk.services.dynamodb.DynamoDbClient db, DynamoDbSessionStoreOptions options) Creates a store. -
Method Summary
Modifier and TypeMethodDescriptionRetrieves a session snapshot.onSnapshotStateChange(String snapshotId, Consumer<SessionSnapshot<?>> cb, SessionStoreOptions storeOpts) Subscribes to status changes for the snapshot identified bysnapshotId.saveSnapshot(String snapshotId, SnapshotMutator<S> mutator, SessionStoreOptions storeOpts) Atomically reads the snapshot identified bysnapshotId(or creates a new one), appliesmutator, and persists the result.
-
Constructor Details
-
DynamoDbSessionStore
public DynamoDbSessionStore(software.amazon.awssdk.services.dynamodb.DynamoDbClient db) Creates a store with default options.- Parameters:
db- the DynamoDB client
-
DynamoDbSessionStore
public DynamoDbSessionStore(software.amazon.awssdk.services.dynamodb.DynamoDbClient db, DynamoDbSessionStoreOptions options) Creates a store.- Parameters:
db- the DynamoDB clientoptions- the store options
-
-
Method Details
-
saveSnapshot
public String saveSnapshot(String snapshotId, SnapshotMutator<S> mutator, SessionStoreOptions storeOpts) Atomically reads the snapshot identified bysnapshotId(or creates a new one), appliesmutator, and persists the result.Implements the same identity/sessionId/status defaulting contract as the reference stores, then writes the snapshot (checkpoint shards + metadata row) and advances the session pointer.
- Specified by:
saveSnapshotin interfaceSnapshotWriter<S>- Parameters:
snapshotId- the ID of the snapshot to write; ifnullthe store mints a UUIDmutator- the pure read-modify-write function; receives the existing snapshot (ornull) and returns the snapshot to persist, ornullto declinestoreOpts- store options- Returns:
- the snapshot ID that was written, or
nullif the mutator declined
-
getSnapshot
Retrieves a session snapshot.When
GetSnapshotOptions.getSnapshotId()is set the store returns that specific snapshot. WhenGetSnapshotOptions.getSessionId()is set the store resolves and returns the latest leaf snapshot for that session. Returnsnullif no matching snapshot exists.By
snapshotId: loads the snapshot row and reconstructs its state from the checkpoint shards + orderedsegmentPathdiffs. BysessionId: reads the pointer, then loads and reconstructs the pointed snapshot.- Specified by:
getSnapshotin interfaceSnapshotReader<S>- Parameters:
opts- options specifying which snapshot to retrieve; exactly one ofsnapshotId/sessionIdmust be set- Returns:
- the matching snapshot, or
nullif none exists
-
onSnapshotStateChange
public AutoCloseable onSnapshotStateChange(String snapshotId, Consumer<SessionSnapshot<?>> cb, SessionStoreOptions storeOpts) Subscribes to status changes for the snapshot identified bysnapshotId.Invokes
cbimmediately with the current snapshot and again on every subsequent change. The returnedAutoCloseableunsubscribes when closed.DynamoDB has no built-in per-item change notification usable here, so the subscription polls
getSnapshot(com.google.genkit.ai.agent.GetSnapshotOptions)on a shared daemon scheduler and fires the callback whenever the serialized snapshot content changes. The callback also fires immediately if the snapshot already exists.- Specified by:
onSnapshotStateChangein interfaceSnapshotSubscriber- Parameters:
snapshotId- the snapshot to observecb- callback invoked with the updated snapshot on each changestoreOpts- store options- Returns:
- a handle that cancels the subscription when
AutoCloseable.close()is called
-