From 5efe1d178e9f0f25a9dbabfb8be724cbc569e75b Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Sat, 24 Dec 2016 13:49:35 +0100 Subject: [PATCH] labels: add Compare and String methods --- labels/labels.go | 40 +++++++++++++++++++++++++++++++++++++ querier.go | 22 +------------------- querier_test.go | 52 ------------------------------------------------ writer.go | 2 +- 4 files changed, 42 insertions(+), 74 deletions(-) diff --git a/labels/labels.go b/labels/labels.go index 001f11247..0b79bc2e9 100644 --- a/labels/labels.go +++ b/labels/labels.go @@ -1,7 +1,10 @@ package labels import ( + "bytes" "sort" + "strconv" + "strings" "github.com/cespare/xxhash" ) @@ -21,6 +24,23 @@ func (ls Labels) Len() int { return len(ls) } func (ls Labels) Swap(i, j int) { ls[i], ls[j] = ls[j], ls[i] } func (ls Labels) Less(i, j int) bool { return ls[i].Name < ls[j].Name } +func (ls Labels) String() string { + var b bytes.Buffer + + b.WriteByte('{') + for i, l := range ls { + if i > 0 { + b.WriteByte(',') + } + b.WriteString(l.Name) + b.WriteByte('=') + b.WriteString(strconv.Quote(l.Value)) + } + b.WriteByte('}') + + return b.String() +} + // Hash returns a hash value for the label set. func (ls Labels) Hash() uint64 { b := make([]byte, 0, 1024) @@ -101,3 +121,23 @@ func FromStrings(ss ...string) Labels { sort.Sort(res) return res } + +// Compare compares the two label sets. +// The result will be 0 if a==b, <0 if a < b, and >0 if a > b. +func Compare(a, b Labels) int { + l := len(a) + if len(b) < l { + l = len(b) + } + + for i := 0; i < l; i++ { + if d := strings.Compare(a[i].Name, b[i].Name); d != 0 { + return d + } + if d := strings.Compare(a[i].Value, b[i].Value); d != 0 { + return d + } + } + // If all labels so far were in common, the set with fewer labels comes first. + return len(a) - len(b) +} diff --git a/querier.go b/querier.go index 6d9f022a4..4c6a877de 100644 --- a/querier.go +++ b/querier.go @@ -340,26 +340,6 @@ func newShardSeriesSet(a, b SeriesSet) *shardSeriesSet { return s } -// compareLabels compares the two label sets. -// The result will be 0 if a==b, <0 if a < b, and >0 if a > b. -func compareLabels(a, b labels.Labels) int { - l := len(a) - if len(b) < l { - l = len(b) - } - - for i := 0; i < l; i++ { - if d := strings.Compare(a[i].Name, b[i].Name); d != 0 { - return d - } - if d := strings.Compare(a[i].Value, b[i].Value); d != 0 { - return d - } - } - // If all labels so far were in common, the set with fewer labels comes first. - return len(a) - len(b) -} - func (s *shardSeriesSet) Series() Series { return s.cur } @@ -378,7 +358,7 @@ func (s *shardSeriesSet) compare() int { if s.bs == nil { return -1 } - return compareLabels(s.as.Labels(), s.bs.Labels()) + return labels.Compare(s.as.Labels(), s.bs.Labels()) } func (s *shardSeriesSet) advanceA() { diff --git a/querier_test.go b/querier_test.go index 86d3e310c..831fc2436 100644 --- a/querier_test.go +++ b/querier_test.go @@ -175,58 +175,6 @@ func expandSeriesIterator(it SeriesIterator) (r []sample, err error) { return r, it.Err() } -func TestCompareLabels(t *testing.T) { - cases := []struct { - a, b []labels.Label - res int - }{ - { - a: []labels.Label{}, - b: []labels.Label{}, - res: 0, - }, - { - a: []labels.Label{{"a", ""}}, - b: []labels.Label{{"a", ""}, {"b", ""}}, - res: -1, - }, - { - a: []labels.Label{{"a", ""}}, - b: []labels.Label{{"a", ""}}, - res: 0, - }, - { - a: []labels.Label{{"aa", ""}, {"aa", ""}}, - b: []labels.Label{{"aa", ""}, {"ab", ""}}, - res: -1, - }, - { - a: []labels.Label{{"aa", ""}, {"abb", ""}}, - b: []labels.Label{{"aa", ""}, {"ab", ""}}, - res: 1, - }, - { - a: []labels.Label{ - {"__name__", "go_gc_duration_seconds"}, - {"job", "prometheus"}, - {"quantile", "0.75"}, - }, - b: []labels.Label{ - {"__name__", "go_gc_duration_seconds"}, - {"job", "prometheus"}, - {"quantile", "1"}, - }, - res: -1, - }, - } - for _, c := range cases { - // Use constructor to ensure sortedness. - a, b := labels.New(c.a...), labels.New(c.b...) - - require.Equal(t, c.res, compareLabels(a, b)) - } -} - func TestSampleRing(t *testing.T) { cases := []struct { input []int64 diff --git a/writer.go b/writer.go index aacfc0d78..ff2154533 100644 --- a/writer.go +++ b/writer.go @@ -325,7 +325,7 @@ func (w *indexWriter) writeSeries() error { series = append(series, s) } slice.Sort(series, func(i, j int) bool { - return compareLabels(series[i].labels, series[j].labels) < 0 + return labels.Compare(series[i].labels, series[j].labels) < 0 }) // Current end of file plus 5 bytes for section header. -- GitLab