mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-28 14:40:51 +02:00
graph/db: let ChannelGraph init the graphCache
In this commit, we let the ChannelGraph be responsible for populating the graphCache and then passing it to the KVStore. This is a first step in moving the graphCache completely out of the KVStore layer.
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
package graphdb
|
||||
|
||||
import "github.com/lightningnetwork/lnd/kvdb"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/lightningnetwork/lnd/graph/db/models"
|
||||
"github.com/lightningnetwork/lnd/kvdb"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/routing/route"
|
||||
)
|
||||
|
||||
// Config is a struct that holds all the necessary dependencies for a
|
||||
// ChannelGraph.
|
||||
@@ -20,17 +27,67 @@ type Config struct {
|
||||
// KVStore. Upcoming commits will move the graph cache out of the KVStore and
|
||||
// into this layer so that the KVStore is only responsible for CRUD operations.
|
||||
type ChannelGraph struct {
|
||||
graphCache *GraphCache
|
||||
|
||||
*KVStore
|
||||
}
|
||||
|
||||
// NewChannelGraph creates a new ChannelGraph instance with the given backend.
|
||||
func NewChannelGraph(cfg *Config) (*ChannelGraph, error) {
|
||||
func NewChannelGraph(cfg *Config, options ...ChanGraphOption) (*ChannelGraph,
|
||||
error) {
|
||||
|
||||
opts := defaultChanGraphOptions()
|
||||
for _, o := range options {
|
||||
o(opts)
|
||||
}
|
||||
|
||||
store, err := NewKVStore(cfg.KVDB, cfg.KVStoreOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !opts.useGraphCache {
|
||||
return &ChannelGraph{
|
||||
KVStore: store,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// The graph cache can be turned off (e.g. for mobile users) for a
|
||||
// speed/memory usage tradeoff.
|
||||
graphCache := NewGraphCache(opts.preAllocCacheNumNodes)
|
||||
startTime := time.Now()
|
||||
log.Debugf("Populating in-memory channel graph, this might take a " +
|
||||
"while...")
|
||||
|
||||
err = store.ForEachNodeCacheable(func(node route.Vertex,
|
||||
features *lnwire.FeatureVector) error {
|
||||
|
||||
graphCache.AddNodeFeatures(node, features)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = store.ForEachChannel(func(info *models.ChannelEdgeInfo,
|
||||
policy1, policy2 *models.ChannelEdgePolicy) error {
|
||||
|
||||
graphCache.AddChannel(info, policy1, policy2)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Debugf("Finished populating in-memory channel graph (took %v, %s)",
|
||||
time.Since(startTime), graphCache.Stats())
|
||||
|
||||
store.setGraphCache(graphCache)
|
||||
|
||||
return &ChannelGraph{
|
||||
KVStore: store,
|
||||
KVStore: store,
|
||||
graphCache: graphCache,
|
||||
}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user