@@ -453,6 +453,74 @@ func (c *KVStore) ForEachChannel(cb func(*models.ChannelEdgeInfo,
453
453
}, func () {})
454
454
}
455
455
456
+ // ForEachChannelCacheable iterates through all the channel edges stored within
457
+ // the graph and invokes the passed callback for each edge. The callback takes
458
+ // two edges as since this is a directed graph, both the in/out edges are
459
+ // visited. If the callback returns an error, then the transaction is aborted
460
+ // and the iteration stops early.
461
+ //
462
+ // NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
463
+ // for that particular channel edge routing policy will be passed into the
464
+ // callback.
465
+ //
466
+ // NOTE: this method is like ForEachChannel but fetches only the data required
467
+ // for the graph cache.
468
+ func (c * KVStore ) ForEachChannelCacheable (cb func (* models.CachedEdgeInfo ,
469
+ * models.CachedEdgePolicy , * models.CachedEdgePolicy ) error ) error {
470
+
471
+ return c .db .View (func (tx kvdb.RTx ) error {
472
+ edges := tx .ReadBucket (edgeBucket )
473
+ if edges == nil {
474
+ return ErrGraphNoEdgesFound
475
+ }
476
+
477
+ // First, load all edges in memory indexed by node and channel
478
+ // id.
479
+ channelMap , err := c .getChannelMap (edges )
480
+ if err != nil {
481
+ return err
482
+ }
483
+
484
+ edgeIndex := edges .NestedReadBucket (edgeIndexBucket )
485
+ if edgeIndex == nil {
486
+ return ErrGraphNoEdgesFound
487
+ }
488
+
489
+ // Load edge index, recombine each channel with the policies
490
+ // loaded above and invoke the callback.
491
+ return kvdb .ForAll (
492
+ edgeIndex , func (k , edgeInfoBytes []byte ) error {
493
+ var chanID [8 ]byte
494
+ copy (chanID [:], k )
495
+
496
+ edgeInfoReader := bytes .NewReader (edgeInfoBytes )
497
+ info , err := deserializeChanEdgeInfo (
498
+ edgeInfoReader ,
499
+ )
500
+ if err != nil {
501
+ return err
502
+ }
503
+
504
+ policy1 := channelMap [channelMapKey {
505
+ nodeKey : info .NodeKey1Bytes ,
506
+ chanID : chanID ,
507
+ }]
508
+
509
+ policy2 := channelMap [channelMapKey {
510
+ nodeKey : info .NodeKey2Bytes ,
511
+ chanID : chanID ,
512
+ }]
513
+
514
+ return cb (
515
+ models .NewCachedEdge (& info ),
516
+ models .NewCachedPolicy (policy1 ),
517
+ models .NewCachedPolicy (policy2 ),
518
+ )
519
+ },
520
+ )
521
+ }, func () {})
522
+ }
523
+
456
524
// forEachNodeDirectedChannel iterates through all channels of a given node,
457
525
// executing the passed callback on the directed edge representing the channel
458
526
// and its incoming policy. If the callback returns an error, then the iteration
0 commit comments