aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/influxdata/influxdb/models/rows.go
blob: c087a4882d0d2d57da01a8d142db7091d6eebda3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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"`
    Partial bool              `json:"partial,omitempty"`
}

// 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 {
    h := NewInlineFNV64a()
    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

// Len implements sort.Interface.
func (p Rows) Len() int { return len(p) }

// Less implements sort.Interface.
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()
}

// Swap implements sort.Interface.
func (p Rows) Swap(i, j int) { p[i], p[j] = p[j], p[i] }