@@ -21,12 +21,12 @@ var log = logging.Logger("indexer-core/cache")
21
21
// radixCache is a rotatable cache with value deduplication.
22
22
type radixCache struct {
23
23
// multihash -> indexer.Value
24
- current * radixtree.Tree
25
- previous * radixtree.Tree
24
+ current * radixtree.Tree [[] * indexer. Value ]
25
+ previous * radixtree.Tree [[] * indexer. Value ]
26
26
27
27
// IndexEntery interning
28
- curEnts * radixtree.Tree
29
- prevEnts * radixtree.Tree
28
+ curEnts * radixtree.Tree [ * indexer. Value ]
29
+ prevEnts * radixtree.Tree [ * indexer. Value ]
30
30
31
31
mutex sync.Mutex
32
32
evictions int
@@ -36,8 +36,8 @@ type radixCache struct {
36
36
// New creates a new radixCache instance.
37
37
func New (maxSize int ) * radixCache {
38
38
return & radixCache {
39
- current : radixtree .New (),
40
- curEnts : radixtree .New (),
39
+ current : radixtree .New [[] * indexer. Value ] (),
40
+ curEnts : radixtree .New [ * indexer. Value ] (),
41
41
rotateSize : maxSize >> 1 ,
42
42
}
43
43
}
@@ -107,16 +107,15 @@ keysLoop:
107
107
if c .curEnts .Len () > (c .rotateSize << 1 ) {
108
108
c .rotate ()
109
109
// Remove all non-indexed cache entries.
110
- c .previous .Walk ("" , func (k string , v interface {}) bool {
111
- values := v .([]* indexer.Value )
110
+ for _ , values := range c .previous .Iter () {
112
111
for _ , val := range values {
113
112
_ , _ , found := c .findInternValue (val )
114
113
if ! found {
115
114
panic ("cannot find interned value for cached index" )
116
115
}
117
116
}
118
- return false
119
- })
117
+ }
118
+
120
119
c .current = c .previous
121
120
c .previous = nil
122
121
c .prevEnts = nil
@@ -173,10 +172,9 @@ func (c *radixCache) RemoveProvider(providerID peer.ID) int {
173
172
174
173
var count int
175
174
var deletes []string
176
- var tree * radixtree.Tree
175
+ var tree * radixtree.Tree [[] * indexer. Value ]
177
176
178
- walkFunc := func (k string , v interface {}) bool {
179
- values := v .([]* indexer.Value )
177
+ walkFunc := func (k string , values []* indexer.Value ) {
180
178
var vrm int
181
179
for i := 0 ; i < len (values ); {
182
180
if providerID == values [i ].ProviderID {
@@ -198,14 +196,15 @@ func (c *radixCache) RemoveProvider(providerID peer.ID) int {
198
196
tree .Put (k , values )
199
197
}
200
198
count += vrm
201
- return false
202
199
}
203
200
204
201
hasCurValues := removeProviderInterns (c .curEnts , providerID )
205
202
if hasCurValues {
206
203
// Remove provider values only if there were any provider interns.
207
204
tree = c .current
208
- c .current .Walk ("" , walkFunc )
205
+ for k , v := range c .current .Iter () {
206
+ walkFunc (k , v )
207
+ }
209
208
for _ , k := range deletes {
210
209
c .current .Delete (k )
211
210
}
@@ -221,7 +220,9 @@ func (c *radixCache) RemoveProvider(providerID peer.ID) int {
221
220
if hasPrevValues || hasCurValues {
222
221
deletes = deletes [:0 ]
223
222
tree = c .previous
224
- c .previous .Walk ("" , walkFunc )
223
+ for k , v := range c .previous .Iter () {
224
+ walkFunc (k , v )
225
+ }
225
226
for _ , k := range deletes {
226
227
c .previous .Delete (k )
227
228
}
@@ -245,10 +246,9 @@ func (c *radixCache) RemoveProviderContext(providerID peer.ID, contextID []byte)
245
246
246
247
var deletes []string
247
248
var count int
248
- var tree * radixtree.Tree
249
+ var tree * radixtree.Tree [[] * indexer. Value ]
249
250
250
- walkFunc := func (k string , v interface {}) bool {
251
- values := v .([]* indexer.Value )
251
+ walkFunc := func (k string , values []* indexer.Value ) {
252
252
var vrm int
253
253
for i := 0 ; i < len (values ); {
254
254
if values [i ] == val {
@@ -270,18 +270,21 @@ func (c *radixCache) RemoveProviderContext(providerID peer.ID, contextID []byte)
270
270
tree .Put (k , values )
271
271
}
272
272
count += vrm
273
- return false
274
273
}
275
274
276
275
tree = c .current
277
- c .current .Walk ("" , walkFunc )
276
+ for k , v := range c .current .Iter () {
277
+ walkFunc (k , v )
278
+ }
278
279
for _ , k := range deletes {
279
280
c .current .Delete (k )
280
281
}
281
282
if c .previous != nil {
282
283
deletes = deletes [:0 ]
283
284
tree = c .previous
284
- c .previous .Walk ("" , walkFunc )
285
+ for k , v := range c .previous .Iter () {
286
+ walkFunc (k , v )
287
+ }
285
288
for _ , k := range deletes {
286
289
c .previous .Delete (k )
287
290
}
@@ -329,22 +332,21 @@ func (c *radixCache) Stats() cache.Stats {
329
332
330
333
func (c * radixCache ) get (k string ) ([]* indexer.Value , bool ) {
331
334
// Search current cache.
332
- v , found := c .current .Get (k )
335
+ values , found := c .current .Get (k )
333
336
if ! found {
334
337
if c .previous == nil {
335
338
return nil , false
336
339
}
337
340
338
341
// Search previous if not found in current.
339
- v , found = c .previous .Get (k )
342
+ values , found = c .previous .Get (k )
340
343
if ! found {
341
344
return nil , false
342
345
}
343
346
344
347
// Pull the interned values for these values forward from the previous
345
348
// cache to reuse the interned values instead of allocating them again
346
349
// in the current cache.
347
- values := v .([]* indexer.Value )
348
350
for i , val := range values {
349
351
values [i ] = c .internValue (val , false , true )
350
352
}
@@ -353,16 +355,16 @@ func (c *radixCache) get(k string) ([]*indexer.Value, bool) {
353
355
c .current .Put (k , values )
354
356
c .previous .Delete (k )
355
357
}
356
- return v .([] * indexer. Value ) , true
358
+ return values , true
357
359
}
358
360
359
361
func (c * radixCache ) rotate () {
360
362
if c .previous != nil {
361
363
c .evictions += c .previous .Len ()
362
364
log .Infow ("Rotating cache" , "evictions" , c .previous .Len ())
363
365
}
364
- c .previous , c .current = c .current , radixtree .New ()
365
- c .prevEnts , c .curEnts = c .curEnts , radixtree .New ()
366
+ c .previous , c .current = c .current , radixtree .New [[] * indexer. Value ] ()
367
+ c .prevEnts , c .curEnts = c .curEnts , radixtree .New [ * indexer. Value ] ()
366
368
}
367
369
368
370
// internValue stores a single copy of a Value under a key composed of
@@ -411,7 +413,7 @@ func (c *radixCache) findInternValue(value *indexer.Value) (string, *indexer.Val
411
413
v , found := c .curEnts .Get (k )
412
414
if found {
413
415
// Found existing interned value.
414
- return k , v .( * indexer. Value ) , true
416
+ return k , v , true
415
417
}
416
418
417
419
if c .prevEnts != nil {
@@ -420,21 +422,20 @@ func (c *radixCache) findInternValue(value *indexer.Value) (string, *indexer.Val
420
422
// Pull interned value forward from previous cache.
421
423
c .curEnts .Put (k , v )
422
424
c .prevEnts .Delete (k )
423
- return k , v .( * indexer. Value ) , true
425
+ return k , v , true
424
426
}
425
427
}
426
428
427
429
return k , nil , false
428
430
}
429
431
430
- func removeIndex (tree * radixtree.Tree , k string , value * indexer.Value ) bool {
432
+ func removeIndex (tree * radixtree.Tree [[] * indexer. Value ] , k string , value * indexer.Value ) bool {
431
433
// Get from current cache.
432
- v , found := tree .Get (k )
434
+ values , found := tree .Get (k )
433
435
if ! found {
434
436
return false
435
437
}
436
438
437
- values := v .([]* indexer.Value )
438
439
for i , v := range values {
439
440
if v == value || v .Match (* value ) {
440
441
if len (values ) == 1 {
@@ -451,12 +452,11 @@ func removeIndex(tree *radixtree.Tree, k string, value *indexer.Value) bool {
451
452
return false
452
453
}
453
454
454
- func removeProviderInterns (tree * radixtree.Tree , providerID peer.ID ) bool {
455
+ func removeProviderInterns (tree * radixtree.Tree [ * indexer. Value ] , providerID peer.ID ) bool {
455
456
var deletes []string
456
- tree .Walk (string (providerID ), func ( k string , v interface {}) bool {
457
+ for k := range tree .IterAt (string (providerID )) {
457
458
deletes = append (deletes , k )
458
- return false
459
- })
459
+ }
460
460
for _ , k := range deletes {
461
461
tree .Delete (k )
462
462
}
0 commit comments