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

lightningnetwork / lnd / 12199391122

06 Dec 2024 01:10PM UTC coverage: 49.807% (-9.1%) from 58.933%
12199391122

push

github

web-flow
Merge pull request #9337 from Guayaba221/patch-1

chore: fix typo in ruby.md

100137 of 201051 relevant lines covered (49.81%)

2.07 hits per line

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

64.41
/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 {
4✔
23

4✔
24
        return paginator{
4✔
25
                cursor:      c,
4✔
26
                reversed:    reversed,
4✔
27
                indexOffset: indexOffset,
4✔
28
                totalItems:  totalItems,
4✔
29
        }
4✔
30
}
4✔
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) {
4✔
35
        var keyIndex [8]byte
4✔
36
        byteOrder.PutUint64(keyIndex[:], index)
4✔
37
        return p.cursor.Seek(keyIndex[:])
4✔
38
}
4✔
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 {
×
43
        keyIndex, _ := p.cursor.Last()
×
44
        if keyIndex == nil {
×
45
                return 0
×
46
        }
×
47

48
        return byteOrder.Uint64(keyIndex)
×
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) {
4✔
55
        if p.reversed {
4✔
56
                return p.cursor.Prev()
×
57
        }
×
58
        return p.cursor.Next()
4✔
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) {
4✔
66
        indexKey, indexValue := p.keyValueForIndex(p.indexOffset + 1)
4✔
67

4✔
68
        // If the query is specifying reverse iteration, then we must
4✔
69
        // handle a few offset cases.
4✔
70
        if p.reversed {
4✔
71
                switch {
×
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:
×
76
                        indexKey, indexValue = p.cursor.Last()
×
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:
×
83
                        return nil, nil
×
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():
×
92
                        return p.cursor.Last()
×
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:
×
100
                        p.keyValueForIndex(p.indexOffset)
×
101
                        indexKey, indexValue = p.cursor.Prev()
×
102
                }
103
        }
104

105
        return indexKey, indexValue
4✔
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 {
4✔
116
        indexKey, indexValue := p.cursorStart()
4✔
117

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

126
                added, err := fetchAndAppend(indexKey, indexValue)
4✔
127
                if err != nil {
4✔
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 {
8✔
134
                        totalItems++
4✔
135
                }
4✔
136
        }
137

138
        return nil
4✔
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