5
5
assertKeyPattern ,
6
6
assertPattern ,
7
7
matches ,
8
+ fit ,
8
9
compareRank ,
9
10
M ,
10
11
zeroPad ,
@@ -13,6 +14,8 @@ import {
13
14
isEncodedRemotable ,
14
15
makeCopySet ,
15
16
makeCopyMap ,
17
+ mustCompress ,
18
+ decompress ,
16
19
} from '@agoric/store' ;
17
20
import { Far , passStyleOf } from '@endo/marshal' ;
18
21
import { decodeToJustin } from '@endo/marshal/src/marshal-justin.js' ;
@@ -213,7 +216,7 @@ export function makeCollectionManager(
213
216
return storeKindInfo [ kindName ] . kindID ;
214
217
}
215
218
216
- // Not that it's only used for this purpose, what should it be called?
219
+ // Now that it's only used for this purpose, what should it be called?
217
220
// TODO Should we be using the new encodeBigInt scheme instead, anyway?
218
221
const BIGINT_TAG_LEN = 10 ;
219
222
@@ -257,6 +260,23 @@ export function makeCollectionManager(
257
260
const dbKeyPrefix = `vc.${ collectionID } .` ;
258
261
let currentGenerationNumber = 0 ;
259
262
263
+ const keyLabel = `invalid key type for collection ${ q ( label ) } ` ;
264
+ const valueLabel = `invalid value type for collection ${ q ( label ) } ` ;
265
+
266
+ const serializeValue = value => {
267
+ if ( valueShape === undefined ) {
268
+ return serialize ( value ) ;
269
+ }
270
+ return serialize ( mustCompress ( value , valueShape , valueLabel ) ) ;
271
+ } ;
272
+
273
+ const unserializeValue = data => {
274
+ if ( valueShape === undefined ) {
275
+ return unserialize ( data ) ;
276
+ }
277
+ return decompress ( unserialize ( data ) , valueShape ) ;
278
+ } ;
279
+
260
280
function prefix ( dbEntryKey ) {
261
281
return `${ dbKeyPrefix } ${ dbEntryKey } ` ;
262
282
}
@@ -331,11 +351,10 @@ export function makeCollectionManager(
331
351
}
332
352
333
353
function get ( key ) {
334
- matches ( key , keyShape ) ||
335
- assert . fail ( X `invalid key type for collection ${ q ( label ) } ` ) ;
354
+ fit ( key , keyShape , keyLabel ) ;
336
355
const result = syscall . vatstoreGet ( keyToDBKey ( key ) ) ;
337
356
if ( result ) {
338
- return unserialize ( JSON . parse ( result ) ) ;
357
+ return unserializeValue ( JSON . parse ( result ) ) ;
339
358
}
340
359
assert . fail ( X `key ${ key } not found in collection ${ q ( label ) } ` ) ;
341
360
}
@@ -351,16 +370,11 @@ export function makeCollectionManager(
351
370
}
352
371
353
372
function init ( key , value ) {
354
- matches ( key , keyShape ) ||
355
- assert . fail ( X `invalid key type for collection ${ q ( label ) } ` ) ;
373
+ fit ( key , keyShape , keyLabel ) ;
356
374
! has ( key ) ||
357
375
assert . fail ( X `key ${ key } already registered in collection ${ q ( label ) } ` ) ;
358
- if ( valueShape ) {
359
- matches ( value , valueShape ) ||
360
- assert . fail ( X `invalid value type for collection ${ q ( label ) } ` ) ;
361
- }
376
+ const serializedValue = serializeValue ( value ) ;
362
377
currentGenerationNumber += 1 ;
363
- const serializedValue = serialize ( value ) ;
364
378
assertAcceptableSyscallCapdataSize ( [ serializedValue ] ) ;
365
379
if ( durable ) {
366
380
serializedValue . slots . forEach ( ( vref , slotIndex ) => {
@@ -388,13 +402,8 @@ export function makeCollectionManager(
388
402
}
389
403
390
404
function set ( key , value ) {
391
- matches ( key , keyShape ) ||
392
- assert . fail ( X `invalid key type for collection ${ q ( label ) } ` ) ;
393
- if ( valueShape ) {
394
- matches ( value , valueShape ) ||
395
- assert . fail ( X `invalid value type for collection ${ q ( label ) } ` ) ;
396
- }
397
- const after = serialize ( harden ( value ) ) ;
405
+ fit ( key , keyShape , keyLabel ) ;
406
+ const after = serializeValue ( harden ( value ) ) ;
398
407
assertAcceptableSyscallCapdataSize ( [ after ] ) ;
399
408
if ( durable ) {
400
409
after . slots . forEach ( ( vref , i ) => {
@@ -412,8 +421,7 @@ export function makeCollectionManager(
412
421
}
413
422
414
423
function deleteInternal ( key ) {
415
- matches ( key , keyShape ) ||
416
- assert . fail ( X `invalid key type for collection ${ q ( label ) } ` ) ;
424
+ fit ( key , keyShape , keyLabel ) ;
417
425
const dbKey = keyToDBKey ( key ) ;
418
426
const rawValue = syscall . vatstoreGet ( dbKey ) ;
419
427
assert ( rawValue , X `key ${ key } not found in collection ${ q ( label ) } ` ) ;
@@ -472,7 +480,7 @@ export function makeCollectionManager(
472
480
if ( dbKey < end ) {
473
481
priorDBKey = dbKey ;
474
482
if ( ignoreKeys ) {
475
- const value = unserialize ( JSON . parse ( dbValue ) ) ;
483
+ const value = unserializeValue ( JSON . parse ( dbValue ) ) ;
476
484
if ( matches ( value , valuePatt ) ) {
477
485
yield [ undefined , value ] ;
478
486
}
@@ -484,7 +492,7 @@ export function makeCollectionManager(
484
492
} else {
485
493
const key = dbKeyToKey ( dbKey ) ;
486
494
if ( matches ( key , keyPatt ) ) {
487
- const value = unserialize ( JSON . parse ( dbValue ) ) ;
495
+ const value = unserializeValue ( JSON . parse ( dbValue ) ) ;
488
496
if ( matches ( value , valuePatt ) ) {
489
497
yield [ key , value ] ;
490
498
}
0 commit comments