1111- After N incremental checkpoints: compact into single full Checkpoint
1212"""
1313
14+ import json
1415from pathlib import Path
15- from typing import TYPE_CHECKING , Dict , List
16+ from typing import TYPE_CHECKING , Any , Dict , List
1617
1718from pydantic import BaseModel , ConfigDict , Field
1819
2930
3031CHECKPOINT_FILENAME_PREFIX = "checkpoint"
3132INCREMENTAL_CHECKPOINT_PREFIX = "incremental_checkpoint"
32- CHECKPOINT_VERSION = 1
33+ # Version 2 stores file content as base64 bytes; version 1 stored it as text.
34+ CHECKPOINT_VERSION = 2
35+ # An incremental checkpoint holds events, whose format did not change.
36+ INCREMENTAL_CHECKPOINT_VERSION = 1
3337
3438
35- def _check_version (version : int , kind : str ) -> None :
39+ def _check_version (version : int , latest : int , kind : str ) -> None :
3640 """Refuse a checkpoint from a later client."""
3741
3842 # A later client can change what a field holds while the object still parses.
3943 # The restore would then be wrong and silent. Every caller falls back to a
4044 # download of all events, so a refusal costs one slow cold start.
4145
42- if version > CHECKPOINT_VERSION :
46+ if version > latest :
4347 raise ValueError (
4448 f"This { kind } has version { version } , and this client reads up to "
45- f"version { CHECKPOINT_VERSION } ."
49+ f"version { latest } ."
4650 )
4751
4852
53+ def _upgrade_text_content (checkpoint : dict [str , Any ]) -> dict [str , Any ]:
54+ """A version 1 checkpoint as the current version: its text content as
55+ UTF-8 bytes. Version 1 held only text, so this gives the exact bytes."""
56+ files = [
57+ {** file , "content" : file ["content" ].encode ("utf-8" )}
58+ for file in checkpoint ["files" ]
59+ ]
60+ return {** checkpoint , "version" : CHECKPOINT_VERSION , "files" : files }
61+
62+
4963# Default compacting threshold: merge after this many incremental checkpoints
5064DEFAULT_COMPACTING_THRESHOLD = 4
5165
@@ -175,11 +189,14 @@ def as_compressed_data(self) -> bytes:
175189
176190 @classmethod
177191 def from_compressed_data (cls , data : bytes ) -> "Checkpoint" :
178- """Load checkpoint from compressed data."""
192+ """Load checkpoint from compressed data. A version 1 checkpoint, which
193+ held text content, loads as the current version."""
179194 uncompressed_data = uncompress_data (data )
180- checkpoint = cls .model_validate_json (uncompressed_data )
181- _check_version (checkpoint .version , "checkpoint" )
182- return checkpoint
195+ checkpoint = json .loads (uncompressed_data )
196+ _check_version (checkpoint ["version" ], CHECKPOINT_VERSION , "checkpoint" )
197+ if checkpoint ["version" ] <= 1 :
198+ return cls .model_validate (_upgrade_text_content (checkpoint ))
199+ return cls .model_validate_json (uncompressed_data )
183200
184201
185202class IncrementalCheckpoint (BaseModel ):
@@ -196,7 +213,7 @@ class IncrementalCheckpoint(BaseModel):
196213 - After compacting: single full Checkpoint with merged state
197214 """
198215
199- version : int = CHECKPOINT_VERSION
216+ version : int = INCREMENTAL_CHECKPOINT_VERSION
200217 timestamp : float = Field (default_factory = create_event_timestamp )
201218 email : str
202219 sequence_number : int # 1, 2, 3, ... (which checkpoint this is)
@@ -236,7 +253,9 @@ def from_compressed_data(cls, data: bytes) -> "IncrementalCheckpoint":
236253 """Load from compressed data."""
237254 uncompressed_data = uncompress_data (data )
238255 checkpoint = cls .model_validate_json (uncompressed_data )
239- _check_version (checkpoint .version , "incremental checkpoint" )
256+ _check_version (
257+ checkpoint .version , INCREMENTAL_CHECKPOINT_VERSION , "incremental checkpoint"
258+ )
240259 return checkpoint
241260
242261
0 commit comments