rows.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package models
  2. import (
  3. "sort"
  4. )
  5. // Row represents a single row returned from the execution of a statement.
  6. type Row struct {
  7. Name string `json:"name,omitempty"`
  8. Tags map[string]string `json:"tags,omitempty"`
  9. Columns []string `json:"columns,omitempty"`
  10. Values [][]interface{} `json:"values,omitempty"`
  11. Partial bool `json:"partial,omitempty"`
  12. }
  13. // SameSeries returns true if r contains values for the same series as o.
  14. func (r *Row) SameSeries(o *Row) bool {
  15. return r.tagsHash() == o.tagsHash() && r.Name == o.Name
  16. }
  17. // tagsHash returns a hash of tag key/value pairs.
  18. func (r *Row) tagsHash() uint64 {
  19. h := NewInlineFNV64a()
  20. keys := r.tagsKeys()
  21. for _, k := range keys {
  22. h.Write([]byte(k))
  23. h.Write([]byte(r.Tags[k]))
  24. }
  25. return h.Sum64()
  26. }
  27. // tagKeys returns a sorted list of tag keys.
  28. func (r *Row) tagsKeys() []string {
  29. a := make([]string, 0, len(r.Tags))
  30. for k := range r.Tags {
  31. a = append(a, k)
  32. }
  33. sort.Strings(a)
  34. return a
  35. }
  36. // Rows represents a collection of rows. Rows implements sort.Interface.
  37. type Rows []*Row
  38. // Len implements sort.Interface.
  39. func (p Rows) Len() int { return len(p) }
  40. // Less implements sort.Interface.
  41. func (p Rows) Less(i, j int) bool {
  42. // Sort by name first.
  43. if p[i].Name != p[j].Name {
  44. return p[i].Name < p[j].Name
  45. }
  46. // Sort by tag set hash. Tags don't have a meaningful sort order so we
  47. // just compute a hash and sort by that instead. This allows the tests
  48. // to receive rows in a predictable order every time.
  49. return p[i].tagsHash() < p[j].tagsHash()
  50. }
  51. // Swap implements sort.Interface.
  52. func (p Rows) Swap(i, j int) { p[i], p[j] = p[j], p[i] }