@@ -2,6 +2,7 @@ package graphdb
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
5
6
"sync"
6
7
"sync/atomic"
7
8
"time"
@@ -58,50 +59,18 @@ func NewChannelGraph(cfg *Config, options ...ChanGraphOption) (*ChannelGraph,
58
59
return nil , err
59
60
}
60
61
61
- if ! opts .useGraphCache {
62
- return & ChannelGraph {
63
- KVStore : store ,
64
- quit : make (chan struct {}),
65
- }, nil
62
+ g := & ChannelGraph {
63
+ KVStore : store ,
64
+ quit : make (chan struct {}),
66
65
}
67
66
68
67
// The graph cache can be turned off (e.g. for mobile users) for a
69
68
// speed/memory usage tradeoff.
70
- graphCache := NewGraphCache (opts .preAllocCacheNumNodes )
71
- startTime := time .Now ()
72
- log .Debugf ("Populating in-memory channel graph, this might take a " +
73
- "while..." )
74
-
75
- err = store .ForEachNodeCacheable (func (node route.Vertex ,
76
- features * lnwire.FeatureVector ) error {
77
-
78
- graphCache .AddNodeFeatures (node , features )
79
-
80
- return nil
81
- })
82
- if err != nil {
83
- return nil , err
69
+ if opts .useGraphCache {
70
+ g .graphCache = NewGraphCache (opts .preAllocCacheNumNodes )
84
71
}
85
72
86
- err = store .ForEachChannel (func (info * models.ChannelEdgeInfo ,
87
- policy1 , policy2 * models.ChannelEdgePolicy ) error {
88
-
89
- graphCache .AddChannel (info , policy1 , policy2 )
90
-
91
- return nil
92
- })
93
- if err != nil {
94
- return nil , err
95
- }
96
-
97
- log .Debugf ("Finished populating in-memory channel graph (took %v, %s)" ,
98
- time .Since (startTime ), graphCache .Stats ())
99
-
100
- return & ChannelGraph {
101
- KVStore : store ,
102
- graphCache : graphCache ,
103
- quit : make (chan struct {}),
104
- }, nil
73
+ return g , nil
105
74
}
106
75
107
76
// Start kicks off any goroutines required for the ChannelGraph to function.
@@ -114,6 +83,13 @@ func (c *ChannelGraph) Start() error {
114
83
log .Debugf ("ChannelGraph starting" )
115
84
defer log .Debug ("ChannelGraph started" )
116
85
86
+ if c .graphCache != nil {
87
+ if err := c .populateCache (); err != nil {
88
+ return fmt .Errorf ("could not populate the graph " +
89
+ "cache: %w" , err )
90
+ }
91
+ }
92
+
117
93
return nil
118
94
}
119
95
@@ -132,6 +108,42 @@ func (c *ChannelGraph) Stop() error {
132
108
return nil
133
109
}
134
110
111
+ // populateCache loads the entire channel graph into the in-memory graph cache.
112
+ //
113
+ // NOTE: This should only be called if the graphCache has been constructed.
114
+ func (c * ChannelGraph ) populateCache () error {
115
+ startTime := time .Now ()
116
+ log .Debugf ("Populating in-memory channel graph, this might take a " +
117
+ "while..." )
118
+
119
+ err := c .KVStore .ForEachNodeCacheable (func (node route.Vertex ,
120
+ features * lnwire.FeatureVector ) error {
121
+
122
+ c .graphCache .AddNodeFeatures (node , features )
123
+
124
+ return nil
125
+ })
126
+ if err != nil {
127
+ return err
128
+ }
129
+
130
+ err = c .KVStore .ForEachChannel (func (info * models.ChannelEdgeInfo ,
131
+ policy1 , policy2 * models.ChannelEdgePolicy ) error {
132
+
133
+ c .graphCache .AddChannel (info , policy1 , policy2 )
134
+
135
+ return nil
136
+ })
137
+ if err != nil {
138
+ return err
139
+ }
140
+
141
+ log .Debugf ("Finished populating in-memory channel graph (took %v, %s)" ,
142
+ time .Since (startTime ), c .graphCache .Stats ())
143
+
144
+ return nil
145
+ }
146
+
135
147
// ForEachNodeDirectedChannel iterates through all channels of a given node,
136
148
// executing the passed callback on the directed edge representing the channel
137
149
// and its incoming policy. If the callback returns an error, then the iteration
0 commit comments