@@ -51,18 +51,9 @@ import {
51
51
TransactionToken ,
52
52
TransactionLockHelper ,
53
53
} from "./TransactionLockHelper" ;
54
- import {
55
- empty ,
56
- RedBlackTreeStructure ,
57
- set ,
58
- iterateFromIndex ,
59
- iterateKeysFromFirst ,
60
- get ,
61
- iterateKeysFromLast ,
62
- has ,
63
- remove ,
64
- iterateFromFirst ,
65
- } from "@collectable/red-black-tree" ;
54
+
55
+ import BTree from "sorted-btree" ;
56
+
66
57
export interface StoreData {
67
58
data : Map < string , ItemType > ;
68
59
indices : Map < string , InMemoryIndex > ;
@@ -448,18 +439,15 @@ class InMemoryStore implements DbStore {
448
439
449
440
// Note: Currently maintains nothing interesting -- rebuilds the results every time from scratch. Scales like crap.
450
441
class InMemoryIndex extends DbIndexFTSFromRangeQueries {
451
- private _rbIndex : RedBlackTreeStructure < string , ItemType [ ] > ;
442
+ private _bTreeIndex : BTree < string , ItemType [ ] > ;
452
443
private _trans ?: InMemoryTransaction ;
453
444
constructor (
454
445
_mergedData : Map < string , ItemType > ,
455
446
indexSchema : IndexSchema ,
456
447
primaryKeyPath : KeyPathType
457
448
) {
458
449
super ( indexSchema , primaryKeyPath ) ;
459
- this . _rbIndex = empty < string , ItemType [ ] > (
460
- ( a : string , b : string ) => ( a < b ? - 1 : a > b ? 1 : 0 ) ,
461
- true
462
- ) ;
450
+ this . _bTreeIndex = new BTree ( ) ;
463
451
this . put ( values ( _mergedData ) , true ) ;
464
452
}
465
453
@@ -504,12 +492,12 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
504
492
505
493
each ( keys , ( key ) => {
506
494
// For non-unique indexes we want to overwrite
507
- if ( ! this . isUniqueIndex ( ) && has ( key , this . _rbIndex ) ) {
508
- const existingItems = get ( key , this . _rbIndex ) ! ! ! as ItemType [ ] ;
495
+ if ( ! this . isUniqueIndex ( ) && this . _bTreeIndex . has ( key ) ) {
496
+ const existingItems = this . _bTreeIndex . get ( key ) ! ! ! as ItemType [ ] ;
509
497
existingItems . push ( item ) ;
510
- set < string , ItemType [ ] > ( key , existingItems , this . _rbIndex ) ;
498
+ this . _bTreeIndex . set ( key , existingItems ) ;
511
499
} else {
512
- set ( key , [ item ] , this . _rbIndex ) ;
500
+ this . _bTreeIndex . set ( key , [ item ] ) ;
513
501
}
514
502
} ) ;
515
503
} ) ;
@@ -534,7 +522,7 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
534
522
535
523
let values = [ ] as ItemType [ ] ;
536
524
for ( const key of joinedKeys ) {
537
- values . push ( get ( key , this . _rbIndex ) as ItemType [ ] ) ;
525
+ values . push ( this . _bTreeIndex . get ( key ) as ItemType [ ] ) ;
538
526
}
539
527
return Promise . resolve ( compact ( flatten ( values ) ) ) ;
540
528
}
@@ -548,9 +536,9 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
548
536
}
549
537
550
538
if ( typeof key === "string" ) {
551
- remove ( key , this . _rbIndex ) ;
539
+ this . _bTreeIndex . delete ( key ) ;
552
540
} else {
553
- const idxItems = get ( key . idxKey , this . _rbIndex ) ;
541
+ const idxItems = this . _bTreeIndex . get ( key . idxKey ) ;
554
542
if ( ! idxItems ) {
555
543
return ;
556
544
}
@@ -567,9 +555,9 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
567
555
// otherwise, update the index value with the new array
568
556
// sans the primary key item
569
557
if ( idxItemsWithoutItem ?. length === 0 ) {
570
- remove ( key . idxKey , this . _rbIndex ) ;
558
+ this . _bTreeIndex . delete ( key . idxKey ) ;
571
559
} else {
572
- set < string , ItemType [ ] > ( key . idxKey , idxItemsWithoutItem , this . _rbIndex ) ;
560
+ this . _bTreeIndex . set ( key . idxKey , idxItemsWithoutItem ) ;
573
561
}
574
562
}
575
563
}
@@ -582,24 +570,27 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
582
570
const definedLimit = limit
583
571
? limit
584
572
: this . isUniqueIndex ( )
585
- ? this . _rbIndex . _size
573
+ ? this . _bTreeIndex . _size
586
574
: MAX_COUNT ;
587
575
let definedOffset = offset ? offset : 0 ;
588
576
const data = new Array < ItemType > ( definedLimit ) ;
589
577
const reverse =
590
578
reverseOrSortOrder === true ||
591
579
reverseOrSortOrder === QuerySortOrder . Reverse ;
592
580
// when index is not unique, we cannot use offset as a starting index
593
- const iterator = iterateFromIndex (
594
- reverse ,
595
- this . isUniqueIndex ( ) ? definedOffset : 0 ,
596
- this . _rbIndex
597
- ) ;
581
+ let skip = this . isUniqueIndex ( ) ? definedOffset : 0 ;
582
+ const iterator = reverse
583
+ ? this . _bTreeIndex . entriesReversed ( )
584
+ : this . _bTreeIndex . entries ( ) ;
598
585
let i = 0 ;
599
586
for ( const item of iterator ) {
587
+ if ( skip > 0 ) {
588
+ skip -- ;
589
+ continue ;
590
+ }
600
591
// when index is not unique, each node may contain multiple items
601
592
if ( ! this . isUniqueIndex ( ) ) {
602
- let count = item . value . length ;
593
+ let count = item [ 1 ] . length ;
603
594
const minOffsetCount = Math . min ( count , definedOffset ) ;
604
595
count -= minOffsetCount ;
605
596
definedOffset -= minOffsetCount ;
@@ -609,9 +600,9 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
609
600
}
610
601
611
602
const values = this . _getKeyValues (
612
- item . key ,
603
+ item [ 0 ] ,
613
604
definedLimit - i ,
614
- item . value . length - count ,
605
+ item [ 1 ] . length - count ,
615
606
reverse
616
607
) ;
617
608
@@ -621,7 +612,7 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
621
612
622
613
i += values . length ;
623
614
} else {
624
- data [ i ] = item . value [ 0 ] ;
615
+ data [ i ] = item [ 1 ] [ 0 ] ;
625
616
i ++ ;
626
617
}
627
618
@@ -671,16 +662,17 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
671
662
limit = limit
672
663
? limit
673
664
: this . isUniqueIndex ( )
674
- ? this . _rbIndex . _size
665
+ ? this . _bTreeIndex . _size
675
666
: MAX_COUNT ;
676
667
offset = offset ? offset : 0 ;
677
668
const keyLow = serializeKeyToString ( keyLowRange , this . _keyPath ) ;
678
669
const keyHigh = serializeKeyToString ( keyHighRange , this . _keyPath ) ;
679
670
const iterator = reverse
680
- ? iterateKeysFromLast ( this . _rbIndex )
681
- : iterateKeysFromFirst ( this . _rbIndex ) ;
671
+ ? this . _bTreeIndex . entriesReversed ( )
672
+ : this . _bTreeIndex . entries ( ) ;
682
673
let values = [ ] as ItemType [ ] ;
683
- for ( const key of iterator ) {
674
+ for ( const entry of iterator ) {
675
+ const key = entry [ 0 ] ;
684
676
if (
685
677
( key > keyLow || ( key === keyLow && ! lowRangeExclusive ) ) &&
686
678
( key < keyHigh || ( key === keyHigh && ! highRangeExclusive ) )
@@ -690,7 +682,7 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
690
682
offset -- ;
691
683
continue ;
692
684
} else {
693
- const idxValues = get ( key , this . _rbIndex ) as ItemType [ ] ;
685
+ const idxValues = this . _bTreeIndex . get ( key ) as ItemType [ ] ;
694
686
offset -= idxValues . length ;
695
687
// if offset >= 0, we skipped just enough, or we still need to skip more
696
688
// if offset < 0, we need to get some of the values from the index
@@ -704,7 +696,7 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
704
696
}
705
697
706
698
if ( this . isUniqueIndex ( ) ) {
707
- values = values . concat ( get ( key , this . _rbIndex ) as ItemType [ ] ) ;
699
+ values = values . concat ( this . _bTreeIndex . get ( key ) as ItemType [ ] ) ;
708
700
} else {
709
701
values = values . concat (
710
702
this . _getKeyValues (
@@ -767,7 +759,7 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
767
759
if ( limit <= 0 ) {
768
760
return [ ] ;
769
761
}
770
- const idxValues = get ( key , this . _rbIndex ) as ItemType [ ] ;
762
+ const idxValues = this . _bTreeIndex . get ( key ) as ItemType [ ] ;
771
763
772
764
// get may return undefined, if the key is not found
773
765
if ( ! idxValues ) {
@@ -801,9 +793,10 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
801
793
) : string [ ] {
802
794
const keyLow = serializeKeyToString ( keyLowRange , this . _keyPath ) ;
803
795
const keyHigh = serializeKeyToString ( keyHighRange , this . _keyPath ) ;
804
- const iterator = iterateKeysFromFirst ( this . _rbIndex ) ;
796
+ const iterator = this . _bTreeIndex . entries ( ) ;
805
797
const keys = [ ] ;
806
- for ( const key of iterator ) {
798
+ for ( const entry of iterator ) {
799
+ const key = entry [ 0 ] ;
807
800
if (
808
801
( key > keyLow || ( key === keyLow && ! lowRangeExclusive ) ) &&
809
802
( key < keyHigh || ( key === keyHigh && ! highRangeExclusive ) )
@@ -823,17 +816,17 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
823
816
) : number {
824
817
const keyLow = serializeKeyToString ( keyLowRange , this . _keyPath ) ;
825
818
const keyHigh = serializeKeyToString ( keyHighRange , this . _keyPath ) ;
826
- const iterator = iterateFromFirst ( this . _rbIndex ) ;
819
+ const iterator = this . _bTreeIndex . entries ( ) ;
827
820
let keyCount = 0 ;
828
821
for ( const item of iterator ) {
829
822
if (
830
- ( item . key > keyLow || ( item . key === keyLow && ! lowRangeExclusive ) ) &&
831
- ( item . key < keyHigh || ( item . key === keyHigh && ! highRangeExclusive ) )
823
+ ( item [ 0 ] > keyLow || ( item [ 0 ] === keyLow && ! lowRangeExclusive ) ) &&
824
+ ( item [ 0 ] < keyHigh || ( item [ 0 ] === keyHigh && ! highRangeExclusive ) )
832
825
) {
833
826
if ( this . isUniqueIndex ( ) ) {
834
827
keyCount ++ ;
835
828
} else {
836
- keyCount += item . value . length ;
829
+ keyCount += item [ 1 ] . length ;
837
830
}
838
831
}
839
832
}
@@ -842,13 +835,13 @@ class InMemoryIndex extends DbIndexFTSFromRangeQueries {
842
835
843
836
countAll ( ) : Promise < number > {
844
837
if ( this . isUniqueIndex ( ) ) {
845
- return Promise . resolve ( this . _rbIndex . _size ) ;
838
+ return Promise . resolve ( this . _bTreeIndex . _size ) ;
846
839
} else {
847
840
const keyCount = attempt ( ( ) => {
848
- const iterator = iterateFromFirst ( this . _rbIndex ) ;
841
+ const iterator = this . _bTreeIndex . entries ( ) ;
849
842
let keyCount = 0 ;
850
843
for ( const item of iterator ) {
851
- keyCount += item . value . length ;
844
+ keyCount += item [ 1 ] . length ;
852
845
}
853
846
return keyCount ;
854
847
} ) ;
0 commit comments