lncfg: instantiate the native SQL backend if configured

This commit adds an optional separate "native" SQL backend that will
host all tables supporting native SQL. For now since we don't have many
such tables we'll keep it in one file for SQLite, but it may be split up
if desired.
This commit is contained in:
Andras Banki-Horvath 2023-12-06 17:15:15 +01:00
parent debfaf41b9
commit e31879ea0a
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8

View File

@ -3,6 +3,7 @@ package lncfg
import (
"context"
"fmt"
"path"
"path/filepath"
"time"
@ -29,6 +30,7 @@ const (
SqliteChainDBName = "chain.sqlite"
SqliteNeutrinoDBName = "neutrino.sqlite"
SqliteTowerDBName = "watchtower.sqlite"
SqliteNativeDBName = "lnd.sqlite"
BoltBackend = "bolt"
EtcdBackend = "etcd"
@ -83,6 +85,8 @@ type DB struct {
Sqlite *sqldb.SqliteConfig `group:"sqlite" namespace:"sqlite" description:"Sqlite settings."`
UseNativeSQL bool `long:"use-native-sql" description:"Use native SQL for tables that already support it."`
NoGraphCache bool `long:"no-graph-cache" description:"Don't use the in-memory graph cache for path finding. Much slower but uses less RAM. Can only be used with a bolt database backend."`
PruneRevocation bool `long:"prune-revocation" description:"Run the optional migration that prunes the revocation logs to save disk space."`
@ -111,6 +115,7 @@ func DefaultDB() *DB {
MaxConnections: defaultSqliteMaxConnections,
BusyTimeout: defaultSqliteBusyTimeout,
},
UseNativeSQL: false,
}
}
@ -215,6 +220,11 @@ type DatabaseBackends struct {
// the underlying wallet database from.
WalletDB btcwallet.LoaderOption
// NativeSQLStore is a pointer to a native SQL store that can be used
// for native SQL queries for tables that already support it. This may
// be nil if the use-native-sql flag was not set.
NativeSQLStore *sqldb.BaseDB
// Remote indicates whether the database backends are remote, possibly
// replicated instances or local bbolt or sqlite backed databases.
Remote bool
@ -428,6 +438,20 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
}
closeFuncs[NSWalletDB] = postgresWalletBackend.Close
var nativeSQLStore *sqldb.BaseDB
if db.UseNativeSQL {
nativePostgresStore, err := sqldb.NewPostgresStore(
db.Postgres,
)
if err != nil {
return nil, fmt.Errorf("error opening "+
"native postgres store: %v", err)
}
nativeSQLStore = nativePostgresStore.BaseDB
closeFuncs[PostgresBackend] = nativePostgresStore.Close
}
// Warn if the user is trying to switch over to a Postgres DB
// while there is a wallet or channel bbolt DB still present.
warnExistingBoltDBs(
@ -455,8 +479,9 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
WalletDB: btcwallet.LoaderWithExternalWalletDB(
postgresWalletBackend,
),
Remote: true,
CloseFuncs: closeFuncs,
NativeSQLStore: nativeSQLStore,
Remote: true,
CloseFuncs: closeFuncs,
}, nil
case SqliteBackend:
@ -535,6 +560,21 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
}
closeFuncs[NSWalletDB] = sqliteWalletBackend.Close
var nativeSQLStore *sqldb.BaseDB
if db.UseNativeSQL {
nativeSQLiteStore, err := sqldb.NewSqliteStore(
db.Sqlite,
path.Join(chanDBPath, SqliteNativeDBName),
)
if err != nil {
return nil, fmt.Errorf("error opening "+
"native SQLite store: %v", err)
}
nativeSQLStore = nativeSQLiteStore.BaseDB
closeFuncs[SqliteBackend] = nativeSQLiteStore.Close
}
// Warn if the user is trying to switch over to a sqlite DB
// while there is a wallet or channel bbolt DB still present.
warnExistingBoltDBs(
@ -562,7 +602,8 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
WalletDB: btcwallet.LoaderWithExternalWalletDB(
sqliteWalletBackend,
),
CloseFuncs: closeFuncs,
NativeSQLStore: nativeSQLStore,
CloseFuncs: closeFuncs,
}, nil
}