@@ -3,6 +3,7 @@ package lnd
33import (
44 "bytes"
55 "context"
6+ "database/sql"
67 "errors"
78 "fmt"
89 "io/ioutil"
@@ -31,6 +32,7 @@ import (
3132 "github.com/lightningnetwork/lnd/chainntnfs"
3233 "github.com/lightningnetwork/lnd/chainreg"
3334 "github.com/lightningnetwork/lnd/channeldb"
35+ "github.com/lightningnetwork/lnd/clock"
3436 "github.com/lightningnetwork/lnd/invoices"
3537 "github.com/lightningnetwork/lnd/keychain"
3638 "github.com/lightningnetwork/lnd/kvdb"
@@ -42,6 +44,7 @@ import (
4244 "github.com/lightningnetwork/lnd/macaroons"
4345 "github.com/lightningnetwork/lnd/rpcperms"
4446 "github.com/lightningnetwork/lnd/signal"
47+ "github.com/lightningnetwork/lnd/sqldb"
4548 "github.com/lightningnetwork/lnd/walletunlocker"
4649 "github.com/lightningnetwork/lnd/watchtower"
4750 "github.com/lightningnetwork/lnd/watchtower/wtclient"
@@ -869,6 +872,11 @@ type DatabaseInstances struct {
869872 // WalletDB is the configuration for loading the wallet database using
870873 // the btcwallet's loader.
871874 WalletDB btcwallet.LoaderOption
875+
876+ // NativeSQLStore is a pointer to a native SQL store that can be used
877+ // for native SQL queries for tables that already support it. This may
878+ // be nil if the use-native-sql flag was not set.
879+ NativeSQLStore * sqldb.BaseDB
872880}
873881
874882// DefaultDatabaseBuilder is a type that builds the default database backends
@@ -923,10 +931,11 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
923931 // state DB point to the same local or remote DB and the same namespace
924932 // within that DB.
925933 dbs := & DatabaseInstances {
926- HeightHintDB : databaseBackends .HeightHintDB ,
927- MacaroonDB : databaseBackends .MacaroonDB ,
928- DecayedLogDB : databaseBackends .DecayedLogDB ,
929- WalletDB : databaseBackends .WalletDB ,
934+ HeightHintDB : databaseBackends .HeightHintDB ,
935+ MacaroonDB : databaseBackends .MacaroonDB ,
936+ DecayedLogDB : databaseBackends .DecayedLogDB ,
937+ WalletDB : databaseBackends .WalletDB ,
938+ NativeSQLStore : databaseBackends .NativeSQLStore ,
930939 }
931940 cleanUp := func () {
932941 // We can just close the returned close functions directly. Even
@@ -1011,11 +1020,21 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
10111020 // using the same struct (and DB backend) instance.
10121021 dbs .ChanStateDB = dbs .GraphDB
10131022
1014- // For now the only InvoiceDB implementation is the *channeldb.DB.
1015- //
1016- // TODO(positiveblue): use a sql first implementation for this
1017- // interface.
1018- dbs .InvoiceDB = dbs .GraphDB
1023+ // Instantiate a native SQL invoice store if the flag is set.
1024+ if d .cfg .DB .UseNativeSQL {
1025+ executor := sqldb .NewTransactionExecutor (
1026+ dbs .NativeSQLStore ,
1027+ func (tx * sql.Tx ) sqldb.InvoiceQueries {
1028+ return dbs .NativeSQLStore .WithTx (tx )
1029+ },
1030+ )
1031+
1032+ dbs .InvoiceDB = sqldb .NewInvoiceStore (
1033+ executor , clock .NewDefaultClock (),
1034+ )
1035+ } else {
1036+ dbs .InvoiceDB = dbs .GraphDB
1037+ }
10191038
10201039 // Wrap the watchtower client DB and make sure we clean up.
10211040 if cfg .WtClient .Active {
@@ -1298,8 +1317,9 @@ func initNeutrinoBackend(ctx context.Context, cfg *Config, chainDir string,
12981317 )
12991318 switch {
13001319 case cfg .DB .Backend == kvdb .SqliteBackendName :
1320+ sqliteConfig := lncfg .GetSqliteConfigKVDB (cfg .DB .Sqlite )
13011321 db , err = kvdb .Open (
1302- kvdb .SqliteBackendName , ctx , cfg . DB . Sqlite , dbPath ,
1322+ kvdb .SqliteBackendName , ctx , sqliteConfig , dbPath ,
13031323 lncfg .SqliteNeutrinoDBName , lncfg .NSNeutrinoDB ,
13041324 )
13051325
0 commit comments