Documentation
¶
Overview ¶
Package timeseries implements encoder and decoder for time-series data point in similar to Facebook Gorilla time-series database. The spec is described in the paper "Gorilla: A Fast, Scalable, In-Memory Time Series Database" http://www.vldb.org/pvldb/vol8/p1816-teller.pdf
This implementation is based on a third party Java implementation https://github.com/burmanm/gorilla-tsc/ However this Go package removes the enhancements in this Java version.
- The precision of timestamps is one second (not a milisecond).
- The data point value type is flaot64 only.
- The first timestamp delta is sized at 14 bits. This size span a bit more than 4 hours (16,384 seconds).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal encodes a block timestamp and data points to bytes.
Example ¶
package main
import (
"encoding/hex"
"fmt"
"time"
"github.com/hnakamur/timeseries"
)
func main() {
t0 := uint32(time.Date(2015, 3, 24, 2, 0, 0, 0, time.UTC).Unix())
points := []timeseries.Point{
{
Timestamp: uint32(time.Date(2015, 3, 24, 2, 1, 2, 0, time.UTC).Unix()),
Value: 12.0,
},
{
Timestamp: uint32(time.Date(2015, 3, 24, 2, 2, 2, 0, time.UTC).Unix()),
Value: 12.0,
},
{
Timestamp: uint32(time.Date(2015, 3, 24, 2, 3, 2, 0, time.UTC).Unix()),
Value: 24.0,
},
}
buf, err := timeseries.Marshal(t0, points)
if err != nil {
fmt.Printf("failed to marshal points: err=%+v\n", err)
return
}
fmt.Println(hex.EncodeToString(buf))
}
Output: 5510c52000f900a0000000000002fc6b07ffffffffe0
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder decodes bytes data to a block timestamp and data points.
Example ¶
package main
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"time"
"github.com/hnakamur/timeseries"
)
func main() {
input, err := hex.DecodeString("5510c52000f900a0000000000002fc6b07ffffffffe0")
if err != nil {
fmt.Printf("failed to decode hex string: err=%+v\n", err)
return
}
b := bytes.NewBuffer(input)
dec := timeseries.NewDecoder(b)
t0, err := dec.DecodeHeader()
if err != nil {
fmt.Printf("failed to decode time series header: err=%+v\n", err)
return
}
var points []timeseries.Point
for {
p, err := dec.DecodePoint()
if err == io.EOF {
break
} else if err != nil {
fmt.Printf("failed to decode time series point: err=%+v\n", err)
return
}
points = append(points, p)
}
fmt.Printf("block timestamp=%v\n", time.Unix(int64(t0), 0).UTC())
for _, p := range points {
fmt.Printf("timestamp=%v, value=%f\n", time.Unix(int64(p.Timestamp), 0).UTC(), p.Value)
}
}
Output: block timestamp=2015-03-24 02:00:00 +0000 UTC timestamp=2015-03-24 02:01:02 +0000 UTC, value=12.000000 timestamp=2015-03-24 02:02:02 +0000 UTC, value=12.000000 timestamp=2015-03-24 02:03:02 +0000 UTC, value=24.000000
func (*Decoder) DecodeHeader ¶
DecodeHeader decodes header to the block timestamp.
func (*Decoder) DecodePoint ¶
DecodePoint decodes a data point. It returns io.EOF when it see the finish marker or it got EOF from the underlying reader.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder encodes time series data in similar way to Facebook Gorilla in-memory time series database.
Example ¶
package main
import (
"bytes"
"encoding/hex"
"fmt"
"time"
"github.com/hnakamur/timeseries"
)
func main() {
t0 := uint32(time.Date(2015, 3, 24, 2, 0, 0, 0, time.UTC).Unix())
points := []timeseries.Point{
{
Timestamp: uint32(time.Date(2015, 3, 24, 2, 1, 2, 0, time.UTC).Unix()),
Value: 12.0,
},
{
Timestamp: uint32(time.Date(2015, 3, 24, 2, 2, 2, 0, time.UTC).Unix()),
Value: 12.0,
},
{
Timestamp: uint32(time.Date(2015, 3, 24, 2, 3, 2, 0, time.UTC).Unix()),
Value: 24.0,
},
}
var b bytes.Buffer
enc := timeseries.NewEncoder(&b)
err := enc.EncodeHeader(t0)
if err != nil {
fmt.Printf("failed to encode time series header: err=%+v\n", err)
return
}
for _, p := range points {
err := enc.EncodePoint(p)
if err != nil {
fmt.Printf("failed to encode time series point: err=%+v\n", err)
return
}
}
err = enc.Finish()
if err != nil {
fmt.Printf("failed to encode time series finish marker: err=%+v\n", err)
return
}
fmt.Println(hex.EncodeToString(b.Bytes()))
}
Output: 5510c52000f900a0000000000002fc6b07ffffffffe0
func (*Encoder) EncodeHeader ¶
EncodeHeader encodes the block timestamp to the header bits.
func (*Encoder) EncodePoint ¶
EncodePoint encodes a data point.
type Point ¶
type Point struct {
// Timestamp represents the seconds since 1970-01-01 00:00:00 +0000 UTC.
// The max value is 2106-02-07 06:28:15 +0000 UTC.
// You can verify the max value at https://play.golang.org/p/XgHveabiUd
Timestamp uint32
// Value represents the data point value.
Value float64
}
Point is a time-series data point.
func Unmarshal ¶
Unmarshal decodes bytes to a block timestamp and data points.
Example ¶
package main
import (
"encoding/hex"
"fmt"
"time"
"github.com/hnakamur/timeseries"
)
func main() {
input, err := hex.DecodeString("5510c52000f900a0000000000002fc6b07ffffffffe0")
if err != nil {
fmt.Printf("failed to decode hex string: err=%+v\n", err)
return
}
t0, points, err := timeseries.Unmarshal(input)
if err != nil {
fmt.Printf("failed to unmarshal time series: err=%+v\n", err)
return
}
fmt.Printf("block timestamp=%v\n", time.Unix(int64(t0), 0).UTC())
for _, p := range points {
fmt.Printf("timestamp=%v, value=%f\n", time.Unix(int64(p.Timestamp), 0).UTC(), p.Value)
}
}
Output: block timestamp=2015-03-24 02:00:00 +0000 UTC timestamp=2015-03-24 02:01:02 +0000 UTC, value=12.000000 timestamp=2015-03-24 02:02:02 +0000 UTC, value=12.000000 timestamp=2015-03-24 02:03:02 +0000 UTC, value=24.000000
