Skip to content

Commit 7571acb

Browse files
committed
channeldb: remove unnecessary for loop from Query method
This commit simplifies the code of the ForwardingLog.Query method by removing a confusing for-loop. The for-loop makes it seem as though multiple events could be encoded under a single timestamp. But from the time that this forwarding log was introduced, it was never possible to encode multiple events under the same timestamp and so this loop will never execute successfully more than once per timestamp and can thus be removed. This paves the way such that future expansions of the method can be added easily. See the initial commit that introduced this code [here](f2cd668). In this commit you can see that from the start it was never possible to have more than one event in a single timestamp since any previous event in that timestamp would be overwritten. Then see [this commit](97c7370) where even more protection was added to ensure that each event had a unique timestamp.
1 parent fc906f2 commit 7571acb

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

channeldb/forwarding_log.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ type ForwardingLogTimeSlice struct {
227227
// the number of events to be returned.
228228
//
229229
// TODO(roasbeef): rename?
230-
func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice, error) {
230+
func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice,
231+
error) {
232+
231233
var resp ForwardingLogTimeSlice
232234

233235
// If the user provided an index offset, then we'll not know how many
@@ -256,8 +258,9 @@ func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice, e
256258
// We'll continue until either we reach the end of the range,
257259
// or reach our max number of events.
258260
logCursor := logBucket.ReadCursor()
259-
timestamp, events := logCursor.Seek(startTime[:])
260-
for ; timestamp != nil && bytes.Compare(timestamp, endTime[:]) <= 0; timestamp, events = logCursor.Next() {
261+
timestamp, eventBytes := logCursor.Seek(startTime[:])
262+
//nolint:ll
263+
for ; timestamp != nil && bytes.Compare(timestamp, endTime[:]) <= 0; timestamp, eventBytes = logCursor.Next() {
261264
// If our current return payload exceeds the max number
262265
// of events, then we'll exit now.
263266
if uint32(len(resp.ForwardingEvents)) >= q.NumMaxEvents {
@@ -271,27 +274,31 @@ func (f *ForwardingLog) Query(q ForwardingEventQuery) (ForwardingLogTimeSlice, e
271274
continue
272275
}
273276

274-
currentTime := time.Unix(
275-
0, int64(byteOrder.Uint64(timestamp)),
276-
)
277-
278277
// At this point, we've skipped enough records to start
279278
// to collate our query. For each record, we'll
280279
// increment the final record offset so the querier can
281280
// utilize pagination to seek further.
282-
readBuf := bytes.NewReader(events)
283-
for readBuf.Len() != 0 {
284-
var event ForwardingEvent
285-
err := decodeForwardingEvent(readBuf, &event)
286-
if err != nil {
287-
return err
288-
}
289-
290-
event.Timestamp = currentTime
291-
resp.ForwardingEvents = append(resp.ForwardingEvents, event)
292-
293-
recordOffset++
281+
readBuf := bytes.NewReader(eventBytes)
282+
if readBuf.Len() == 0 {
283+
continue
294284
}
285+
286+
currentTime := time.Unix(
287+
0, int64(byteOrder.Uint64(timestamp)),
288+
)
289+
290+
var event ForwardingEvent
291+
err := decodeForwardingEvent(readBuf, &event)
292+
if err != nil {
293+
return err
294+
}
295+
296+
event.Timestamp = currentTime
297+
resp.ForwardingEvents = append(
298+
resp.ForwardingEvents, event,
299+
)
300+
301+
recordOffset++
295302
}
296303

297304
return nil

0 commit comments

Comments
 (0)