@@ -212,6 +212,21 @@ class MemoryBackend implements Backend {
212212 return this . searchByIndex ( key , this . repr . sortedReferencingClassIndex , params ) ;
213213 }
214214
215+ skipToObjectByClass ( className : string , startObject : Hash ) : Promise < string | undefined > {
216+ return this . skipToObjectByIndex ( className , this . repr . sortedClassIndex , startObject ) ;
217+ }
218+
219+ skipToObjectByReference ( referringPath : string , referencedHash : string , startObject : Hash ) : Promise < string | undefined > {
220+ let key = referringPath + '#' + referencedHash ;
221+ return this . skipToObjectByIndex ( key , this . repr . sortedReferenceIndex , startObject ) ;
222+ }
223+
224+ skipToObjectByReferencingClass ( referringClassName : string , referringPath : string , referencedHash : string , startObject : Hash ) : Promise < string | undefined > {
225+ let key = referringClassName + '.' + referringPath + '#' + referencedHash ;
226+ return this . skipToObjectByIndex ( key , this . repr . sortedReferencingClassIndex , startObject ) ;
227+
228+ }
229+
215230 async loadOpHeader ( opHash : string ) : Promise < StoredOpHeader | undefined > {
216231 return this . repr . opCausalHistories . get ( opHash ) ;
217232 }
@@ -220,32 +235,45 @@ class MemoryBackend implements Backend {
220235 return this . repr . opCausalHistoriesByHash . get ( causalHistoryHash ) ;
221236 }
222237
238+ private async skipToObjectByIndex ( key : string , sortedIndex : Map < string , Hash [ ] > , objectHash : Hash ) : Promise < string | undefined > {
239+
240+ let classHashes = sortedIndex . get ( key ) ;
241+
242+ if ( classHashes !== undefined ) {
243+ let idx = classHashes . indexOf ( objectHash ) ;
244+
245+ if ( idx >= 0 ) {
246+ return MemoryBackend . toStringIndex ( idx ) ;
247+ }
248+ }
249+
250+ return undefined ;
251+
252+ }
253+
223254 private async searchByIndex ( key : string , sortedIndex : Map < string , Hash [ ] > , params ?: BackendSearchParams | undefined ) : Promise < BackendSearchResults > {
224255
225256 let classHashes = sortedIndex . get ( key ) ;
226257
227-
228258 if ( classHashes === undefined ) {
229259 return { items : [ ] , start : '' , end : '' }
230260 } else {
231261 let order = ( params === undefined || params . order === undefined ) ? 'asc' : params . order . toLowerCase ( ) ;
232262
233263 let segment ;
234264
235-
236-
237265 if ( order === 'desc' ) {
238266 classHashes . reverse ( ) ;
239267 }
240268
241269 let start = 0 ;
242270
243271 if ( params !== undefined && params . start !== undefined ) {
244- start = Number . parseInt ( params . start ) ;
272+ start = MemoryBackend . fromStringIndex ( params . start ) ;
245273 }
246274
247275 if ( start >= classHashes . length ) {
248- return { items : [ ] , start : classHashes . length . toString ( ) , end : classHashes . length . toString ( ) }
276+ return { items : [ ] , start : MemoryBackend . toStringIndex ( classHashes . length ) , end : MemoryBackend . toStringIndex ( classHashes . length ) }
249277 }
250278
251279 let end = classHashes . length
@@ -256,12 +284,20 @@ class MemoryBackend implements Backend {
256284
257285 let result :Literal [ ] = segment . map ( ( hash : Hash ) => this . repr . objects . get ( hash ) ?. literal as Literal ) ;
258286
259- return { start : start . toString ( ) , end : end . toString ( ) , items : result } ;
287+ return { start : MemoryBackend . toStringIndex ( start ) , end : MemoryBackend . toStringIndex ( end ) , items : result } ;
260288
261289 }
262290
263291 }
264292
293+ private static toStringIndex ( idx : number ) : string {
294+ return idx . toString ( 16 ) . padStart ( 16 , '0' ) ;
295+ }
296+
297+ private static fromStringIndex ( idxString : string ) : number {
298+ return Number . parseInt ( idxString , 16 ) ;
299+ }
300+
265301 async storeCheckpoint ( checkpoint : StateCheckpoint ) : Promise < void > {
266302 this . repr . checkpoints . set ( checkpoint . mutableObject , checkpoint ) ;
267303 }
0 commit comments