@@ -53,7 +53,6 @@ const (
53
53
databaseVersion = 2 // reindexed if database version does not match
54
54
cachedLastBlocks = 1000 // last block of map pointers
55
55
cachedLvPointers = 1000 // first log value pointer of block pointers
56
- cachedBaseRows = 100 // groups of base layer filter row data
57
56
cachedFilterMaps = 3 // complete filter maps (cached by map renderer)
58
57
cachedRenderSnapshots = 8 // saved map renderer data at block boundaries
59
58
)
@@ -101,7 +100,6 @@ type FilterMaps struct {
101
100
filterMapCache * lru.Cache [uint32 , filterMap ]
102
101
lastBlockCache * lru.Cache [uint32 , lastBlockOfMap ]
103
102
lvPointerCache * lru.Cache [uint64 , uint64 ]
104
- baseRowsCache * lru.Cache [uint64 , [][]uint32 ]
105
103
106
104
// the matchers set and the fields of FilterMapsMatcherBackend instances are
107
105
// read and written both by exported functions and the indexer.
@@ -264,7 +262,6 @@ func NewFilterMaps(db ctxcdb.KeyValueStore, initView *ChainView, historyCutoff,
264
262
filterMapCache : lru.NewCache [uint32 , filterMap ](cachedFilterMaps ),
265
263
lastBlockCache : lru.NewCache [uint32 , lastBlockOfMap ](cachedLastBlocks ),
266
264
lvPointerCache : lru.NewCache [uint64 , uint64 ](cachedLvPointers ),
267
- baseRowsCache : lru.NewCache [uint64 , [][]uint32 ](cachedBaseRows ),
268
265
renderSnapshots : lru.NewCache [uint64 , * renderedMap ](cachedRenderSnapshots ),
269
266
}
270
267
f .checkRevertRange () // revert maps that are inconsistent with the current chain view
@@ -348,7 +345,6 @@ func (f *FilterMaps) reset() {
348
345
f .renderSnapshots .Purge ()
349
346
f .lastBlockCache .Purge ()
350
347
f .lvPointerCache .Purge ()
351
- f .baseRowsCache .Purge ()
352
348
f .indexLock .Unlock ()
353
349
// deleting the range first ensures that resetDb will be called again at next
354
350
// startup and any leftover data will be removed even if it cannot finish now.
@@ -565,47 +561,69 @@ func (f *FilterMaps) getFilterMap(mapIndex uint32) (filterMap, error) {
565
561
}
566
562
fm := make (filterMap , f .mapHeight )
567
563
for rowIndex := range fm {
568
- var err error
569
- fm [rowIndex ], err = f .getFilterMapRow (mapIndex , uint32 (rowIndex ), false )
564
+ rows , err := f .getFilterMapRows ([]uint32 {mapIndex }, uint32 (rowIndex ), false )
570
565
if err != nil {
571
566
return nil , fmt .Errorf ("failed to load filter map %d from database: %v" , mapIndex , err )
572
567
}
568
+ fm [rowIndex ] = rows [0 ]
573
569
}
574
570
f .filterMapCache .Add (mapIndex , fm )
575
571
return fm , nil
576
572
}
577
573
578
- // getFilterMapRow fetches the given filter map row. If baseLayerOnly is true
579
- // then only the first baseRowLength entries are returned.
580
- func (f * FilterMaps ) getFilterMapRow (mapIndex , rowIndex uint32 , baseLayerOnly bool ) (FilterRow , error ) {
581
- baseMapRowIndex := f .mapRowIndex (mapIndex & - f .baseRowGroupLength , rowIndex )
582
- baseRows , ok := f .baseRowsCache .Get (baseMapRowIndex )
583
- if ! ok {
584
- var err error
585
- baseRows , err = rawdb .ReadFilterMapBaseRows (f .db , baseMapRowIndex , f .baseRowGroupLength , f .logMapWidth )
586
- if err != nil {
587
- return nil , fmt .Errorf ("failed to retrieve filter map %d base rows %d: %v" , mapIndex , rowIndex , err )
574
+ // getFilterMapRows fetches a set of filter map rows at the corresponding map
575
+ // indices and a shared row index. If baseLayerOnly is true then only the first
576
+ // baseRowLength entries are returned.
577
+ func (f * FilterMaps ) getFilterMapRows (mapIndices []uint32 , rowIndex uint32 , baseLayerOnly bool ) ([]FilterRow , error ) {
578
+ rows := make ([]FilterRow , len (mapIndices ))
579
+ var ptr int
580
+ for len (mapIndices ) > ptr {
581
+ baseRowGroup := mapIndices [ptr ] / f .baseRowGroupLength
582
+ groupLength := 1
583
+ for ptr + groupLength < len (mapIndices ) && mapIndices [ptr + groupLength ]/ f .baseRowGroupLength == baseRowGroup {
584
+ groupLength ++
588
585
}
589
- f .baseRowsCache .Add (baseMapRowIndex , baseRows )
590
- }
591
- baseRow := slices .Clone (baseRows [mapIndex & (f .baseRowGroupLength - 1 )])
592
- if baseLayerOnly {
593
- return baseRow , nil
586
+ if err := f .getFilterMapRowsOfGroup (rows [ptr :ptr + groupLength ], mapIndices [ptr :ptr + groupLength ], rowIndex , baseLayerOnly ); err != nil {
587
+ return nil , err
588
+ }
589
+ ptr += groupLength
594
590
}
595
- extRow , err := rawdb .ReadFilterMapExtRow (f .db , f .mapRowIndex (mapIndex , rowIndex ), f .logMapWidth )
591
+ return rows , nil
592
+ }
593
+
594
+ // getFilterMapRowsOfGroup fetches a set of filter map rows at map indices
595
+ // belonging to the same base row group.
596
+ func (f * FilterMaps ) getFilterMapRowsOfGroup (target []FilterRow , mapIndices []uint32 , rowIndex uint32 , baseLayerOnly bool ) error {
597
+ baseRowGroup := mapIndices [0 ] / f .baseRowGroupLength
598
+ baseMapRowIndex := f .mapRowIndex (baseRowGroup * f .baseRowGroupLength , rowIndex )
599
+ baseRows , err := rawdb .ReadFilterMapBaseRows (f .db , baseMapRowIndex , f .baseRowGroupLength , f .logMapWidth )
596
600
if err != nil {
597
- return nil , fmt .Errorf ("failed to retrieve filter map %d extended row %d: %v" , mapIndex , rowIndex , err )
601
+ return fmt .Errorf ("failed to retrieve base row group %d of row %d: %v" , baseRowGroup , rowIndex , err )
598
602
}
599
- return FilterRow (append (baseRow , extRow ... )), nil
603
+ for i , mapIndex := range mapIndices {
604
+ if mapIndex / f .baseRowGroupLength != baseRowGroup {
605
+ panic ("mapIndices are not in the same base row group" )
606
+ }
607
+ row := baseRows [mapIndex & (f .baseRowGroupLength - 1 )]
608
+ if ! baseLayerOnly {
609
+ extRow , err := rawdb .ReadFilterMapExtRow (f .db , f .mapRowIndex (mapIndex , rowIndex ), f .logMapWidth )
610
+ if err != nil {
611
+ return fmt .Errorf ("failed to retrieve filter map %d extended row %d: %v" , mapIndex , rowIndex , err )
612
+ }
613
+ row = append (row , extRow ... )
614
+ }
615
+ target [i ] = row
616
+ }
617
+ return nil
600
618
}
601
619
602
620
// storeFilterMapRows stores a set of filter map rows at the corresponding map
603
621
// indices and a shared row index.
604
622
func (f * FilterMaps ) storeFilterMapRows (batch ctxcdb.Batch , mapIndices []uint32 , rowIndex uint32 , rows []FilterRow ) error {
605
623
for len (mapIndices ) > 0 {
606
- baseMapIndex := mapIndices [0 ] & - f .baseRowGroupLength
624
+ baseRowGroup := mapIndices [0 ] / f .baseRowGroupLength
607
625
groupLength := 1
608
- for groupLength < len (mapIndices ) && mapIndices [groupLength ]& - f .baseRowGroupLength == baseMapIndex {
626
+ for groupLength < len (mapIndices ) && mapIndices [groupLength ]/ f .baseRowGroupLength == baseRowGroup {
609
627
groupLength ++
610
628
}
611
629
if err := f .storeFilterMapRowsOfGroup (batch , mapIndices [:groupLength ], rowIndex , rows [:groupLength ]); err != nil {
@@ -619,26 +637,20 @@ func (f *FilterMaps) storeFilterMapRows(batch ctxcdb.Batch, mapIndices []uint32,
619
637
// storeFilterMapRowsOfGroup stores a set of filter map rows at map indices
620
638
// belonging to the same base row group.
621
639
func (f * FilterMaps ) storeFilterMapRowsOfGroup (batch ctxcdb.Batch , mapIndices []uint32 , rowIndex uint32 , rows []FilterRow ) error {
622
- baseMapIndex := mapIndices [0 ] & - f .baseRowGroupLength
623
- baseMapRowIndex := f .mapRowIndex (baseMapIndex , rowIndex )
640
+ baseRowGroup := mapIndices [0 ] / f .baseRowGroupLength
641
+ baseMapRowIndex := f .mapRowIndex (baseRowGroup * f . baseRowGroupLength , rowIndex )
624
642
var baseRows [][]uint32
625
643
if uint32 (len (mapIndices )) != f .baseRowGroupLength { // skip base rows read if all rows are replaced
626
- var ok bool
627
- baseRows , ok = f .baseRowsCache .Get (baseMapRowIndex )
628
- if ok {
629
- baseRows = slices .Clone (baseRows )
630
- } else {
631
- var err error
632
- baseRows , err = rawdb .ReadFilterMapBaseRows (f .db , baseMapRowIndex , f .baseRowGroupLength , f .logMapWidth )
633
- if err != nil {
634
- return fmt .Errorf ("failed to retrieve filter map %d base rows %d for modification: %v" , mapIndices [0 ]& - f .baseRowGroupLength , rowIndex , err )
635
- }
644
+ var err error
645
+ baseRows , err = rawdb .ReadFilterMapBaseRows (f .db , baseMapRowIndex , f .baseRowGroupLength , f .logMapWidth )
646
+ if err != nil {
647
+ return fmt .Errorf ("failed to retrieve base row group %d of row %d for modification: %v" , baseRowGroup , rowIndex , err )
636
648
}
637
649
} else {
638
650
baseRows = make ([][]uint32 , f .baseRowGroupLength )
639
651
}
640
652
for i , mapIndex := range mapIndices {
641
- if mapIndex & - f .baseRowGroupLength != baseMapIndex {
653
+ if mapIndex / f .baseRowGroupLength != baseRowGroup {
642
654
panic ("mapIndices are not in the same base row group" )
643
655
}
644
656
baseRow := []uint32 (rows [i ])
@@ -650,7 +662,6 @@ func (f *FilterMaps) storeFilterMapRowsOfGroup(batch ctxcdb.Batch, mapIndices []
650
662
baseRows [mapIndex & (f .baseRowGroupLength - 1 )] = baseRow
651
663
rawdb .WriteFilterMapExtRow (batch , f .mapRowIndex (mapIndex , rowIndex ), extRow , f .logMapWidth )
652
664
}
653
- f .baseRowsCache .Add (baseMapRowIndex , baseRows )
654
665
rawdb .WriteFilterMapBaseRows (batch , baseMapRowIndex , baseRows , f .logMapWidth )
655
666
return nil
656
667
}
0 commit comments