Files
seaweedfs/weed/mq/offset_range.go
T
Chris Lu f35caf8be3 mq: read a recorded offset range that ends at offset 0
The broker and the Kafka gateway each scanned a partition's files for
the high water mark and counted a maximum only when it was above 0, so
a partition holding one message still read as empty. Both now share
mq.OffsetRange, which tracks whether a range was found apart from its
value.
2026-11-30 13:45:28 -08:00

35 lines
940 B
Go

package mq
import "encoding/binary"
// OffsetRange accumulates the offset ranges recorded on a partition's files.
type OffsetRange struct {
earliest, latest int64
hasEarliest, hasLatest bool
}
func (r *OffsetRange) Add(extended map[string][]byte) {
if b, ok := extended[ExtendedAttrOffsetMin]; ok && len(b) == 8 {
if v := int64(binary.BigEndian.Uint64(b)); !r.hasEarliest || v < r.earliest {
r.earliest, r.hasEarliest = v, true
}
}
if b, ok := extended[ExtendedAttrOffsetMax]; ok && len(b) == 8 {
if v := int64(binary.BigEndian.Uint64(b)); !r.hasLatest || v > r.latest {
r.latest, r.hasLatest = v, true
}
}
}
// Bounds is the earliest recorded offset and the offset after the latest,
// both 0 when no file records a range.
func (r *OffsetRange) Bounds() (earliestOffset, highWaterMark int64) {
if r.hasEarliest {
earliestOffset = r.earliest
}
if r.hasLatest {
highWaterMark = r.latest + 1
}
return
}