2016-03-20 20:55:48 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Row represents a single row returned from the execution of a statement.
|
|
|
|
type Row struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Tags map[string]string `json:"tags,omitempty"`
|
|
|
|
Columns []string `json:"columns,omitempty"`
|
|
|
|
Values [][]interface{} `json:"values,omitempty"`
|
2017-01-20 02:10:17 +01:00
|
|
|
Partial bool `json:"partial,omitempty"`
|
2016-03-20 20:55:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SameSeries returns true if r contains values for the same series as o.
|
|
|
|
func (r *Row) SameSeries(o *Row) bool {
|
|
|
|
return r.tagsHash() == o.tagsHash() && r.Name == o.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// tagsHash returns a hash of tag key/value pairs.
|
|
|
|
func (r *Row) tagsHash() uint64 {
|
2017-01-20 02:10:17 +01:00
|
|
|
h := NewInlineFNV64a()
|
2016-03-20 20:55:48 +01:00
|
|
|
keys := r.tagsKeys()
|
|
|
|
for _, k := range keys {
|
|
|
|
h.Write([]byte(k))
|
|
|
|
h.Write([]byte(r.Tags[k]))
|
|
|
|
}
|
|
|
|
return h.Sum64()
|
|
|
|
}
|
|
|
|
|
|
|
|
// tagKeys returns a sorted list of tag keys.
|
|
|
|
func (r *Row) tagsKeys() []string {
|
|
|
|
a := make([]string, 0, len(r.Tags))
|
|
|
|
for k := range r.Tags {
|
|
|
|
a = append(a, k)
|
|
|
|
}
|
|
|
|
sort.Strings(a)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rows represents a collection of rows. Rows implements sort.Interface.
|
|
|
|
type Rows []*Row
|
|
|
|
|
2017-01-20 02:10:17 +01:00
|
|
|
// Len implements sort.Interface.
|
2016-03-20 20:55:48 +01:00
|
|
|
func (p Rows) Len() int { return len(p) }
|
|
|
|
|
2017-01-20 02:10:17 +01:00
|
|
|
// Less implements sort.Interface.
|
2016-03-20 20:55:48 +01:00
|
|
|
func (p Rows) Less(i, j int) bool {
|
|
|
|
// Sort by name first.
|
|
|
|
if p[i].Name != p[j].Name {
|
|
|
|
return p[i].Name < p[j].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort by tag set hash. Tags don't have a meaningful sort order so we
|
|
|
|
// just compute a hash and sort by that instead. This allows the tests
|
|
|
|
// to receive rows in a predictable order every time.
|
|
|
|
return p[i].tagsHash() < p[j].tagsHash()
|
|
|
|
}
|
|
|
|
|
2017-01-20 02:10:17 +01:00
|
|
|
// Swap implements sort.Interface.
|
2016-03-20 20:55:48 +01:00
|
|
|
func (p Rows) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|