channeldb+graphdb: init graph DB outside of channel db
We also now use the graph DB's own optional functions. An instance of the graph is currently still passed to the channeldb's `CreateWithBackend` function. This will be removed in a later commit once the two have been completely disjoint.
This commit is contained in:
11 files changed
+98
-203
No files matched your search
+14
-10
@@ -198,11 +198,15 @@ type ChannelGraph struct {
|
||||
|
||||
// NewChannelGraph allocates a new ChannelGraph backed by a DB instance. The
|
||||
// returned instance has its own unique reject cache and channel cache.
|
||||
func NewChannelGraph(db kvdb.Backend, rejectCacheSize, chanCacheSize int,
|
||||
batchCommitInterval time.Duration, preAllocCacheNumNodes int,
|
||||
useGraphCache, noMigrations bool) (*ChannelGraph, error) {
|
||||
func NewChannelGraph(db kvdb.Backend, options ...OptionModifier) (*ChannelGraph,
|
||||
error) {
|
||||
|
||||
if !noMigrations {
|
||||
opts := DefaultOptions()
|
||||
for _, o := range options {
|
||||
o(opts)
|
||||
}
|
||||
|
||||
if !opts.NoMigration {
|
||||
if err := initChannelGraph(db); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -210,20 +214,20 @@ func NewChannelGraph(db kvdb.Backend, rejectCacheSize, chanCacheSize int,
|
||||
|
||||
g := &ChannelGraph{
|
||||
db: db,
|
||||
rejectCache: newRejectCache(rejectCacheSize),
|
||||
chanCache: newChannelCache(chanCacheSize),
|
||||
rejectCache: newRejectCache(opts.RejectCacheSize),
|
||||
chanCache: newChannelCache(opts.ChannelCacheSize),
|
||||
}
|
||||
g.chanScheduler = batch.NewTimeScheduler(
|
||||
db, &g.cacheMu, batchCommitInterval,
|
||||
db, &g.cacheMu, opts.BatchCommitInterval,
|
||||
)
|
||||
g.nodeScheduler = batch.NewTimeScheduler(
|
||||
db, nil, batchCommitInterval,
|
||||
db, nil, opts.BatchCommitInterval,
|
||||
)
|
||||
|
||||
// The graph cache can be turned off (e.g. for mobile users) for a
|
||||
// speed/memory usage tradeoff.
|
||||
if useGraphCache {
|
||||
g.graphCache = NewGraphCache(preAllocCacheNumNodes)
|
||||
if opts.UseGraphCache {
|
||||
g.graphCache = NewGraphCache(opts.PreAllocCacheNumNodes)
|
||||
startTime := time.Now()
|
||||
log.Debugf("Populating in-memory channel graph, this might " +
|
||||
"take a while...")
|
||||
|
||||
+3
-16
@@ -79,11 +79,7 @@ func MakeTestGraph(t testing.TB, modifiers ...OptionModifier) (*ChannelGraph, er
|
||||
return nil, err
|
||||
}
|
||||
|
||||
graph, err := NewChannelGraph(
|
||||
backend, opts.RejectCacheSize, opts.ChannelCacheSize,
|
||||
opts.BatchCommitInterval, opts.PreAllocCacheNumNodes,
|
||||
true, false,
|
||||
)
|
||||
graph, err := NewChannelGraph(backend)
|
||||
if err != nil {
|
||||
backendCleanup()
|
||||
return nil, err
|
||||
@@ -4016,12 +4012,7 @@ func TestGraphLoading(t *testing.T) {
|
||||
defer backend.Close()
|
||||
defer backendCleanup()
|
||||
|
||||
opts := DefaultOptions()
|
||||
graph, err := NewChannelGraph(
|
||||
backend, opts.RejectCacheSize, opts.ChannelCacheSize,
|
||||
opts.BatchCommitInterval, opts.PreAllocCacheNumNodes,
|
||||
true, false,
|
||||
)
|
||||
graph, err := NewChannelGraph(backend)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Populate the graph with test data.
|
||||
@@ -4031,11 +4022,7 @@ func TestGraphLoading(t *testing.T) {
|
||||
|
||||
// Recreate the graph. This should cause the graph cache to be
|
||||
// populated.
|
||||
graphReloaded, err := NewChannelGraph(
|
||||
backend, opts.RejectCacheSize, opts.ChannelCacheSize,
|
||||
opts.BatchCommitInterval, opts.PreAllocCacheNumNodes,
|
||||
true, false,
|
||||
)
|
||||
graphReloaded, err := NewChannelGraph(backend)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Assert that the cache content is identical.
|
||||
|
||||
@@ -63,6 +63,13 @@ func DefaultOptions() *Options {
|
||||
// OptionModifier is a function signature for modifying the default Options.
|
||||
type OptionModifier func(*Options)
|
||||
|
||||
// WithRejectCacheSize sets the RejectCacheSize to n.
|
||||
func WithRejectCacheSize(n int) OptionModifier {
|
||||
return func(o *Options) {
|
||||
o.RejectCacheSize = n
|
||||
}
|
||||
}
|
||||
|
||||
// WithChannelCacheSize sets the ChannelCacheSize to n.
|
||||
func WithChannelCacheSize(n int) OptionModifier {
|
||||
return func(o *Options) {
|
||||
|
||||
Reference in new issue
Block a user