@@ -20,7 +20,7 @@ import type {Cell, Value} from '../../@types/store/index.d.ts';
2020import { jsonStringWithUndefined } from '../../common/json.ts' ;
2121import { IdMap , mapNew , mapSet , mapToObj } from '../../common/map.ts' ;
2222import { objEnsure , objForEach } from '../../common/obj.ts' ;
23- import { noop , slice } from '../../common/other.ts' ;
23+ import { ifNotUndefined , noop , slice } from '../../common/other.ts' ;
2424import { stampNewWithHash , stampUpdate } from '../../common/stamps.ts' ;
2525import { EMPTY_STRING , T , V , strStartsWith } from '../../common/strings.ts' ;
2626import { createCustomPersister } from '../common/create.ts' ;
@@ -56,96 +56,53 @@ export const createDurableObjectStoragePersister = ((
5656 const getPersisted = async ( ) : Promise <
5757 PersistedContent < PersistsType . MergeableStoreOnly >
5858 > => {
59- // Initialize empty stamped objects for tables and values
6059 const tables : TablesStamp < true > = stampNewObjectWithHash ( ) ;
6160 const values : ValuesStamp < true > = stampNewObjectWithHash ( ) ;
62-
63- // Get all stored entries with the given prefix
64- const storedEntries = await storage . list < StoredValue > ( {
65- prefix : storagePrefix ,
66- } ) ;
67-
68- // Process each stored entry to reconstruct the persisted data structure
69- storedEntries . forEach ( ( [ zeroOrCellOrValue , time , hash ] , key ) => {
70- const keyParts = deconstructKey ( key ) ;
71- if ( ! keyParts ) return ;
72-
73- const [ type , ...ids ] = keyParts ;
74-
75- if ( type === T ) {
76- // Handle tables data
77- processTableEntry ( tables , ids , zeroOrCellOrValue , time , hash ) ;
78- } else if ( type === V ) {
79- // Handle values data
80- processValueEntry ( values , ids , zeroOrCellOrValue , time , hash ) ;
81- }
82- } ) ;
83-
61+ ( await storage . list < StoredValue > ( { prefix : storagePrefix } ) ) . forEach (
62+ async ( [ zeroOrCellOrValue , time , hash ] , key ) =>
63+ ifNotUndefined ( deconstructKey ( key ) , ( [ type , ...ids ] ) =>
64+ type == T
65+ ? ifNotUndefined (
66+ ids [ 0 ] ,
67+ ( tableId ) => {
68+ const table = objEnsure (
69+ tables [ 0 ] ,
70+ tableId ,
71+ stampNewObjectWithHash ,
72+ ) as TableStamp < true > ;
73+ ifNotUndefined (
74+ ids [ 1 ] ,
75+ ( rowId ) => {
76+ const row = objEnsure (
77+ table [ 0 ] ,
78+ rowId ,
79+ stampNewObjectWithHash ,
80+ ) as RowStamp < true > ;
81+ ifNotUndefined (
82+ ids [ 2 ] ,
83+ ( cellId ) =>
84+ ( row [ 0 ] [ cellId ] = [ zeroOrCellOrValue , time , hash ] ) ,
85+ ( ) => stampUpdate ( row , time , hash ) ,
86+ ) ;
87+ } ,
88+ ( ) => stampUpdate ( table , time , hash ) ,
89+ ) ;
90+ } ,
91+ ( ) => stampUpdate ( tables , time , hash ) ,
92+ )
93+ : type == V
94+ ? ifNotUndefined (
95+ ids [ 0 ] ,
96+ ( valueId ) =>
97+ ( values [ 0 ] [ valueId ] = [ zeroOrCellOrValue , time , hash ] ) ,
98+ ( ) => stampUpdate ( values , time , hash ) ,
99+ )
100+ : 0 ,
101+ ) ,
102+ ) ;
84103 return [ tables , values ] ;
85104 } ;
86105
87- // Helper function to process table-related storage entries
88- const processTableEntry = (
89- tables : TablesStamp < true > ,
90- ids : Ids ,
91- zeroOrCellOrValue : 0 | Cell | Value | undefined ,
92- time : string ,
93- hash : number ,
94- ) : void => {
95- const [ tableId , rowId , cellId ] = ids ;
96-
97- if ( tableId ) {
98- // Ensure table exists in the structure
99- const table = objEnsure (
100- tables [ 0 ] ,
101- tableId ,
102- stampNewObjectWithHash ,
103- ) as TableStamp < true > ;
104-
105- if ( rowId ) {
106- // Ensure row exists within the table
107- const row = objEnsure (
108- table [ 0 ] ,
109- rowId ,
110- stampNewObjectWithHash ,
111- ) as RowStamp < true > ;
112-
113- if ( cellId ) {
114- // Cell-level data: store the actual cell value
115- row [ 0 ] [ cellId ] = [ zeroOrCellOrValue , time , hash ] ;
116- } else {
117- // Row-level metadata: update the row's timestamp and hash
118- stampUpdate ( row , time , hash ) ;
119- }
120- } else {
121- // Table-level metadata: update the table's timestamp and hash
122- stampUpdate ( table , time , hash ) ;
123- }
124- } else {
125- // Tables-level metadata: update the root tables timestamp and hash
126- stampUpdate ( tables , time , hash ) ;
127- }
128- } ;
129-
130- // Helper function to process value-related storage entries
131- const processValueEntry = (
132- values : ValuesStamp < true > ,
133- ids : Ids ,
134- zeroOrCellOrValue : 0 | Cell | Value | undefined ,
135- time : string ,
136- hash : number ,
137- ) : void => {
138- const [ valueId ] = ids ;
139-
140- if ( valueId ) {
141- // Value-level data: store the actual value
142- values [ 0 ] [ valueId ] = [ zeroOrCellOrValue , time , hash ] ;
143- } else {
144- // Values-level metadata: update the root values timestamp and hash
145- stampUpdate ( values , time , hash ) ;
146- }
147- } ;
148-
149106 const setPersisted = async (
150107 getContent : ( ) => PersistedContent < PersistsType . MergeableStoreOnly > ,
151108 [
@@ -156,42 +113,25 @@ export const createDurableObjectStoragePersister = ((
156113 true
157114 > = getContent ( ) as any ,
158115 ) : Promise < void > => {
159- // Prepare a map to collect all storage entries that need to be persisted
160116 const keysToSet : IdMap < StoredValue > = mapNew ( ) ;
161-
162- // Store the root tables metadata (timestamp and hash)
163117 mapSet ( keysToSet , constructKey ( T ) , [ 0 , tablesHlc , tablesHash ] ) ;
164-
165- // Process each table in the store
166118 objForEach ( tablesObj , ( [ tableObj , tableHlc , tableHash ] , tableId ) => {
167- // Store table-level metadata
168119 mapSet ( keysToSet , constructKey ( T , tableId ) , [ 0 , tableHlc , tableHash ] ) ;
169-
170- // Process each row within the table
171120 objForEach ( tableObj , ( [ rowObj , rowHlc , rowHash ] , rowId ) => {
172- // Store row-level metadata
173121 mapSet ( keysToSet , constructKey ( T , tableId , rowId ) , [
174122 0 ,
175123 rowHlc ,
176124 rowHash ,
177125 ] ) ;
178-
179- // Store each cell value within the row
180126 objForEach ( rowObj , ( cellStamp , cellId ) =>
181127 mapSet ( keysToSet , constructKey ( T , tableId , rowId , cellId ) , cellStamp ) ,
182128 ) ;
183129 } ) ;
184130 } ) ;
185-
186- // Store the root values metadata (timestamp and hash)
187131 mapSet ( keysToSet , constructKey ( V ) , [ 0 , valuesHlc , valuesHash ] ) ;
188-
189- // Process each value in the store
190132 objForEach ( valuesObj , ( valueStamp , valueId ) =>
191133 mapSet ( keysToSet , constructKey ( V , valueId ) , valueStamp ) ,
192134 ) ;
193-
194- // Persist all collected entries to storage in a single batch operation
195135 await storage . put ( mapToObj ( keysToSet ) ) ;
196136 } ;
197137
0 commit comments