@@ -38,48 +38,23 @@ export class FileSystemGlobalState {
3838 // Ensure storage directories exist synchronously during initialization
3939 ensureDirectoryExistsSync ( getActorsDir ( this . #storagePath) ) ;
4040
41- // Load all actors into cache synchronously
42- this . #loadAllActorsIntoCache( ) ;
43-
44- logger ( ) . info ( "file system loaded" , {
45- dir : this . #storagePath,
46- actorCount : this . #stateCache. size ,
47- } ) ;
48- }
49-
50- /**
51- * Load all actors into the state cache from the file system
52- * Only called once during initialization
53- */
54- #loadAllActorsIntoCache( ) : void {
5541 const actorsDir = getActorsDir ( this . #storagePath) ;
56-
42+ let actorCount = 0 ;
43+
5744 try {
58- // HACK: Use synchronous filesystem operations for initialization
5945 const actorIds = fsSync . readdirSync ( actorsDir ) ;
60-
61- for ( const actorId of actorIds ) {
62- const stateFilePath = getActorDataPath ( this . #storagePath, actorId ) ;
63-
64- if ( fsSync . existsSync ( stateFilePath ) ) {
65- try {
66- const stateData = fsSync . readFileSync ( stateFilePath ) ;
67- const state = cbor . decode ( stateData ) as ActorState ;
68-
69- this . #stateCache. set ( actorId , state ) ;
70- } catch ( error ) {
71- logger ( ) . error (
72- "failed to read actor state during cache initialization" ,
73- { actorId, error } ,
74- ) ;
75- }
76- }
77- }
46+ actorCount = actorIds . length ;
7847 } catch ( error ) {
79- logger ( ) . error ( "failed to load actors into cache " , { error } ) ;
48+ logger ( ) . error ( "failed to count actors" , { error } ) ;
8049 }
50+
51+ logger ( ) . info ( "file system loaded" , {
52+ dir : this . #storagePath,
53+ actorCount,
54+ } ) ;
8155 }
8256
57+
8358 /**
8459 * Get the current storage directory path
8560 */
@@ -88,16 +63,34 @@ export class FileSystemGlobalState {
8863 }
8964
9065 /**
91- * Load actor state from cache
66+ * Load actor state from cache or disk (lazy loading)
9267 */
9368 loadActorState ( actorId : string ) : ActorState {
94- this . ensureActorExists ( actorId ) ;
95-
96- // Get actor state from cache
69+ // Check if already in cache
9770 const cachedActor = this . #stateCache. get ( actorId ) ;
98- invariant ( cachedActor , `actor state should exist in cache for ${ actorId } ` ) ;
71+ if ( cachedActor ) {
72+ return cachedActor ;
73+ }
9974
100- return cachedActor ;
75+ // Try to load from disk
76+ const stateFilePath = getActorDataPath ( this . #storagePath, actorId ) ;
77+
78+ if ( ! fsSync . existsSync ( stateFilePath ) ) {
79+ throw new Error ( `Actor does not exist for ID: ${ actorId } ` ) ;
80+ }
81+
82+ try {
83+ const stateData = fsSync . readFileSync ( stateFilePath ) ;
84+ const state = cbor . decode ( stateData ) as ActorState ;
85+
86+ // Cache the loaded state
87+ this . #stateCache. set ( actorId , state ) ;
88+
89+ return state ;
90+ } catch ( error ) {
91+ logger ( ) . error ( "failed to load actor state" , { actorId, error } ) ;
92+ throw new Error ( `Failed to load actor state: ${ error } ` ) ;
93+ }
10194 }
10295
10396 /**
@@ -135,29 +128,27 @@ export class FileSystemGlobalState {
135128 // Write data
136129 const serializedState = cbor . encode ( state ) ;
137130 await fs . writeFile ( dataPath , serializedState ) ;
138- console . log ( "saving state" , dataPath ) ;
139131 } catch ( error ) {
140132 logger ( ) . error ( "failed to save actor state" , { actorId, error } ) ;
141133 throw new Error ( `Failed to save actor state: ${ error } ` ) ;
142134 }
143135 }
144136
145137 /**
146- * Check if a actor exists in the cache
138+ * Check if a actor exists in cache or on disk
147139 */
148140 hasActor ( actorId : string ) : boolean {
149- return this . #stateCache. has ( actorId ) ;
150- }
151-
152- /**
153- * Ensure a actor exists, throwing if it doesn't
154- */
155- ensureActorExists ( actorId : string ) : void {
156- if ( ! this . hasActor ( actorId ) ) {
157- throw new Error ( `Actor does not exist for ID: ${ actorId } ` ) ;
141+ // Check cache first
142+ if ( this . #stateCache. has ( actorId ) ) {
143+ return true ;
158144 }
145+
146+ // Check if file exists on disk
147+ const stateFilePath = getActorDataPath ( this . #storagePath, actorId ) ;
148+ return fsSync . existsSync ( stateFilePath ) ;
159149 }
160150
151+
161152 /**
162153 * Create a actor
163154 */
@@ -186,49 +177,4 @@ export class FileSystemGlobalState {
186177 // Save to disk
187178 await this . saveActorState ( actorId ) ;
188179 }
189-
190- /**
191- * Find actor by name and key
192- */
193- findActorByNameAndKey ( name : string , key : ActorKey ) : ActorState | undefined {
194- // NOTE: This is a slow implementation that checks each actor individually.
195- // This can be optimized with an index in the future.
196-
197- return this . findActor ( ( actor ) => {
198- if ( actor . name !== name ) return false ;
199-
200- // If actor doesn't have a key, it's not a match
201- if ( ! actor . key || actor . key . length !== key . length ) {
202- return false ;
203- }
204-
205- // Check if all elements in key are in actor.key
206- for ( let i = 0 ; i < key . length ; i ++ ) {
207- if ( key [ i ] !== actor . key [ i ] ) {
208- return false ;
209- }
210- }
211- return true ;
212- } ) ;
213- }
214-
215- /**
216- * Find actor by filter function
217- */
218- findActor ( filter : ( actor : ActorState ) => boolean ) : ActorState | undefined {
219- for ( const actor of this . #stateCache. values ( ) ) {
220- if ( filter ( actor ) ) {
221- return actor ;
222- }
223- }
224- return undefined ;
225- }
226-
227- /**
228- * Get all actors from the cache
229- */
230- getAllActors ( ) : ActorState [ ] {
231- // Return all actors from the cache
232- return Array . from ( this . #stateCache. values ( ) ) ;
233- }
234180}
0 commit comments