• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 16313565012

16 Jul 2025 07:46AM UTC coverage: 67.205% (-0.1%) from 67.321%
16313565012

Pull #10081

github

web-flow
Merge bf32adb8a into 9059a4e7b
Pull Request #10081: graph/db: use `/*SLICE:<field_name>*/` to optimise various graph queries

0 of 379 new or added lines in 4 files covered. (0.0%)

99 existing lines in 24 files now uncovered.

135374 of 201433 relevant lines covered (67.21%)

21718.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/sqldb/paginate.go
1
package sqldb
2

3
import (
4
        "context"
5
        "fmt"
6
)
7

8
// PagedQueryFunc represents a function that takes a slice of converted items
9
// and returns results.
10
type PagedQueryFunc[T any, R any] func(context.Context, []T) ([]R, error)
11

12
// ItemCallbackFunc represents a function that processes individual results.
13
type ItemCallbackFunc[R any] func(context.Context, R) error
14

15
// ConvertFunc represents a function that converts from input type to query type
16
type ConvertFunc[I any, T any] func(I) T
17

18
// PagedQueryConfig holds configuration values for calls to ExecutePagedQuery.
19
type PagedQueryConfig struct {
20
        PageSize int
21
}
22

23
// DefaultPagedQueryConfig returns a default configuration
NEW
24
func DefaultPagedQueryConfig() *PagedQueryConfig {
×
NEW
25
        return &PagedQueryConfig{
×
NEW
26
                PageSize: 1000,
×
NEW
27
        }
×
NEW
28
}
×
29

30
// ExecutePagedQuery executes a paginated query over a slice of input items.
31
// It converts the input items to a query type using the provided convertFunc,
32
// executes the query using the provided queryFunc, and applies the callback
33
// to each result.
34
func ExecutePagedQuery[I any, T any, R any](ctx context.Context,
35
        cfg *PagedQueryConfig, inputItems []I, convertFunc ConvertFunc[I, T],
NEW
36
        queryFunc PagedQueryFunc[T, R], callback ItemCallbackFunc[R]) error {
×
NEW
37

×
NEW
38
        if len(inputItems) == 0 {
×
NEW
39
                return nil
×
NEW
40
        }
×
41

42
        // Process items in pages.
NEW
43
        for i := 0; i < len(inputItems); i += cfg.PageSize {
×
NEW
44
                // Calculate the end index for this page.
×
NEW
45
                end := i + cfg.PageSize
×
NEW
46
                if end > len(inputItems) {
×
NEW
47
                        end = len(inputItems)
×
NEW
48
                }
×
49

50
                // Get the page slice of input items.
NEW
51
                inputPage := inputItems[i:end]
×
NEW
52

×
NEW
53
                // Convert only the items needed for this page.
×
NEW
54
                convertedPage := make([]T, len(inputPage))
×
NEW
55
                for j, inputItem := range inputPage {
×
NEW
56
                        convertedPage[j] = convertFunc(inputItem)
×
NEW
57
                }
×
58

59
                // Execute the query for this page.
NEW
60
                results, err := queryFunc(ctx, convertedPage)
×
NEW
61
                if err != nil {
×
NEW
62
                        return fmt.Errorf("query failed for page "+
×
NEW
63
                                "starting at %d: %w", i, err)
×
NEW
64
                }
×
65

66
                // Apply the callback to each result.
NEW
67
                for _, result := range results {
×
NEW
68
                        if err := callback(ctx, result); err != nil {
×
NEW
69
                                return fmt.Errorf("callback failed for "+
×
NEW
70
                                        "result: %w", err)
×
NEW
71
                        }
×
72
                }
73
        }
74

NEW
75
        return nil
×
76
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc