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

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

23726.46 hits per line

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

93.22
/channeldb/paginate.go
1
package channeldb
2

3
import "github.com/lightningnetwork/lnd/kvdb"
4

5
type paginator struct {
6
        // cursor is the cursor which we are using to iterate through a bucket.
7
        cursor kvdb.RCursor
8

9
        // reversed indicates whether we are paginating forwards or backwards.
10
        reversed bool
11

12
        // indexOffset is the index from which we will begin querying.
13
        indexOffset uint64
14

15
        // totalItems is the total number of items we allow in our response.
16
        totalItems uint64
17
}
18

19
// newPaginator returns a struct which can be used to query an indexed bucket
20
// in pages.
21
func newPaginator(c kvdb.RCursor, reversed bool,
22
        indexOffset, totalItems uint64) paginator {
70✔
23

70✔
24
        return paginator{
70✔
25
                cursor:      c,
70✔
26
                reversed:    reversed,
70✔
27
                indexOffset: indexOffset,
70✔
28
                totalItems:  totalItems,
70✔
29
        }
70✔
30
}
70✔
31

32
// keyValueForIndex seeks our cursor to a given index and returns the key and
33
// value at that position.
34
func (p paginator) keyValueForIndex(index uint64) ([]byte, []byte) {
80✔
35
        var keyIndex [8]byte
80✔
36
        byteOrder.PutUint64(keyIndex[:], index)
80✔
37
        return p.cursor.Seek(keyIndex[:])
80✔
38
}
80✔
39

40
// lastIndex returns the last value in our index, if our index is empty it
41
// returns 0.
42
func (p paginator) lastIndex() uint64 {
14✔
43
        keyIndex, _ := p.cursor.Last()
14✔
44
        if keyIndex == nil {
14✔
45
                return 0
×
46
        }
×
47

48
        return byteOrder.Uint64(keyIndex)
14✔
49
}
50

51
// nextKey is a helper closure to determine what key we should use next when
52
// we are iterating, depending on whether we are iterating forwards or in
53
// reverse.
54
func (p paginator) nextKey() ([]byte, []byte) {
1,002✔
55
        if p.reversed {
1,268✔
56
                return p.cursor.Prev()
266✔
57
        }
266✔
58
        return p.cursor.Next()
736✔
59
}
60

61
// cursorStart gets the index key and value for the first item we are looking
62
// up, taking into account that we may be paginating in reverse. The index
63
// offset provided is *excusive* so we will start with the item after the offset
64
// for forwards queries, and the item before the index for backwards queries.
65
func (p paginator) cursorStart() ([]byte, []byte) {
70✔
66
        indexKey, indexValue := p.keyValueForIndex(p.indexOffset + 1)
70✔
67

70✔
68
        // If the query is specifying reverse iteration, then we must
70✔
69
        // handle a few offset cases.
70✔
70
        if p.reversed {
92✔
71
                switch {
22✔
72
                // This indicates the default case, where no offset was
73
                // specified. In that case we just start from the last
74
                // entry.
75
                case p.indexOffset == 0:
6✔
76
                        indexKey, indexValue = p.cursor.Last()
6✔
77

78
                // This indicates the offset being set to the very
79
                // first entry. Since there are no entries before
80
                // this offset, and the direction is reversed, we can
81
                // return without adding any invoices to the response.
82
                case p.indexOffset == 1:
2✔
83
                        return nil, nil
2✔
84

85
                // If we have been given an index offset that is beyond our last
86
                // index value, we just return the last indexed value in our set
87
                // since we are querying in reverse. We do not cover the case
88
                // where our index offset equals our last index value, because
89
                // index offset is exclusive, so we would want to start at the
90
                // value before our last index.
91
                case p.indexOffset > p.lastIndex():
4✔
92
                        return p.cursor.Last()
4✔
93

94
                // Otherwise we have an index offset which is within our set of
95
                // indexed keys, and we want to start at the item before our
96
                // offset. We seek to our index offset, then return the element
97
                // before it. We do this rather than p.indexOffset-1 to account
98
                // for indexes that have gaps.
99
                default:
10✔
100
                        p.keyValueForIndex(p.indexOffset)
10✔
101
                        indexKey, indexValue = p.cursor.Prev()
10✔
102
                }
103
        }
104

105
        return indexKey, indexValue
64✔
106
}
107

108
// query gets the start point for our index offset and iterates through keys
109
// in our index until we reach the total number of items required for the query
110
// or we run out of cursor values. This function takes a fetchAndAppend function
111
// which is responsible for looking up the entry at that index, adding the entry
112
// to its set of return items (if desired) and return a boolean which indicates
113
// whether the item was added. This is required to allow the paginator to
114
// determine when the response has the maximum number of required items.
115
func (p paginator) query(fetchAndAppend func(k, v []byte) (bool, error)) error {
70✔
116
        indexKey, indexValue := p.cursorStart()
70✔
117

70✔
118
        var totalItems int
70✔
119
        for ; indexKey != nil; indexKey, indexValue = p.nextKey() {
1,086✔
120
                // If our current return payload exceeds the max number
1,016✔
121
                // of invoices, then we'll exit now.
1,016✔
122
                if uint64(totalItems) >= p.totalItems {
1,030✔
123
                        break
14✔
124
                }
125

126
                added, err := fetchAndAppend(indexKey, indexValue)
1,002✔
127
                if err != nil {
1,002✔
128
                        return err
×
129
                }
×
130

131
                // If we added an item to our set in the latest fetch and append
132
                // we increment our total count.
133
                if added {
1,558✔
134
                        totalItems++
556✔
135
                }
556✔
136
        }
137

138
        return nil
70✔
139
}
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