forked from grafana/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 7
/
chunk.go
63 lines (54 loc) · 1.87 KB
/
chunk.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
// package chunk encodes timeseries in chunks of data
// see devdocs/chunk-format.md for more information.
package chunk
import (
"fmt"
"github.com/grafana/metrictank/mdata/chunk/tsz"
"github.com/grafana/metrictank/mdata/errors"
)
// Chunk is a chunk of data. not concurrency safe.
// last check that the methods are being called safely by Dieter on 20/11/2018
// checked: String, Push, Finish, Encode and properties Series, NumPoints, First
// for the most part, confirming serialized access is easy by tracking all callers/references.
// The main exception is the ChunkWriteRequest mechanism. any CWR created is processed
// asynchronously, and chunk properties will be read (the series.T0, chunk.Encode(), etc)
// But it can be proven that we only call NewChunkWriteRequest() on chunks that are no longer being modified.
type Chunk struct {
Series tsz.SeriesLong
NumPoints uint32
First bool
}
func New(t0 uint32) *Chunk {
return &Chunk{
Series: *tsz.NewSeriesLong(t0),
}
}
func NewFirst(t0 uint32) *Chunk {
return &Chunk{
Series: *tsz.NewSeriesLong(t0),
First: true,
}
}
func (c *Chunk) String() string {
return fmt.Sprintf("<chunk T0=%d, LastTs=%d, NumPoints=%d, First=%t, Closed=%t>", c.Series.T0, c.Series.T, c.NumPoints, c.First, c.Series.Finished)
}
func (c *Chunk) Push(t uint32, v float64) error {
if t == c.Series.T {
return errors.ErrMetricNewValueForTimestamp
} else if t < c.Series.T {
return errors.ErrMetricTooOld
}
c.Series.Push(t, v)
c.NumPoints++
return nil
}
func (c *Chunk) Finish() {
c.Series.Finish()
}
// Encode encodes the chunk
// note: chunks don't know their own span, the caller/owner manages that,
// so for formats that encode it, it needs to be passed in.
// the returned value contains no references to the chunk. data is copied.
func (c *Chunk) Encode(span uint32) []byte {
return encode(span, FormatGoTszLongWithSpan, c.Series.Bytes())
}