@@ -20,9 +20,9 @@ import (
20
20
)
21
21
22
22
const (
23
- // queryPaginationLimit is used in the LIMIT clause of the SQL queries
24
- // to limit the number of rows returned.
25
- queryPaginationLimit = 100
23
+ // defaultQueryPaginationLimit is used in the LIMIT clause of the SQL
24
+ // queries to limit the number of rows returned.
25
+ defaultQueryPaginationLimit = 100
26
26
)
27
27
28
28
// SQLInvoiceQueries is an interface that defines the set of operations that can
@@ -152,16 +152,47 @@ type BatchedSQLInvoiceQueries interface {
152
152
type SQLStore struct {
153
153
db BatchedSQLInvoiceQueries
154
154
clock clock.Clock
155
+ opts SQLStoreOptions
156
+ }
157
+
158
+ // SQLStoreOptions holds the options for the SQL store.
159
+ type SQLStoreOptions struct {
160
+ paginationLimit int
161
+ }
162
+
163
+ // defaultSQLStoreOptions returns the default options for the SQL store.
164
+ func defaultSQLStoreOptions () SQLStoreOptions {
165
+ return SQLStoreOptions {
166
+ paginationLimit : defaultQueryPaginationLimit ,
167
+ }
168
+ }
169
+
170
+ // SQLStoreOption is a functional option that can be used to optionally modify
171
+ // the behavior of the SQL store.
172
+ type SQLStoreOption func (* SQLStoreOptions )
173
+
174
+ // WithPaginationLimit sets the pagination limit for the SQL store queries that
175
+ // paginate results.
176
+ func WithPaginationLimit (limit int ) SQLStoreOption {
177
+ return func (o * SQLStoreOptions ) {
178
+ o .paginationLimit = limit
179
+ }
155
180
}
156
181
157
182
// NewSQLStore creates a new SQLStore instance given a open
158
183
// BatchedSQLInvoiceQueries storage backend.
159
184
func NewSQLStore (db BatchedSQLInvoiceQueries ,
160
- clock clock.Clock ) * SQLStore {
185
+ clock clock.Clock , options ... SQLStoreOption ) * SQLStore {
186
+
187
+ opts := defaultSQLStoreOptions ()
188
+ for _ , applyOption := range options {
189
+ applyOption (& opts )
190
+ }
161
191
162
192
return & SQLStore {
163
193
db : db ,
164
194
clock : clock ,
195
+ opts : opts ,
165
196
}
166
197
}
167
198
@@ -617,13 +648,11 @@ func (i *SQLStore) FetchPendingInvoices(ctx context.Context) (
617
648
618
649
readTxOpt := NewSQLInvoiceQueryReadTx ()
619
650
err := i .db .ExecTx (ctx , & readTxOpt , func (db SQLInvoiceQueries ) error {
620
- limit := queryPaginationLimit
621
-
622
651
return queryWithLimit (func (offset int ) (int , error ) {
623
652
params := sqlc.FilterInvoicesParams {
624
653
PendingOnly : true ,
625
654
NumOffset : int32 (offset ),
626
- NumLimit : int32 (limit ),
655
+ NumLimit : int32 (i . opts . paginationLimit ),
627
656
Reverse : false ,
628
657
}
629
658
@@ -646,7 +675,7 @@ func (i *SQLStore) FetchPendingInvoices(ctx context.Context) (
646
675
}
647
676
648
677
return len (rows ), nil
649
- }, limit )
678
+ }, i . opts . paginationLimit )
650
679
}, func () {
651
680
invoices = make (map [lntypes.Hash ]Invoice )
652
681
})
@@ -660,8 +689,7 @@ func (i *SQLStore) FetchPendingInvoices(ctx context.Context) (
660
689
661
690
// InvoicesSettledSince can be used by callers to catch up any settled invoices
662
691
// they missed within the settled invoice time series. We'll return all known
663
- // settled invoice that have a settle index higher than the passed
664
- // sinceSettleIndex.
692
+ // settled invoice that have a settle index higher than the passed idx.
665
693
//
666
694
// NOTE: The index starts from 1. As a result we enforce that specifying a value
667
695
// below the starting index value is a noop.
@@ -676,14 +704,11 @@ func (i *SQLStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
676
704
677
705
readTxOpt := NewSQLInvoiceQueryReadTx ()
678
706
err := i .db .ExecTx (ctx , & readTxOpt , func (db SQLInvoiceQueries ) error {
679
- settleIdx := idx
680
- limit := queryPaginationLimit
681
-
682
707
err := queryWithLimit (func (offset int ) (int , error ) {
683
708
params := sqlc.FilterInvoicesParams {
684
- SettleIndexGet : sqldb .SQLInt64 (settleIdx + 1 ),
685
- NumLimit : int32 (limit ),
709
+ SettleIndexGet : sqldb .SQLInt64 (idx + 1 ),
686
710
NumOffset : int32 (offset ),
711
+ NumLimit : int32 (i .opts .paginationLimit ),
687
712
Reverse : false ,
688
713
}
689
714
@@ -707,10 +732,8 @@ func (i *SQLStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
707
732
invoices = append (invoices , * invoice )
708
733
}
709
734
710
- settleIdx += uint64 (limit )
711
-
712
735
return len (rows ), nil
713
- }, limit )
736
+ }, i . opts . paginationLimit )
714
737
if err != nil {
715
738
return err
716
739
}
@@ -777,7 +800,7 @@ func (i *SQLStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
777
800
778
801
// InvoicesAddedSince can be used by callers to seek into the event time series
779
802
// of all the invoices added in the database. This method will return all
780
- // invoices with an add index greater than the specified sinceAddIndex .
803
+ // invoices with an add index greater than the specified idx .
781
804
//
782
805
// NOTE: The index starts from 1. As a result we enforce that specifying a value
783
806
// below the starting index value is a noop.
@@ -792,14 +815,11 @@ func (i *SQLStore) InvoicesAddedSince(ctx context.Context, idx uint64) (
792
815
793
816
readTxOpt := NewSQLInvoiceQueryReadTx ()
794
817
err := i .db .ExecTx (ctx , & readTxOpt , func (db SQLInvoiceQueries ) error {
795
- addIdx := idx
796
- limit := queryPaginationLimit
797
-
798
818
return queryWithLimit (func (offset int ) (int , error ) {
799
819
params := sqlc.FilterInvoicesParams {
800
- AddIndexGet : sqldb .SQLInt64 (addIdx + 1 ),
801
- NumLimit : int32 (limit ),
820
+ AddIndexGet : sqldb .SQLInt64 (idx + 1 ),
802
821
NumOffset : int32 (offset ),
822
+ NumLimit : int32 (i .opts .paginationLimit ),
803
823
Reverse : false ,
804
824
}
805
825
@@ -821,10 +841,8 @@ func (i *SQLStore) InvoicesAddedSince(ctx context.Context, idx uint64) (
821
841
result = append (result , * invoice )
822
842
}
823
843
824
- addIdx += uint64 (limit )
825
-
826
844
return len (rows ), nil
827
- }, limit )
845
+ }, i . opts . paginationLimit )
828
846
}, func () {
829
847
result = nil
830
848
})
@@ -851,41 +869,34 @@ func (i *SQLStore) QueryInvoices(ctx context.Context,
851
869
852
870
readTxOpt := NewSQLInvoiceQueryReadTx ()
853
871
err := i .db .ExecTx (ctx , & readTxOpt , func (db SQLInvoiceQueries ) error {
854
- limit := queryPaginationLimit
855
-
856
872
return queryWithLimit (func (offset int ) (int , error ) {
857
873
params := sqlc.FilterInvoicesParams {
858
874
NumOffset : int32 (offset ),
859
- NumLimit : int32 (limit ),
875
+ NumLimit : int32 (i . opts . paginationLimit ),
860
876
PendingOnly : q .PendingOnly ,
877
+ Reverse : q .Reversed ,
861
878
}
862
879
863
880
if q .Reversed {
864
- idx := int32 (q .IndexOffset )
865
-
866
881
// If the index offset was not set, we want to
867
882
// fetch from the lastest invoice.
868
- if idx == 0 {
883
+ if q . IndexOffset == 0 {
869
884
params .AddIndexLet = sqldb .SQLInt64 (
870
885
int64 (math .MaxInt64 ),
871
886
)
872
887
} else {
873
888
// The invoice with index offset id must
874
889
// not be included in the results.
875
890
params .AddIndexLet = sqldb .SQLInt64 (
876
- idx - int32 ( offset ) - 1 ,
891
+ q . IndexOffset - 1 ,
877
892
)
878
893
}
879
-
880
- params .Reverse = true
881
894
} else {
882
895
// The invoice with index offset id must not be
883
896
// included in the results.
884
897
params .AddIndexGet = sqldb .SQLInt64 (
885
- q .IndexOffset + uint64 ( offset ) + 1 ,
898
+ q .IndexOffset + 1 ,
886
899
)
887
-
888
- params .Reverse = false
889
900
}
890
901
891
902
if q .CreationDateStart != 0 {
@@ -923,7 +934,7 @@ func (i *SQLStore) QueryInvoices(ctx context.Context,
923
934
}
924
935
925
936
return len (rows ), nil
926
- }, limit )
937
+ }, i . opts . paginationLimit )
927
938
}, func () {
928
939
invoices = nil
929
940
})
0 commit comments