• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13517631772

25 Feb 2025 09:12AM UTC coverage: 58.815% (-0.01%) from 58.825%
13517631772

Pull #9544

github

web-flow
Merge pull request #9545 from ellemouton/graph13

graph: extract cache from CRUD [2]
Pull Request #9544: graph: move graph cache out of CRUD layer

2479 of 3249 new or added lines in 5 files covered. (76.3%)

302 existing lines in 19 files now uncovered.

136348 of 231825 relevant lines covered (58.82%)

19321.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.76
/graph/db/graph.go
1
package graphdb
2

3
import (
4
        "time"
5

6
        "github.com/lightningnetwork/lnd/graph/db/models"
7
        "github.com/lightningnetwork/lnd/kvdb"
8
        "github.com/lightningnetwork/lnd/lnwire"
9
        "github.com/lightningnetwork/lnd/routing/route"
10
)
11

12
// Config is a struct that holds all the necessary dependencies for a
13
// ChannelGraph.
14
type Config struct {
15
        // KVDB is the kvdb.Backend that will be used for initializing the
16
        // KVStore CRUD layer.
17
        KVDB kvdb.Backend
18

19
        // KVStoreOpts is a list of functional options that will be used when
20
        // initializing the KVStore.
21
        KVStoreOpts []KVStoreOptionModifier
22
}
23

24
// ChannelGraph is a layer above the graph's CRUD layer.
25
//
26
// NOTE: currently, this is purely a pass-through layer directly to the backing
27
// KVStore. Upcoming commits will move the graph cache out of the KVStore and
28
// into this layer so that the KVStore is only responsible for CRUD operations.
29
type ChannelGraph struct {
30
        graphCache *GraphCache
31

32
        *KVStore
33
}
34

35
// NewChannelGraph creates a new ChannelGraph instance with the given backend.
36
func NewChannelGraph(cfg *Config, options ...ChanGraphOption) (*ChannelGraph,
37
        error) {
176✔
38

176✔
39
        opts := defaultChanGraphOptions()
176✔
40
        for _, o := range options {
281✔
41
                o(opts)
105✔
42
        }
105✔
43

44
        store, err := NewKVStore(cfg.KVDB, cfg.KVStoreOpts...)
176✔
45
        if err != nil {
176✔
NEW
46
                return nil, err
×
UNCOV
47
        }
×
48

49
        if !opts.useGraphCache {
212✔
50
                return &ChannelGraph{
36✔
51
                        KVStore: store,
36✔
52
                }, nil
36✔
53
        }
36✔
54

55
        // The graph cache can be turned off (e.g. for mobile users) for a
56
        // speed/memory usage tradeoff.
57
        graphCache := NewGraphCache(opts.preAllocCacheNumNodes)
143✔
58
        startTime := time.Now()
143✔
59
        log.Debugf("Populating in-memory channel graph, this might take a " +
143✔
60
                "while...")
143✔
61

143✔
62
        err = store.ForEachNodeCacheable(func(node route.Vertex,
143✔
63
                features *lnwire.FeatureVector) error {
246✔
64

103✔
65
                graphCache.AddNodeFeatures(node, features)
103✔
66

103✔
67
                return nil
103✔
68
        })
103✔
69
        if err != nil {
143✔
70
                return nil, err
×
71
        }
×
72

73
        err = store.ForEachChannel(func(info *models.ChannelEdgeInfo,
143✔
74
                policy1, policy2 *models.ChannelEdgePolicy) error {
542✔
75

399✔
76
                graphCache.AddChannel(info, policy1, policy2)
399✔
77

399✔
78
                return nil
399✔
79
        })
399✔
80
        if err != nil {
143✔
UNCOV
81
                return nil, err
×
UNCOV
82
        }
×
83

84
        log.Debugf("Finished populating in-memory channel graph (took %v, %s)",
143✔
85
                time.Since(startTime), graphCache.Stats())
143✔
86

143✔
87
        store.setGraphCache(graphCache)
143✔
88

143✔
89
        return &ChannelGraph{
143✔
90
                KVStore:    store,
143✔
91
                graphCache: graphCache,
143✔
92
        }, nil
143✔
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc