github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/influxdb/models/rows.go (about)

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