forked from m3db/m3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalar.go
181 lines (154 loc) · 4.46 KB
/
scalar.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
173
174
175
176
177
178
179
180
181
// 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 block
import (
"fmt"
"time"
"github.com/m3db/m3/src/query/models"
)
// Scalar is a block containing a single value over a certain bound
// This represents constant values; it greatly simplifies downstream operations
// by allowing them to treat this as a regular block, while at the same time
// having an option to optimize by accessing the scalar value directly instead.
type Scalar struct {
val float64
meta Metadata
}
// NewScalar creates a scalar block whose value is given by the function over
// the metadata bounds.
func NewScalar(
val float64,
meta Metadata,
) Block {
// NB: sanity check to ensure scalar values always have clean metadata.
meta.ResultMetadata = NewResultMetadata()
return &Scalar{
val: val,
meta: meta,
}
}
func (c *Scalar) Info() BlockInfo {
return NewBlockInfo(BlockScalar)
}
// Unconsolidated returns the unconsolidated version for the block.
func (b *Scalar) Unconsolidated() (UnconsolidatedBlock, error) {
return nil, fmt.Errorf(
"unconsolidated view not implemented for scalar block, meta: %s", b.meta)
}
func (b *Scalar) Meta() Metadata {
return b.meta
}
func (b *Scalar) StepIter() (StepIter, error) {
bounds := b.meta.Bounds
steps := bounds.Steps()
return &scalarStepIter{
meta: b.meta,
vals: []float64{b.val},
numVals: steps,
idx: -1,
}, nil
}
func (b *Scalar) SeriesIter() (SeriesIter, error) {
bounds := b.meta.Bounds
steps := bounds.Steps()
vals := make([]float64, steps)
t := bounds.Start
for i := range vals {
vals[i] = b.val
t = t.Add(bounds.StepSize)
}
return &scalarSeriesIter{
meta: b.meta,
vals: vals,
idx: -1,
}, nil
}
func (b *Scalar) Close() error { return nil }
func (b *Scalar) Value() float64 {
return b.val
}
type scalarStepIter struct {
numVals, idx int
stepTime time.Time
err error
meta Metadata
vals []float64
}
// build an empty SeriesMetadata.
func buildSeriesMeta(meta Metadata) SeriesMeta {
return SeriesMeta{
Tags: models.NewTags(0, meta.Tags.Opts),
}
}
func (it *scalarStepIter) Close() { /* No-op*/ }
func (it *scalarStepIter) Err() error { return it.err }
func (it *scalarStepIter) StepCount() int { return it.numVals }
func (it *scalarStepIter) SeriesMeta() []SeriesMeta {
return []SeriesMeta{buildSeriesMeta(it.meta)}
}
func (it *scalarStepIter) Next() bool {
if it.err != nil {
return false
}
it.idx++
next := it.idx < it.numVals
if !next {
return false
}
it.stepTime, it.err = it.meta.Bounds.TimeForIndex(it.idx)
if it.err != nil {
return false
}
return next
}
func (it *scalarStepIter) Current() Step {
t := it.stepTime
return &scalarStep{
vals: it.vals,
time: t,
}
}
type scalarStep struct {
vals []float64
time time.Time
}
func (it *scalarStep) Time() time.Time { return it.time }
func (it *scalarStep) Values() []float64 { return it.vals }
type scalarSeriesIter struct {
meta Metadata
vals []float64
idx int
}
func (it *scalarSeriesIter) Close() { /* No-op*/ }
func (it *scalarSeriesIter) Err() error { return nil }
func (it *scalarSeriesIter) SeriesCount() int { return 1 }
func (it *scalarSeriesIter) SeriesMeta() []SeriesMeta {
return []SeriesMeta{buildSeriesMeta(it.meta)}
}
func (it *scalarSeriesIter) Next() bool {
it.idx++
return it.idx == 0
}
func (it *scalarSeriesIter) Current() Series {
return Series{
Meta: buildSeriesMeta(it.meta),
values: it.vals,
}
}