2020-02-23 11:35:47 +00:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SortLabels sorts labels.
|
|
|
|
func SortLabels(labels []prompbmarshal.Label) {
|
2022-10-09 11:51:14 +00:00
|
|
|
if len(labels) < 2 {
|
|
|
|
return
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
ls := labelsSorterPool.Get().(*labelsSorter)
|
|
|
|
*ls = labels
|
|
|
|
if !sort.IsSorted(ls) {
|
|
|
|
sort.Sort(ls)
|
|
|
|
}
|
|
|
|
*ls = nil
|
|
|
|
labelsSorterPool.Put(ls)
|
|
|
|
}
|
|
|
|
|
2022-04-20 12:25:41 +00:00
|
|
|
// SortLabelsStable sorts labels using stable sort.
|
|
|
|
func SortLabelsStable(labels []prompbmarshal.Label) {
|
2022-10-09 11:51:14 +00:00
|
|
|
if len(labels) < 2 {
|
|
|
|
return
|
|
|
|
}
|
2022-04-20 12:25:41 +00:00
|
|
|
ls := labelsSorterPool.Get().(*labelsSorter)
|
|
|
|
*ls = labels
|
|
|
|
if !sort.IsSorted(ls) {
|
|
|
|
sort.Stable(ls)
|
|
|
|
}
|
|
|
|
*ls = nil
|
|
|
|
labelsSorterPool.Put(ls)
|
|
|
|
}
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
var labelsSorterPool = &sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &labelsSorter{}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type labelsSorter []prompbmarshal.Label
|
|
|
|
|
|
|
|
func (ls *labelsSorter) Len() int { return len(*ls) }
|
|
|
|
func (ls *labelsSorter) Swap(i, j int) {
|
|
|
|
a := *ls
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
func (ls *labelsSorter) Less(i, j int) bool {
|
|
|
|
a := *ls
|
|
|
|
return a[i].Name < a[j].Name
|
|
|
|
}
|