Package com.tnsai.state
Class FileCheckpointer
java.lang.Object
com.tnsai.state.FileCheckpointer
- All Implemented Interfaces:
Checkpointer
File-based Checkpointer with JSON serialization.
Features:
- JSON serialization with Jackson
- Atomic writes (write to temp then rename)
- File rotation (configurable max checkpoints per thread)
- Optional GZIP compression
- Thread-safe with ReadWriteLock
Directory Structure:
baseDir/
thread-{threadId}/
checkpoint-{timestamp}-{checkpointId}.json[.gz]
writes-{checkpointId}.json[.gz]
Example usage:
// Basic usage
FileCheckpointer checkpointer = new FileCheckpointer(Path.of("./checkpoints"));
// With compression and rotation
FileCheckpointer checkpointer = FileCheckpointer.builder()
.baseDir(Path.of("./checkpoints"))
.enableCompression(true)
.maxCheckpointsPerThread(100)
.prettyPrint(true)
.build();
// Save checkpoint
checkpointer.put("thread-1", "cp-001",
Map.of("messages", List.of("hello")),
Map.of("step", 1));
// Retrieve latest
CheckpointTuple latest = checkpointer.getTuple("thread-1", null);
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for FileCheckpointer.Nested classes/interfaces inherited from interface com.tnsai.state.Checkpointer
Checkpointer.CheckpointTuple, Checkpointer.Write -
Constructor Summary
ConstructorsConstructorDescriptionFileCheckpointer(Path baseDir) Creates a FileCheckpointer with default settings.FileCheckpointer(Path baseDir, boolean enableCompression, int maxCheckpointsPerThread, boolean prettyPrint) Creates a FileCheckpointer with custom settings. -
Method Summary
Modifier and TypeMethodDescriptionstatic FileCheckpointer.Builderbuilder()Returns a builder for FileCheckpointer.voidclear()Clear all checkpoints.voidDelete checkpoints for a thread.intGet the total number of checkpoint files.longGet the total size of all checkpoint files in bytes.Fetch a checkpoint.List checkpoints for a thread.voidStore a checkpoint.voidputWrites(String checkpointId, List<Checkpointer.Write> writes) Store intermediate writes.
-
Constructor Details
-
FileCheckpointer
Creates a FileCheckpointer with default settings.- Parameters:
baseDir- base directory for checkpoint storage
-
FileCheckpointer
public FileCheckpointer(Path baseDir, boolean enableCompression, int maxCheckpointsPerThread, boolean prettyPrint) Creates a FileCheckpointer with custom settings.- Parameters:
baseDir- base directory for checkpoint storageenableCompression- whether to GZIP compress filesmaxCheckpointsPerThread- max checkpoints to keep per thread (0 = unlimited)prettyPrint- whether to pretty-print JSON
-
-
Method Details
-
builder
Returns a builder for FileCheckpointer.- Returns:
- new builder instance
-
put
public void put(String threadId, String checkpointId, Map<String, Object> state, Map<String, Object> metadata) Description copied from interface:CheckpointerStore a checkpoint.- Specified by:
putin interfaceCheckpointer- Parameters:
threadId- Thread identifiercheckpointId- Checkpoint identifierstate- State datametadata- Checkpoint metadata
-
putWrites
Description copied from interface:CheckpointerStore intermediate writes.- Specified by:
putWritesin interfaceCheckpointer- Parameters:
checkpointId- Checkpoint identifierwrites- List of writes
-
getTuple
Description copied from interface:CheckpointerFetch a checkpoint.- Specified by:
getTuplein interfaceCheckpointer- Parameters:
threadId- Thread identifiercheckpointId- Checkpoint identifier (null for latest)- Returns:
- Checkpoint tuple
-
list
Description copied from interface:CheckpointerList checkpoints for a thread.- Specified by:
listin interfaceCheckpointer- Parameters:
threadId- Thread identifier- Returns:
- List of checkpoints
-
delete
Description copied from interface:CheckpointerDelete checkpoints for a thread.- Specified by:
deletein interfaceCheckpointer- Parameters:
threadId- Thread identifier
-
getCheckpointCount
public int getCheckpointCount()Get the total number of checkpoint files.- Returns:
- total checkpoint count
-
getTotalSize
public long getTotalSize()Get the total size of all checkpoint files in bytes.- Returns:
- total size in bytes
-
clear
public void clear()Clear all checkpoints.
-