@@ -10,54 +10,60 @@ import (
1010
1111// Cache is a generic cache that maintains items that are seen and hard confirmed
1212type Cache [T any ] struct {
13- items * sync.Map
14- hashes * sync.Map
13+ // itemsByHeight stores items keyed by uint64 height
14+ itemsByHeight * sync.Map
15+ // hashes tracks whether a given hash has been seen
16+ hashes * sync.Map
17+ // daIncluded tracks the DA inclusion height for a given hash
1518 daIncluded * sync.Map
1619}
1720
1821// NewCache returns a new Cache struct
1922func NewCache [T any ]() * Cache [T ] {
2023 return & Cache [T ]{
21- items : new (sync.Map ),
22- hashes : new (sync.Map ),
23- daIncluded : new (sync.Map ),
24+ itemsByHeight : new (sync.Map ),
25+ hashes : new (sync.Map ),
26+ daIncluded : new (sync.Map ),
2427 }
2528}
2629
27- // GetItem returns an item from the cache by height
30+ // GetItem returns an item from the cache by height.
31+ // Returns nil if not found or type mismatch.
2832func (c * Cache [T ]) GetItem (height uint64 ) * T {
29- item , ok := c .items .Load (height )
33+ item , ok := c .itemsByHeight .Load (height )
34+ if ! ok {
35+ return nil
36+ }
37+ val , ok := item .(* T )
3038 if ! ok {
3139 return nil
3240 }
33- val := item .(* T )
3441 return val
3542}
3643
3744// SetItem sets an item in the cache by height
3845func (c * Cache [T ]) SetItem (height uint64 , item * T ) {
39- c .items .Store (height , item )
46+ c .itemsByHeight .Store (height , item )
4047}
4148
4249// DeleteItem deletes an item from the cache by height
4350func (c * Cache [T ]) DeleteItem (height uint64 ) {
44- c .items .Delete (height )
51+ c .itemsByHeight .Delete (height )
4552}
4653
47- // RangeByHeight iterates over all items keyed by uint64 height and calls fn for each.
54+ // RangeByHeight iterates over items keyed by height in an unspecified order and calls fn for each.
4855// If fn returns false, iteration stops early.
49- // Non-uint64 keys (e.g. string hash entries) are ignored.
5056func (c * Cache [T ]) RangeByHeight (fn func (height uint64 , item * T ) bool ) {
51- c .items .Range (func (k , v any ) bool {
57+ c .itemsByHeight .Range (func (k , v any ) bool {
5258 height , ok := k .(uint64 )
5359 if ! ok {
5460 return true
5561 }
56- item , ok := v .(* T )
62+ it , ok := v .(* T )
5763 if ! ok {
5864 return true
5965 }
60- return fn (height , item )
66+ return fn (height , it )
6167 })
6268}
6369
@@ -81,23 +87,13 @@ func (c *Cache[T]) IsDAIncluded(hash string) bool {
8187 return ok
8288}
8389
84- // GetDAIncludedHeight returns the DA height at which the hash was DA included
85- func (c * Cache [T ]) GetDAIncludedHeight (hash string ) (uint64 , bool ) {
86- daIncluded , ok := c .daIncluded .Load (hash )
87- if ! ok {
88- return 0 , false
89- }
90- return daIncluded .(uint64 ), true
91- }
92-
9390// SetDAIncluded sets the hash as DA-included with the given DA height
9491func (c * Cache [T ]) SetDAIncluded (hash string , daHeight uint64 ) {
9592 c .daIncluded .Store (hash , daHeight )
9693}
9794
9895const (
9996 itemsByHeightFilename = "items_by_height.gob"
100- itemsByHashFilename = "items_by_hash.gob"
10197 hashesFilename = "hashes.gob"
10298 daIncludedFilename = "da_included.gob"
10399)
@@ -147,34 +143,19 @@ func (c *Cache[T]) SaveToDisk(folderPath string) error {
147143
148144 // prepare items maps
149145 itemsByHeightMap := make (map [uint64 ]* T )
150- itemsByHashMap := make (map [string ]* T )
151146
152- var invalidItemsErr error
153- c .items .Range (func (k , v any ) bool {
154- itemVal , ok := v .(* T )
155- if ! ok {
156- invalidItemsErr = fmt .Errorf ("invalid item type: %T" , v )
157- return false // early exit if the value is not of type *T
158- }
159- switch key := k .(type ) {
160- case uint64 :
161- itemsByHeightMap [key ] = itemVal
162- case string :
163- itemsByHashMap [key ] = itemVal
147+ c .itemsByHeight .Range (func (k , v any ) bool {
148+ if hk , ok := k .(uint64 ); ok {
149+ if it , ok := v .(* T ); ok {
150+ itemsByHeightMap [hk ] = it
151+ }
164152 }
165153 return true
166154 })
167155
168- if invalidItemsErr != nil {
169- return invalidItemsErr
170- }
171-
172156 if err := saveMapGob (filepath .Join (folderPath , itemsByHeightFilename ), itemsByHeightMap ); err != nil {
173157 return err
174158 }
175- if err := saveMapGob (filepath .Join (folderPath , itemsByHashFilename ), itemsByHashMap ); err != nil {
176- return err
177- }
178159
179160 // prepare hashes map
180161 hashesToSave := make (map [string ]bool )
@@ -214,16 +195,7 @@ func (c *Cache[T]) LoadFromDisk(folderPath string) error {
214195 return fmt .Errorf ("failed to load items by height: %w" , err )
215196 }
216197 for k , v := range itemsByHeightMap {
217- c .items .Store (k , v )
218- }
219-
220- // load items by hash
221- itemsByHashMap , err := loadMapGob [string , * T ](filepath .Join (folderPath , itemsByHashFilename ))
222- if err != nil {
223- return fmt .Errorf ("failed to load items by hash: %w" , err )
224- }
225- for k , v := range itemsByHashMap {
226- c .items .Store (k , v )
198+ c .itemsByHeight .Store (k , v )
227199 }
228200
229201 // load hashes
0 commit comments