@@ -212,6 +212,21 @@ class MemoryBackend implements Backend {
212
212
return this . searchByIndex ( key , this . repr . sortedReferencingClassIndex , params ) ;
213
213
}
214
214
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
+
215
230
async loadOpHeader ( opHash : string ) : Promise < StoredOpHeader | undefined > {
216
231
return this . repr . opCausalHistories . get ( opHash ) ;
217
232
}
@@ -220,32 +235,45 @@ class MemoryBackend implements Backend {
220
235
return this . repr . opCausalHistoriesByHash . get ( causalHistoryHash ) ;
221
236
}
222
237
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
+
223
254
private async searchByIndex ( key : string , sortedIndex : Map < string , Hash [ ] > , params ?: BackendSearchParams | undefined ) : Promise < BackendSearchResults > {
224
255
225
256
let classHashes = sortedIndex . get ( key ) ;
226
257
227
-
228
258
if ( classHashes === undefined ) {
229
259
return { items : [ ] , start : '' , end : '' }
230
260
} else {
231
261
let order = ( params === undefined || params . order === undefined ) ? 'asc' : params . order . toLowerCase ( ) ;
232
262
233
263
let segment ;
234
264
235
-
236
-
237
265
if ( order === 'desc' ) {
238
266
classHashes . reverse ( ) ;
239
267
}
240
268
241
269
let start = 0 ;
242
270
243
271
if ( params !== undefined && params . start !== undefined ) {
244
- start = Number . parseInt ( params . start ) ;
272
+ start = MemoryBackend . fromStringIndex ( params . start ) ;
245
273
}
246
274
247
275
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 ) }
249
277
}
250
278
251
279
let end = classHashes . length
@@ -256,12 +284,20 @@ class MemoryBackend implements Backend {
256
284
257
285
let result :Literal [ ] = segment . map ( ( hash : Hash ) => this . repr . objects . get ( hash ) ?. literal as Literal ) ;
258
286
259
- return { start : start . toString ( ) , end : end . toString ( ) , items : result } ;
287
+ return { start : MemoryBackend . toStringIndex ( start ) , end : MemoryBackend . toStringIndex ( end ) , items : result } ;
260
288
261
289
}
262
290
263
291
}
264
292
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
+
265
301
async storeCheckpoint ( checkpoint : StateCheckpoint ) : Promise < void > {
266
302
this . repr . checkpoints . set ( checkpoint . mutableObject , checkpoint ) ;
267
303
}
0 commit comments