Skip to content

Commit 8de9542

Browse files
committed
graph/db: define and init the reject and chan caches
In this commit we add and initialise the reject and channel caches in the SQLStore as is done in the KVStore. These are not used yet.
1 parent 9fbb7a5 commit 8de9542

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

graph/db/options.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,44 @@ func WithPreAllocCacheNumNodes(n int) ChanGraphOption {
6161
}
6262
}
6363

64+
// SQLStoreOptions holds parameters for tuning and customizing a SQLStore
65+
// instance.
66+
type SQLStoreOptions struct {
67+
// RejectCacheSize is the maximum number of rejectCacheEntries to hold
68+
// in the rejection cache.
69+
RejectCacheSize int
70+
71+
// ChannelCacheSize is the maximum number of ChannelEdges to hold in the
72+
// channel cache.
73+
ChannelCacheSize int
74+
}
75+
76+
// DefaultSQLOptions returns a SQLStoreOptions populated with default values.
77+
func DefaultSQLOptions() *SQLStoreOptions {
78+
return &SQLStoreOptions{
79+
RejectCacheSize: DefaultRejectCacheSize,
80+
ChannelCacheSize: DefaultChannelCacheSize,
81+
}
82+
}
83+
84+
// SQLStoreOption defines the function signature for a functional option
85+
// that can be used to customize a SQLStoreOptions instance.
86+
type SQLStoreOption func(*SQLStoreOptions)
87+
88+
// WithSQLStoreRejectCacheSize sets the RejectCacheSize to n.
89+
func WithSQLStoreRejectCacheSize(n int) SQLStoreOption {
90+
return func(o *SQLStoreOptions) {
91+
o.RejectCacheSize = n
92+
}
93+
}
94+
95+
// WithSQLStoreChannelCacheSize sets the ChannelCacheSize to n.
96+
func WithSQLStoreChannelCacheSize(n int) SQLStoreOption {
97+
return func(o *SQLStoreOptions) {
98+
o.ChannelCacheSize = n
99+
}
100+
}
101+
64102
// KVStoreOptions holds parameters for tuning and customizing a graph.DB.
65103
type KVStoreOptions struct {
66104
// RejectCacheSize is the maximum number of rejectCacheEntries to hold

graph/db/sql_store.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package graphdb
22

33
import (
4+
"sync"
5+
46
"github.com/lightningnetwork/lnd/sqldb"
57
)
68

@@ -26,6 +28,14 @@ type BatchedSQLQueries interface {
2628
type SQLStore struct {
2729
db BatchedSQLQueries
2830

31+
// cacheMu guards all caches (rejectCache and chanCache). If
32+
// this mutex will be acquired at the same time as the DB mutex (ie if
33+
// a transaction is starting) then the cacheMu MUST be acquired first to
34+
// prevent deadlock.
35+
cacheMu sync.RWMutex
36+
rejectCache *rejectCache
37+
chanCache *channelCache
38+
2939
// Temporary fall-back to the KVStore so that we can implement the
3040
// interface incrementally.
3141
*KVStore
@@ -37,9 +47,18 @@ var _ V1Store = (*SQLStore)(nil)
3747

3848
// NewSQLStore creates a new SQLStore instance given an open BatchedSQLQueries
3949
// storage backend.
40-
func NewSQLStore(db BatchedSQLQueries, kvStore *KVStore) *SQLStore {
50+
func NewSQLStore(db BatchedSQLQueries, kvStore *KVStore,
51+
options ...SQLStoreOption) *SQLStore {
52+
53+
opts := DefaultSQLOptions()
54+
for _, o := range options {
55+
o(opts)
56+
}
57+
4158
return &SQLStore{
42-
db: db,
43-
KVStore: kvStore,
59+
db: db,
60+
KVStore: kvStore,
61+
rejectCache: newRejectCache(opts.RejectCacheSize),
62+
chanCache: newChannelCache(opts.ChannelCacheSize),
4463
}
4564
}

0 commit comments

Comments
 (0)