@@ -45,14 +45,8 @@ export function initializeVatState(kvStore, streamStore, vatID) {
45
45
kvStore . set ( `${ vatID } .d.nextID` , `${ FIRST_DEVICE_ID } ` ) ;
46
46
kvStore . set ( `${ vatID } .nextDeliveryNum` , `0` ) ;
47
47
kvStore . set ( `${ vatID } .incarnationNumber` , `1` ) ;
48
- kvStore . set (
49
- `${ vatID } .t.startPosition` ,
50
- `${ JSON . stringify ( streamStore . STREAM_START ) } ` ,
51
- ) ;
52
- kvStore . set (
53
- `${ vatID } .t.endPosition` ,
54
- `${ JSON . stringify ( streamStore . STREAM_START ) } ` ,
55
- ) ;
48
+ kvStore . set ( `${ vatID } .t.startPosition` , `${ streamStore . STREAM_START } ` ) ;
49
+ kvStore . set ( `${ vatID } .t.endPosition` , `${ streamStore . STREAM_START } ` ) ;
56
50
}
57
51
58
52
/**
@@ -486,9 +480,9 @@ export function makeVatKeeper(
486
480
*/
487
481
function * getTranscript ( startPos ) {
488
482
if ( startPos === undefined ) {
489
- startPos = JSON . parse ( getRequired ( `${ vatID } .t.startPosition` ) ) ;
483
+ startPos = Number ( getRequired ( `${ vatID } .t.startPosition` ) ) ;
490
484
}
491
- const endPos = JSON . parse ( getRequired ( `${ vatID } .t.endPosition` ) ) ;
485
+ const endPos = Number ( getRequired ( `${ vatID } .t.endPosition` ) ) ;
492
486
for ( const entry of streamStore . readStream (
493
487
transcriptStream ,
494
488
/** @type { StreamPosition } */ ( startPos ) ,
@@ -504,88 +498,34 @@ export function makeVatKeeper(
504
498
* @param {object } entry The transcript entry to append.
505
499
*/
506
500
function addToTranscript ( entry ) {
507
- const oldPos = JSON . parse ( getRequired ( `${ vatID } .t.endPosition` ) ) ;
501
+ const oldPos = Number ( getRequired ( `${ vatID } .t.endPosition` ) ) ;
508
502
const newPos = streamStore . writeStreamItem (
509
503
transcriptStream ,
510
504
JSON . stringify ( entry ) ,
511
505
oldPos ,
512
506
) ;
513
- kvStore . set ( `${ vatID } .t.endPosition` , `${ JSON . stringify ( newPos ) } ` ) ;
507
+ kvStore . set ( `${ vatID } .t.endPosition` , `${ newPos } ` ) ;
514
508
}
515
509
516
510
/** @returns {StreamPosition } */
517
511
function getTranscriptEndPosition ( ) {
518
- return JSON . parse (
512
+ const endPosition =
519
513
kvStore . get ( `${ vatID } .t.endPosition` ) ||
520
- assert . fail ( 'missing endPosition' ) ,
521
- ) ;
514
+ assert . fail ( 'missing endPosition' ) ;
515
+ return Number ( endPosition ) ;
522
516
}
523
517
524
- /**
525
- * @returns {{ snapshotID: string, startPos: StreamPosition } | undefined }
526
- */
527
- function getLastSnapshot ( ) {
528
- const notation = kvStore . get ( `local.${ vatID } .lastSnapshot` ) ;
529
- if ( ! notation ) {
530
- return undefined ;
531
- }
532
- const { snapshotID, startPos } = JSON . parse ( notation ) ;
533
- assert . typeof ( snapshotID , 'string' ) ;
534
- assert ( startPos ) ;
535
- return { snapshotID, startPos } ;
518
+ function getSnapshotInfo ( ) {
519
+ return snapStore ?. getSnapshotInfo ( vatID ) ;
536
520
}
537
521
538
522
function transcriptSnapshotStats ( ) {
539
523
const totalEntries = getTranscriptEndPosition ( ) ;
540
- const lastSnapshot = getLastSnapshot ( ) ;
541
- const snapshottedEntries = lastSnapshot ? lastSnapshot . startPos : 0 ;
524
+ const snapshotInfo = getSnapshotInfo ( ) ;
525
+ const snapshottedEntries = snapshotInfo ? snapshotInfo . endPos : 0 ;
542
526
return { totalEntries, snapshottedEntries } ;
543
527
}
544
528
545
- /**
546
- * Add vatID to consumers of a snapshot.
547
- *
548
- * @param {string } snapshotID
549
- */
550
- function addToSnapshot ( snapshotID ) {
551
- const key = `local.snapshot.${ snapshotID } ` ;
552
- const consumers = JSON . parse ( kvStore . get ( key ) || '[]' ) ;
553
- assert ( Array . isArray ( consumers ) ) ;
554
-
555
- // We can't completely rule out the possibility that
556
- // a vat will use the same snapshot twice in a row.
557
- //
558
- // PERFORMANCE NOTE: we assume consumer lists are short;
559
- // usually length 1. So O(n) search here is better
560
- // than keeping the list sorted.
561
- if ( ! consumers . includes ( vatID ) ) {
562
- consumers . push ( vatID ) ;
563
- kvStore . set ( key , JSON . stringify ( consumers ) ) ;
564
- // console.log('addToSnapshot result:', { vatID, snapshotID, consumers });
565
- }
566
- }
567
-
568
- /**
569
- * Remove vatID from consumers of a snapshot.
570
- *
571
- * @param {string } snapshotID
572
- */
573
- function removeFromSnapshot ( snapshotID ) {
574
- const key = `local.snapshot.${ snapshotID } ` ;
575
- const consumersJSON = kvStore . get ( key ) ;
576
- if ( ! consumersJSON ) {
577
- throw Fail `cannot remove ${ vatID } : ${ key } key not defined` ;
578
- }
579
- const consumers = JSON . parse ( consumersJSON ) ;
580
- assert ( Array . isArray ( consumers ) ) ;
581
- const ix = consumers . indexOf ( vatID ) ;
582
- assert ( ix >= 0 ) ;
583
- consumers . splice ( ix , 1 ) ;
584
- // console.log('removeFromSnapshot done:', { vatID, snapshotID, consumers });
585
- kvStore . set ( key , JSON . stringify ( consumers ) ) ;
586
- return consumers . length ;
587
- }
588
-
589
529
/**
590
530
* Store a snapshot, if given a snapStore.
591
531
*
@@ -597,30 +537,19 @@ export function makeVatKeeper(
597
537
return false ;
598
538
}
599
539
600
- const info = await manager . makeSnapshot ( snapStore ) ;
540
+ const endPosition = getTranscriptEndPosition ( ) ;
541
+ const info = await manager . makeSnapshot ( endPosition , snapStore ) ;
601
542
const {
602
- hash : snapshotID ,
543
+ hash,
603
544
rawByteCount,
604
545
rawSaveSeconds,
605
546
compressedByteCount,
606
547
compressSeconds,
607
548
} = info ;
608
- const old = getLastSnapshot ( ) ;
609
- if ( old && old . snapshotID !== snapshotID ) {
610
- if ( removeFromSnapshot ( old . snapshotID ) === 0 ) {
611
- snapStore . deleteSnapshot ( old . snapshotID ) ;
612
- }
613
- }
614
- const endPosition = getTranscriptEndPosition ( ) ;
615
- kvStore . set (
616
- `local.${ vatID } .lastSnapshot` ,
617
- JSON . stringify ( { snapshotID, startPos : endPosition } ) ,
618
- ) ;
619
- addToSnapshot ( snapshotID ) ;
620
549
kernelSlog . write ( {
621
550
type : 'heap-snapshot-save' ,
622
551
vatID,
623
- snapshotID ,
552
+ hash ,
624
553
rawByteCount,
625
554
rawSaveSeconds,
626
555
compressedByteCount,
@@ -630,17 +559,15 @@ export function makeVatKeeper(
630
559
return true ;
631
560
}
632
561
562
+ function deleteSnapshots ( ) {
563
+ if ( snapStore ) {
564
+ snapStore . deleteVatSnapshots ( vatID ) ;
565
+ }
566
+ }
567
+
633
568
function removeSnapshotAndTranscript ( ) {
634
- const skey = `local.${ vatID } .lastSnapshot` ;
635
569
if ( snapStore ) {
636
- const notation = kvStore . get ( skey ) ;
637
- if ( notation ) {
638
- const { snapshotID } = JSON . parse ( notation ) ;
639
- if ( removeFromSnapshot ( snapshotID ) === 0 ) {
640
- snapStore . deleteSnapshot ( snapshotID ) ;
641
- }
642
- kvStore . delete ( skey ) ;
643
- }
570
+ snapStore . deleteVatSnapshots ( vatID ) ;
644
571
}
645
572
646
573
const endPos = getRequired ( `${ vatID } .t.endPosition` ) ;
@@ -719,8 +646,8 @@ export function makeVatKeeper(
719
646
vatStats,
720
647
dumpState,
721
648
saveSnapshot,
722
- getLastSnapshot ,
723
- removeFromSnapshot ,
649
+ deleteSnapshots ,
650
+ getSnapshotInfo ,
724
651
removeSnapshotAndTranscript,
725
652
} ) ;
726
653
}
0 commit comments