11import { ThreadId } from "@t3tools/contracts" ;
22import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient" ;
3- import * as Clock from "effect/Clock" ;
43import * as Context from "effect/Context" ;
54import * as DateTime from "effect/DateTime" ;
65import * as Effect from "effect/Effect" ;
@@ -33,7 +32,7 @@ export class ProjectionMaintenanceError extends Schema.TaggedError<ProjectionMai
3332export interface ProjectionMaintenanceV2Shape {
3433 readonly verify : Effect . Effect < ProjectionVerificationV2 , ProjectionMaintenanceError > ;
3534 readonly rebuild : Effect . Effect < ProjectionVerificationV2 , ProjectionMaintenanceError > ;
36- readonly compactEventStore : Effect . Effect <
35+ readonly compactEventStore : ( betweenPages ?: Effect . Effect < void > ) => Effect . Effect <
3736 {
3837 readonly deletedEventCount : number ;
3938 readonly deletedReceiptCount : number ;
@@ -231,7 +230,6 @@ export const layer: Layer.Layer<
231230 // Bound each reader request and writer delete, including when the reader
232231 // worker is unavailable and discovery falls back to the writer.
233232 const COMPACTION_PAGE_SIZE = 50 ;
234- const COMPACTION_SLICE_NANOS = 25_000_000n ;
235233
236234 /**
237235 * Scan newest first to retain the newest state for each entity. Keyed
@@ -243,17 +241,9 @@ export const layer: Layer.Layer<
243241 * still scan the entire history when superseded events are sparse.
244242 * turn-item.updated stays intact because replay assigns positions on first write.
245243 */
246- const compactEventStore = Effect . gen ( function * ( ) {
247- let sliceStarted = yield * Clock . monotonicTimeNanos ;
248- const pause = Effect . gen ( function * ( ) {
249- const elapsed = ( yield * Clock . monotonicTimeNanos ) - sliceStarted ;
250- if ( elapsed < COMPACTION_SLICE_NANOS ) return ;
251- // Spend at most 10% of wall time compacting. A slow page can exceed
252- // the slice budget; repay its entire elapsed time before the next page.
253- // Keep both cursors and all retention state in this fiber across rests.
254- yield * Effect . sleep ( ( Number ( elapsed ) / 1_000_000 ) * 9 ) ;
255- sliceStarted = yield * Clock . monotonicTimeNanos ;
256- } ) ;
244+ const compactEventStore = Effect . fnUntraced ( function * (
245+ betweenPages : Effect . Effect < void > = Effect . void ,
246+ ) {
257247 const bounds = yield * sql < {
258248 readonly event_sequence : number ;
259249 readonly receipt_row_id : number ;
@@ -311,10 +301,11 @@ export const layer: Layer.Layer<
311301 FROM orchestration_events AS event
312302 WHERE event.sequence <= ${ throughSequence }
313303 ORDER BY event.sequence DESC
314- LIMIT ${ COMPACTION_PAGE_SIZE }
304+ LIMIT ${ COMPACTION_PAGE_SIZE + 1 }
315305 ` . pipe ( NodeSqliteClient . readOnly ) ;
306+ const page = rows . slice ( 0 , COMPACTION_PAGE_SIZE ) ;
316307 const obsolete : number [ ] = [ ] ;
317- for ( const row of rows ) {
308+ for ( const row of page ) {
318309 if ( row . imported_legacy_thread === 1 ) {
319310 obsolete . push ( row . sequence ) ;
320311 } else if ( row . application_event_version === 2 ) {
@@ -362,9 +353,9 @@ export const layer: Layer.Layer<
362353 deletedEventCount += obsolete . length ;
363354 yield * Effect . yieldNow ;
364355 }
365- throughSequence = ( rows . at ( - 1 ) ?. sequence ?? 1 ) - 1 ;
366- yield * pause ;
367- if ( rows . length < COMPACTION_PAGE_SIZE ) break ;
356+ throughSequence =
357+ rows . length <= COMPACTION_PAGE_SIZE ? 0 : ( page . at ( - 1 ) ?. sequence ?? 1 ) - 1 ;
358+ if ( throughSequence > 0 || throughReceiptRowId > 0 ) yield * betweenPages ;
368359 }
369360
370361 while ( throughReceiptRowId > 0 ) {
@@ -402,7 +393,7 @@ export const layer: Layer.Layer<
402393 }
403394 throughReceiptRowId = ( page . at ( - 1 ) ?. row_id ?? 1 ) - 1 ;
404395 if ( rows . length <= COMPACTION_PAGE_SIZE ) break ;
405- yield * pause ;
396+ yield * betweenPages ;
406397 }
407398
408399 const freelistRows = yield * sql < { readonly freelist_count : number } > `PRAGMA freelist_count` ;
@@ -411,12 +402,12 @@ export const layer: Layer.Layer<
411402 ( freelistRows [ 0 ] ?. freelist_count ?? 0 ) * ( pageSizeRows [ 0 ] ?. page_size ?? 0 ) ;
412403
413404 return { deletedEventCount, deletedReceiptCount, reclaimableBytes } ;
414- } ) ;
405+ } , mapError ( "compact event store" ) ) ;
415406
416407 return ProjectionMaintenanceV2 . of ( {
417408 verify : mapError ( "verify" ) ( verify ) ,
418409 rebuild : mapError ( "rebuild" ) ( rebuild ) ,
419- compactEventStore : mapError ( "compact event store" ) ( compactEventStore ) ,
410+ compactEventStore,
420411 } ) ;
421412 } ) ,
422413) ;
0 commit comments