Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions model/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func (ls Labels) Less(i, j int) bool { return ls[i].Name < ls[j].Name }
// Encoding may change over time or between runs of Prometheus.
func (ls Labels) Bytes(buf []byte) []byte {
b := bytes.NewBuffer(buf[:0])
b.WriteByte(labelSep)
b.WriteByte(LabelSep)
for i, l := range ls {
if i > 0 {
b.WriteByte(sep)
b.WriteByte(Sep)
}
b.WriteString(l.Name)
b.WriteByte(sep)
b.WriteByte(Sep)
b.WriteString(l.Value)
}
return b.Bytes()
Expand Down Expand Up @@ -79,17 +79,17 @@ func (ls Labels) Hash() uint64 {
_, _ = h.Write(b)
for _, v := range ls[i:] {
_, _ = h.WriteString(v.Name)
_, _ = h.Write(seps)
_, _ = h.Write(Seps)
_, _ = h.WriteString(v.Value)
_, _ = h.Write(seps)
_, _ = h.Write(Seps)
}
return h.Sum64()
}

b = append(b, v.Name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, v.Value...)
b = append(b, sep)
b = append(b, Sep)
}
return xxhash.Sum64(b)
}
Expand All @@ -107,9 +107,9 @@ func (ls Labels) HashForLabels(b []byte, names ...string) (uint64, []byte) {
i++
default:
b = append(b, ls[i].Name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, ls[i].Value...)
b = append(b, sep)
b = append(b, Sep)
i++
j++
}
Expand All @@ -131,9 +131,9 @@ func (ls Labels) HashWithoutLabels(b []byte, names ...string) (uint64, []byte) {
continue
}
b = append(b, ls[i].Name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, ls[i].Value...)
b = append(b, sep)
b = append(b, Sep)
}
return xxhash.Sum64(b), b
}
Expand All @@ -142,7 +142,7 @@ func (ls Labels) HashWithoutLabels(b []byte, names ...string) (uint64, []byte) {
// 'names' have to be sorted in ascending order.
func (ls Labels) BytesWithLabels(buf []byte, names ...string) []byte {
b := bytes.NewBuffer(buf[:0])
b.WriteByte(labelSep)
b.WriteByte(LabelSep)
i, j := 0, 0
for i < len(ls) && j < len(names) {
switch {
Expand All @@ -152,10 +152,10 @@ func (ls Labels) BytesWithLabels(buf []byte, names ...string) []byte {
i++
default:
if b.Len() > 1 {
b.WriteByte(sep)
b.WriteByte(Sep)
}
b.WriteString(ls[i].Name)
b.WriteByte(sep)
b.WriteByte(Sep)
b.WriteString(ls[i].Value)
i++
j++
Expand All @@ -168,7 +168,7 @@ func (ls Labels) BytesWithLabels(buf []byte, names ...string) []byte {
// 'names' have to be sorted in ascending order.
func (ls Labels) BytesWithoutLabels(buf []byte, names ...string) []byte {
b := bytes.NewBuffer(buf[:0])
b.WriteByte(labelSep)
b.WriteByte(LabelSep)
j := 0
for i := range ls {
for j < len(names) && names[j] < ls[i].Name {
Expand All @@ -178,10 +178,10 @@ func (ls Labels) BytesWithoutLabels(buf []byte, names ...string) []byte {
continue
}
if b.Len() > 1 {
b.WriteByte(sep)
b.WriteByte(Sep)
}
b.WriteString(ls[i].Name)
b.WriteByte(sep)
b.WriteByte(Sep)
b.WriteString(ls[i].Value)
}
return b.Bytes()
Expand Down Expand Up @@ -279,6 +279,10 @@ func New(ls ...Label) Labels {
return set
}

func NewFromSorted(ls []Label) Labels {
return ls
}

// FromStrings creates new labels from pairs of strings.
func FromStrings(ss ...string) Labels {
if len(ss)%2 != 0 {
Expand Down Expand Up @@ -423,7 +427,7 @@ func (b *Builder) Labels() Labels {
}
res := make(Labels, 0, expectedSize)
for _, l := range b.base {
if slices.Contains(b.del, l.Name) || contains(b.add, l.Name) {
if slices.Contains(b.del, l.Name) || Contains(b.add, l.Name) {
continue
}
res = append(res, l)
Expand Down
10 changes: 5 additions & 5 deletions model/labels/labels_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ const (
AlertName = "alertname"
BucketLabel = "le"

labelSep = '\xfe' // Used at beginning of `Bytes` return.
sep = '\xff' // Used between labels in `Bytes` and `Hash`.
LabelSep = '\xfe' // Used at beginning of `Bytes` return.
Sep = '\xff' // Used between labels in `Bytes` and `Hash`.
)

var seps = []byte{sep} // Used with Hash, which has no WriteByte method.
var Seps = []byte{Sep} // Used with Hash, which has no WriteByte method.

// Label is a key/value a pair of strings.
type Label struct {
Expand Down Expand Up @@ -215,7 +215,7 @@ func (b *Builder) Range(f func(l Label)) {
// Take a copy of add and del, so they are unaffected by calls to Set() or Del().
origAdd, origDel := append(addStack[:0], b.add...), append(delStack[:0], b.del...)
b.base.Range(func(l Label) {
if !slices.Contains(origDel, l.Name) && !contains(origAdd, l.Name) {
if !slices.Contains(origDel, l.Name) && !Contains(origAdd, l.Name) {
f(l)
}
})
Expand All @@ -224,7 +224,7 @@ func (b *Builder) Range(f func(l Label)) {
}
}

func contains(s []Label, n string) bool {
func Contains(s []Label, n string) bool {
for _, a := range s {
if a.Name == n {
return true
Expand Down
37 changes: 23 additions & 14 deletions model/labels/labels_dedupelabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ func (ls Labels) Bytes(buf []byte) []byte {
b := bytes.NewBuffer(buf[:0])
for i := 0; i < len(ls.data); {
if i > 0 {
b.WriteByte(sep)
b.WriteByte(Sep)
}
var name, value string
name, i = decodeString(ls.syms, ls.data, i)
value, i = decodeString(ls.syms, ls.data, i)
b.WriteString(name)
b.WriteByte(sep)
b.WriteByte(Sep)
b.WriteString(value)
}
return b.Bytes()
Expand Down Expand Up @@ -193,17 +193,17 @@ func (ls Labels) Hash() uint64 {
name, pos = decodeString(ls.syms, ls.data, pos)
value, pos = decodeString(ls.syms, ls.data, pos)
_, _ = h.WriteString(name)
_, _ = h.Write(seps)
_, _ = h.Write(Seps)
_, _ = h.WriteString(value)
_, _ = h.Write(seps)
_, _ = h.Write(Seps)
}
return h.Sum64()
}

b = append(b, name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, value...)
b = append(b, sep)
b = append(b, Sep)
pos = newPos
}
return xxhash.Sum64(b)
Expand All @@ -226,9 +226,9 @@ func (ls Labels) HashForLabels(b []byte, names ...string) (uint64, []byte) {
}
if name == names[j] {
b = append(b, name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, value...)
b = append(b, sep)
b = append(b, Sep)
}
}

Expand All @@ -252,9 +252,9 @@ func (ls Labels) HashWithoutLabels(b []byte, names ...string) (uint64, []byte) {
continue
}
b = append(b, name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, value...)
b = append(b, sep)
b = append(b, Sep)
}
return xxhash.Sum64(b), b
}
Expand All @@ -275,10 +275,10 @@ func (ls Labels) BytesWithLabels(buf []byte, names ...string) []byte {
}
if lName == names[j] {
if b.Len() > 1 {
b.WriteByte(sep)
b.WriteByte(Sep)
}
b.WriteString(lName)
b.WriteByte(sep)
b.WriteByte(Sep)
b.WriteString(lValue)
}
pos = newPos
Expand All @@ -299,10 +299,10 @@ func (ls Labels) BytesWithoutLabels(buf []byte, names ...string) []byte {
}
if j == len(names) || lName != names[j] {
if b.Len() > 1 {
b.WriteByte(sep)
b.WriteByte(Sep)
}
b.WriteString(lName)
b.WriteByte(sep)
b.WriteByte(Sep)
b.WriteString(lValue)
}
pos = newPos
Expand Down Expand Up @@ -464,6 +464,15 @@ func New(ls ...Label) Labels {
return Labels{syms: syms.nameTable, data: yoloString(buf)}
}

func NewFromSorted(ls []Label) Labels {
syms := NewSymbolTable()
var stackSpace [16]int
size, nums := mapLabelsToNumbers(syms, ls, stackSpace[:])
buf := make([]byte, size)
marshalNumbersToSizedBuffer(nums, buf)
return Labels{syms: syms.nameTable, data: yoloString(buf)}
}

// FromStrings creates new labels from pairs of strings.
func FromStrings(ss ...string) Labels {
if len(ss)%2 != 0 {
Expand Down
15 changes: 11 additions & 4 deletions model/labels/labels_stringlabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func (ls Labels) HashForLabels(b []byte, names ...string) (uint64, []byte) {
}
if name == names[j] {
b = append(b, name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, value...)
b = append(b, sep)
b = append(b, Sep)
}
}

Expand All @@ -131,9 +131,9 @@ func (ls Labels) HashWithoutLabels(b []byte, names ...string) (uint64, []byte) {
continue
}
b = append(b, name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, value...)
b = append(b, sep)
b = append(b, Sep)
}
return xxhash.Sum64(b), b
}
Expand Down Expand Up @@ -314,6 +314,13 @@ func New(ls ...Label) Labels {
return Labels{data: yoloString(buf)}
}

func NewFromSorted(ls []Label) Labels {
size := labelsSize(ls)
buf := make([]byte, size)
marshalLabelsToSizedBuffer(ls, buf)
return Labels{data: yoloString(buf)}
}

// FromStrings creates new labels from pairs of strings.
func FromStrings(ss ...string) Labels {
if len(ss)%2 != 0 {
Expand Down
8 changes: 4 additions & 4 deletions model/labels/sharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ func StableHash(ls Labels) uint64 {
_, _ = h.Write(b)
for _, v := range ls[i:] {
_, _ = h.WriteString(v.Name)
_, _ = h.Write(seps)
_, _ = h.Write(Seps)
_, _ = h.WriteString(v.Value)
_, _ = h.Write(seps)
_, _ = h.Write(Seps)
}
return h.Sum64()
}

b = append(b, v.Name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, v.Value...)
b = append(b, sep)
b = append(b, Sep)
}
return xxhash.Sum64(b)
}
8 changes: 4 additions & 4 deletions model/labels/sharding_dedupelabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ func StableHash(ls Labels) uint64 {
name, pos = decodeString(ls.syms, ls.data, pos)
value, pos = decodeString(ls.syms, ls.data, pos)
_, _ = h.WriteString(name)
_, _ = h.Write(seps)
_, _ = h.Write(Seps)
_, _ = h.WriteString(value)
_, _ = h.Write(seps)
_, _ = h.Write(Seps)
}
return h.Sum64()
}

b = append(b, name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, value...)
b = append(b, sep)
b = append(b, Sep)
pos = newPos
}
return xxhash.Sum64(b)
Expand Down
8 changes: 4 additions & 4 deletions model/labels/sharding_stringlabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ func StableHash(ls Labels) uint64 {
}
if h != nil {
_, _ = h.WriteString(v.Name)
_, _ = h.Write(seps)
_, _ = h.Write(Seps)
_, _ = h.WriteString(v.Value)
_, _ = h.Write(seps)
_, _ = h.Write(Seps)
continue
}

b = append(b, v.Name...)
b = append(b, sep)
b = append(b, Sep)
b = append(b, v.Value...)
b = append(b, sep)
b = append(b, Sep)
}
if h != nil {
return h.Sum64()
Expand Down
Loading