-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathcommon.go
172 lines (148 loc) · 5.43 KB
/
common.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package binary
import (
"bytes"
"errors"
"github.com/m3db/m3/src/query/block"
"github.com/m3db/m3/src/query/models"
)
// VectorMatchCardinality describes the cardinality relationship
// of two Vectors in a binary operation.
type VectorMatchCardinality int
const (
// CardOneToOne is used for one-one relationship
CardOneToOne VectorMatchCardinality = iota
// CardManyToOne is used for many-one relationship
CardManyToOne
// CardOneToMany is used for one-many relationship
CardOneToMany
// CardManyToMany is used for many-many relationship
CardManyToMany
)
// VectorMatching describes how elements from two Vectors in a binary
// operation are supposed to be matched.
type VectorMatching struct {
// The cardinality of the two Vectors.
Card VectorMatchCardinality
// MatchingLabels contains the labels which define equality of a pair of
// elements from the Vectors.
MatchingLabels [][]byte
// On includes the given label names from matching,
// rather than excluding them.
On bool
// Include contains additional labels that should be included in
// the result from the side with the lower cardinality.
Include []string
}
// HashFunc returns a function that calculates the signature for a metric
// ignoring the provided labels. If on, then the given labels are only used instead.
func HashFunc(on bool, names ...[]byte) func(models.Tags) uint64 {
if on {
return func(tags models.Tags) uint64 {
return tags.TagsWithKeys(names).HashedID()
}
}
return func(tags models.Tags) uint64 {
return tags.TagsWithoutKeys(names).HashedID()
}
}
const initIndexSliceLength = 10
var (
errMismatchedBounds = errors.New("block bounds are mismatched")
errMismatchedStepCounts = errors.New("block step counts are mismatched")
errLeftScalar = errors.New("expected left scalar but node type incorrect")
errRightScalar = errors.New("expected right scalar but node type incorrect")
errNoModifierForComparison = errors.New("comparisons between scalars must use BOOL modifier")
errNoMatching = errors.New("vector matching parameters must be provided for binary operations between series")
)
func tagMap(t models.Tags) map[string]models.Tag {
m := make(map[string]models.Tag, t.Len())
for _, tag := range t.Tags {
m[string(tag.Name)] = tag
}
return m
}
func combineMetaAndSeriesMeta(
meta, otherMeta block.Metadata,
seriesMeta, otherSeriesMeta []block.SeriesMeta,
) (block.Metadata, []block.SeriesMeta, []block.SeriesMeta, error) {
if !meta.Bounds.Equals(otherMeta.Bounds) {
return block.Metadata{},
[]block.SeriesMeta{},
[]block.SeriesMeta{},
errMismatchedBounds
}
// NB (arnikola): mutating tags in `meta` to avoid allocations
leftTags := meta.Tags
otherTags := tagMap(otherMeta.Tags)
metaTagsToAdd := models.NewTags(leftTags.Len(), leftTags.Opts)
otherMetaTagsToAdd := models.NewTags(len(otherTags), leftTags.Opts)
tags := models.NewTags(leftTags.Len(), leftTags.Opts)
for _, t := range leftTags.Tags {
name := string(t.Name)
if otherTag, ok := otherTags[name]; ok {
if bytes.Equal(t.Value, otherTag.Value) {
tags = tags.AddTag(t)
} else {
// If both metas have the same common tag with different
// values explicitly add it to each seriesMeta.
metaTagsToAdd = metaTagsToAdd.AddTag(t)
otherMetaTagsToAdd = otherMetaTagsToAdd.AddTag(otherTag)
}
// NB(arnikola): delete common tag from otherTags as it
// has already been handled
delete(otherTags, name)
} else {
// Tag does not exist on otherMeta explicitly add it to each seriesMeta
metaTagsToAdd = metaTagsToAdd.AddTag(t)
}
}
// Iterate over otherMeta common tags and explicitly add
// remaining tags to otherSeriesMeta
for _, otherTag := range otherTags {
otherMetaTagsToAdd = otherMetaTagsToAdd.AddTag(otherTag)
}
// Set common tags
meta.Tags = tags
for i, m := range seriesMeta {
seriesMeta[i].Tags = m.Tags.Add(metaTagsToAdd)
}
for i, m := range otherSeriesMeta {
otherSeriesMeta[i].Tags = m.Tags.Add(otherMetaTagsToAdd)
}
return meta,
seriesMeta,
otherSeriesMeta,
nil
}
func appendValuesAtIndices(idxArray []int, iter block.StepIter, builder block.Builder) error {
for index := 0; iter.Next(); index++ {
step, err := iter.Current()
if err != nil {
return err
}
values := step.Values()
for _, idx := range idxArray {
builder.AppendValue(index, values[idx])
}
}
return nil
}