@@ -42,7 +42,6 @@ class DB {
42
42
crypto ?: {
43
43
key : Buffer ;
44
44
ops : Crypto ;
45
- canary ?: Buffer ;
46
45
} ;
47
46
fs ?: FileSystem ;
48
47
logger ?: Logger ;
@@ -160,11 +159,16 @@ class DB {
160
159
public transaction ( ) : ResourceAcquire < DBTransaction > {
161
160
return async ( ) => {
162
161
const transactionId = this . transactionCounter ++ ;
163
- const tran = await DBTransaction . createTransaction ( {
162
+ const tran = new DBTransaction ( {
164
163
db : this ,
165
164
transactionId,
166
165
logger : this . logger ,
167
166
} ) ;
167
+ // const tran = await DBTransaction.createTransaction({
168
+ // db: this,
169
+ // transactionId,
170
+ // logger: this.logger,
171
+ // });
168
172
return [
169
173
async ( e ?: Error ) => {
170
174
try {
@@ -216,7 +220,7 @@ class DB {
216
220
public async get < T > (
217
221
keyPath : KeyPath | string | Buffer ,
218
222
raw : boolean = false ,
219
- ) : Promise < T | undefined > {
223
+ ) : Promise < T | Buffer | undefined > {
220
224
if ( ! Array . isArray ( keyPath ) ) {
221
225
keyPath = [ keyPath ] as KeyPath ;
222
226
}
@@ -400,22 +404,57 @@ class DB {
400
404
levelPath ?: LevelPath ,
401
405
) : DBIterator < undefined , undefined > ;
402
406
public iterator (
403
- options : AbstractIteratorOptions & { keys : false } ,
407
+ options : AbstractIteratorOptions & { values : false ; keyAsBuffer ?: true } ,
404
408
levelPath ?: LevelPath ,
405
- ) : DBIterator < undefined , Buffer > ;
409
+ ) : DBIterator < Buffer , undefined > ;
406
410
public iterator (
407
- options : AbstractIteratorOptions & { values : false } ,
411
+ options : AbstractIteratorOptions & { values : false ; keyAsBuffer : false } ,
408
412
levelPath ?: LevelPath ,
409
- ) : DBIterator < Buffer , undefined > ;
413
+ ) : DBIterator < string , undefined > ;
410
414
public iterator (
411
- options ?: AbstractIteratorOptions ,
415
+ options : AbstractIteratorOptions & { keys : false ; valueAsBuffer ?: true } ,
416
+ levelPath ?: LevelPath ,
417
+ ) : DBIterator < undefined , Buffer > ;
418
+ public iterator < V > (
419
+ options : AbstractIteratorOptions & { keys : false ; valueAsBuffer : false } ,
420
+ levelPath ?: LevelPath ,
421
+ ) : DBIterator < undefined , V > ;
422
+ public iterator (
423
+ options ?: AbstractIteratorOptions & {
424
+ keyAsBuffer ?: true ;
425
+ valueAsBuffer ?: true ;
426
+ } ,
412
427
levelPath ?: LevelPath ,
413
428
) : DBIterator < Buffer , Buffer > ;
414
- @ready ( new errors . ErrorDBNotRunning ( ) )
415
429
public iterator (
416
- options ?: AbstractIteratorOptions ,
430
+ options ?: AbstractIteratorOptions & {
431
+ keyAsBuffer : false ;
432
+ valueAsBuffer ?: true ;
433
+ } ,
434
+ levelPath ?: LevelPath ,
435
+ ) : DBIterator < string , Buffer > ;
436
+ public iterator < V > (
437
+ options ?: AbstractIteratorOptions & {
438
+ keyAsBuffer ?: true ;
439
+ valueAsBuffer : false ;
440
+ } ,
441
+ levelPath ?: LevelPath ,
442
+ ) : DBIterator < Buffer , V > ;
443
+ public iterator < V > (
444
+ options ?: AbstractIteratorOptions & {
445
+ keyAsBuffer : false ;
446
+ valueAsBuffer : false ;
447
+ } ,
448
+ levelPath ?: LevelPath ,
449
+ ) : DBIterator < string , V > ;
450
+ @ready ( new errors . ErrorDBNotRunning ( ) )
451
+ public iterator < V > (
452
+ options ?: AbstractIteratorOptions & {
453
+ keyAsBuffer ?: any ;
454
+ valueAsBuffer ?: any ;
455
+ } ,
417
456
levelPath : LevelPath = [ ] ,
418
- ) : DBIterator {
457
+ ) : DBIterator < Buffer | string | undefined , Buffer | V | undefined > {
419
458
levelPath = [ 'data' , ...levelPath ] ;
420
459
return this . _iterator ( options , levelPath ) ;
421
460
}
@@ -432,29 +471,76 @@ class DB {
432
471
* @internal
433
472
*/
434
473
public _iterator (
435
- options : AbstractIteratorOptions & { keys : false } ,
474
+ options : AbstractIteratorOptions & { values : false ; keyAsBuffer ?: true } ,
436
475
levelPath ?: LevelPath ,
437
- ) : DBIterator < undefined , Buffer > ;
476
+ ) : DBIterator < Buffer , undefined > ;
438
477
/**
439
478
* @internal
440
479
*/
441
480
public _iterator (
442
- options : AbstractIteratorOptions & { values : false } ,
481
+ options : AbstractIteratorOptions & { values : false ; keyAsBuffer : false } ,
443
482
levelPath ?: LevelPath ,
444
- ) : DBIterator < Buffer , undefined > ;
483
+ ) : DBIterator < string , undefined > ;
445
484
/**
446
485
* @internal
447
486
*/
448
487
public _iterator (
449
- options ?: AbstractIteratorOptions ,
488
+ options : AbstractIteratorOptions & { keys : false ; valueAsBuffer ?: true } ,
489
+ levelPath ?: LevelPath ,
490
+ ) : DBIterator < undefined , Buffer > ;
491
+ /**
492
+ * @internal
493
+ */
494
+ public _iterator < V > (
495
+ options : AbstractIteratorOptions & { keys : false ; valueAsBuffer : false } ,
496
+ levelPath ?: LevelPath ,
497
+ ) : DBIterator < undefined , V > ;
498
+ /**
499
+ * @internal
500
+ */
501
+ public _iterator (
502
+ options ?: AbstractIteratorOptions & {
503
+ keyAsBuffer ?: true ;
504
+ valueAsBuffer ?: true ;
505
+ } ,
450
506
levelPath ?: LevelPath ,
451
507
) : DBIterator < Buffer , Buffer > ;
508
+ /**
509
+ * @internal
510
+ */
511
+ public _iterator (
512
+ options ?: AbstractIteratorOptions & {
513
+ keyAsBuffer : false ;
514
+ valueAsBuffer ?: true ;
515
+ } ,
516
+ levelPath ?: LevelPath ,
517
+ ) : DBIterator < string , Buffer > ;
518
+ /**
519
+ * @internal
520
+ */
521
+ public _iterator < V > (
522
+ options ?: AbstractIteratorOptions & {
523
+ keyAsBuffer ?: true ;
524
+ valueAsBuffer : false ;
525
+ } ,
526
+ levelPath ?: LevelPath ,
527
+ ) : DBIterator < Buffer , V > ;
528
+ /**
529
+ * @internal
530
+ */
531
+ public _iterator < V > (
532
+ options ?: AbstractIteratorOptions & {
533
+ keyAsBuffer : false ;
534
+ valueAsBuffer : false ;
535
+ } ,
536
+ levelPath ?: LevelPath ,
537
+ ) : DBIterator < string , V > ;
452
538
public _iterator (
453
539
options ?: AbstractIteratorOptions ,
454
540
levelPath : LevelPath = [ ] ,
455
- ) : DBIterator {
456
- options = options ?? { } ;
541
+ ) : DBIterator < any , any > {
457
542
const levelKeyStart = utils . levelPathToKey ( levelPath ) ;
543
+ options = options ?? { } ;
458
544
if ( options . gt != null ) {
459
545
options . gt = Buffer . concat ( [
460
546
levelKeyStart ,
@@ -508,15 +594,22 @@ class DB {
508
594
// Truncate level path so the returned key is relative to the level path
509
595
const keyPath = utils . parseKey ( kv [ 0 ] ) . slice ( levelPath . length ) ;
510
596
kv [ 0 ] = utils . keyPathToKey ( keyPath ) ;
597
+ if ( options ?. keyAsBuffer === false ) {
598
+ kv [ 0 ] = kv [ 0 ] . toString ( 'utf-8' ) ;
599
+ }
511
600
}
512
601
// Handle values: false
513
602
if ( kv [ 1 ] != null ) {
514
- kv [ 1 ] = await this . deserializeDecrypt ( kv [ 1 ] , true ) ;
603
+ if ( options ?. valueAsBuffer === false ) {
604
+ kv [ 1 ] = await this . deserializeDecrypt ( kv [ 1 ] , false ) ;
605
+ } else {
606
+ kv [ 1 ] = await this . deserializeDecrypt ( kv [ 1 ] , true ) ;
607
+ }
515
608
}
516
609
}
517
610
return kv ;
518
611
} ;
519
- return iterator as unknown as DBIterator ;
612
+ return iterator as unknown as DBIterator < any , any > ;
520
613
}
521
614
522
615
/**
@@ -549,13 +642,14 @@ class DB {
549
642
}
550
643
551
644
/**
552
- * Dump from root level
645
+ * Dump from root level path
646
+ * This will show entries from all levels
553
647
* It is intended for diagnostics
554
648
*/
555
- public async dump (
649
+ public async dump < V > (
556
650
levelPath ?: LevelPath ,
557
651
raw ?: false ,
558
- ) : Promise < Array < [ string , any ] > > ;
652
+ ) : Promise < Array < [ string , V ] > > ;
559
653
public async dump (
560
654
levelPath : LevelPath | undefined ,
561
655
raw : true ,
@@ -566,16 +660,14 @@ class DB {
566
660
raw : boolean = false ,
567
661
) : Promise < Array < [ string | Buffer , any ] > > {
568
662
const records : Array < [ string | Buffer , any ] > = [ ] ;
569
- for await ( const [ k , v ] of this . _iterator ( undefined , levelPath ) ) {
570
- let key : string | Buffer , value : any ;
571
- if ( raw ) {
572
- key = k ;
573
- value = v ;
574
- } else {
575
- key = k . toString ( 'utf-8' ) ;
576
- value = utils . deserialize ( v ) ;
577
- }
578
- records . push ( [ key , value ] ) ;
663
+ for await ( const [ k , v ] of this . _iterator (
664
+ {
665
+ keyAsBuffer : raw as any ,
666
+ valueAsBuffer : raw as any ,
667
+ } ,
668
+ levelPath ,
669
+ ) ) {
670
+ records . push ( [ k , v ] ) ;
579
671
}
580
672
return records ;
581
673
}
0 commit comments