chore(gen): Add ingen generator data structures to platform for reuse

pull/10616/head
Stuart Carnie 2018-12-10 13:53:16 -07:00
parent 6670ef9892
commit 8f8311a3ae
No known key found for this signature in database
GPG Key ID: 848D9C9718D78B4F
12 changed files with 647 additions and 9 deletions

View File

@ -0,0 +1,97 @@
// Generated by tmpl
// https://github.com/benbjohnson/tmpl
//
// DO NOT EDIT!
// Source: arrays.gen.go.tmpl
package gen
import (
"github.com/influxdata/platform/tsdb/cursors"
"github.com/influxdata/platform/tsdb/tsm1"
)
type FloatArray struct {
cursors.FloatArray
}
func NewFloatArrayLen(sz int) *FloatArray {
return &FloatArray{
FloatArray: cursors.FloatArray{
Timestamps: make([]int64, sz),
Values: make([]float64, sz),
},
}
}
func (a *FloatArray) Encode(b []byte) ([]byte, error) {
return tsm1.EncodeFloatArrayBlock(&a.FloatArray, b)
}
type IntegerArray struct {
cursors.IntegerArray
}
func NewIntegerArrayLen(sz int) *IntegerArray {
return &IntegerArray{
IntegerArray: cursors.IntegerArray{
Timestamps: make([]int64, sz),
Values: make([]int64, sz),
},
}
}
func (a *IntegerArray) Encode(b []byte) ([]byte, error) {
return tsm1.EncodeIntegerArrayBlock(&a.IntegerArray, b)
}
type UnsignedArray struct {
cursors.UnsignedArray
}
func NewUnsignedArrayLen(sz int) *UnsignedArray {
return &UnsignedArray{
UnsignedArray: cursors.UnsignedArray{
Timestamps: make([]int64, sz),
Values: make([]uint64, sz),
},
}
}
func (a *UnsignedArray) Encode(b []byte) ([]byte, error) {
return tsm1.EncodeUnsignedArrayBlock(&a.UnsignedArray, b)
}
type StringArray struct {
cursors.StringArray
}
func NewStringArrayLen(sz int) *StringArray {
return &StringArray{
StringArray: cursors.StringArray{
Timestamps: make([]int64, sz),
Values: make([]string, sz),
},
}
}
func (a *StringArray) Encode(b []byte) ([]byte, error) {
return tsm1.EncodeStringArrayBlock(&a.StringArray, b)
}
type BooleanArray struct {
cursors.BooleanArray
}
func NewBooleanArrayLen(sz int) *BooleanArray {
return &BooleanArray{
BooleanArray: cursors.BooleanArray{
Timestamps: make([]int64, sz),
Values: make([]bool, sz),
},
}
}
func (a *BooleanArray) Encode(b []byte) ([]byte, error) {
return tsm1.EncodeBooleanArrayBlock(&a.BooleanArray, b)
}

View File

@ -0,0 +1,26 @@
package gen
import (
"github.com/influxdata/platform/tsdb/tsm1"
"github.com/influxdata/platform/tsdb/cursors"
)
{{range .}}
{{ $typename := print .Name "Array" }}
type {{$typename}} struct {
cursors.{{$typename}}
}
func New{{$typename}}Len(sz int) *{{$typename}} {
return &{{$typename}}{
{{$typename}}: cursors.{{$typename}}{
Timestamps: make([]int64, sz),
Values: make([]{{.Type}}, sz),
},
}
}
func (a *{{$typename}}) Encode(b []byte) ([]byte, error) {
return tsm1.Encode{{$typename}}Block(&a.{{$typename}}, b)
}
{{end}}

3
pkg/data/gen/gen.go Normal file
View File

@ -0,0 +1,3 @@
package gen
//go:generate env GO111MODULE=on go run github.com/benbjohnson/tmpl -data=@types.tmpldata arrays.gen.go.tmpl values_constant.gen.go.tmpl

View File

@ -8,6 +8,10 @@ import (
type Sequence interface {
Next() bool
Value() string
}
type CountableSequence interface {
Sequence
Count() int
}
@ -49,11 +53,11 @@ func (s *CounterByteSequence) update() {
s.val = fmt.Sprintf(s.format, fmt.Sprintf(s.nfmt, s.v))
}
func (s *CounterByteSequence) Count() int { return s.end - s.s }
func (s *CounterByteSequence) Value() string { return s.val }
func (s *CounterByteSequence) Count() int { return s.end - s.s }
type ConstantStringSequence string
func (ConstantStringSequence) Next() bool { return true }
func (s ConstantStringSequence) Next() bool { return true }
func (s ConstantStringSequence) Value() string { return string(s) }
func (ConstantStringSequence) Count() int { return 1 }
func (s ConstantStringSequence) Count() int { return 1 }

View File

@ -0,0 +1,37 @@
package gen
import (
"github.com/influxdata/platform/models"
)
type SeriesGenerator interface {
// Next advances the series generator to the next series key.
Next() bool
// Name returns the name of the measurement.
// The returned value may be modified by a subsequent call to Next.
Name() []byte
// Tags returns the tag set.
// The returned value may be modified by a subsequent call to Next.
Tags() models.Tags
// Field returns the name of the field.
// The returned value may be modified by a subsequent call to Next.
Field() []byte
// ValuesGenerator returns a values sequence for the current series.
ValuesGenerator() ValuesSequence
}
type ValuesSequence interface {
Reset()
Next() bool
Values() Values
}
type Values interface {
MinTime() int64
MaxTime() int64
Encode([]byte) ([]byte, error)
}

View File

@ -16,12 +16,12 @@ type TagsSequence interface {
type TagsValuesSequence struct {
tags models.Tags
vals []Sequence
vals []CountableSequence
n int
max int
}
func NewTagsValuesSequenceKeysValues(keys []string, vals []Sequence) *TagsValuesSequence {
func NewTagsValuesSequenceKeysValues(keys []string, vals []CountableSequence) *TagsValuesSequence {
tm := make(map[string]string, len(keys))
for _, k := range keys {
tm[k] = ""
@ -42,7 +42,7 @@ func NewTagsValuesSequenceKeysValues(keys []string, vals []Sequence) *TagsValues
}
}
func NewTagsValuesSequenceValues(prefix string, vals []Sequence) *TagsValuesSequence {
func NewTagsValuesSequenceValues(prefix string, vals []CountableSequence) *TagsValuesSequence {
keys := make([]string, len(vals))
// max tag width
tw := int(math.Ceil(math.Log10(float64(len(vals)))))
@ -82,7 +82,7 @@ func (s *TagsValuesSequence) Count() int { return s.max }
type keyValues struct {
keys []string
vals []Sequence
vals []CountableSequence
}
func (k keyValues) Len() int { return len(k.keys) }

View File

@ -0,0 +1,27 @@
[
{
"Name":"Float",
"name":"float",
"Type":"float64"
},
{
"Name":"Integer",
"name":"integer",
"Type":"int64"
},
{
"Name":"Unsigned",
"name":"unsigned",
"Type":"uint64"
},
{
"Name":"String",
"name":"string",
"Type":"string"
},
{
"Name":"Boolean",
"name":"boolean",
"Type":"bool"
}
]

8
pkg/data/gen/util.go Normal file
View File

@ -0,0 +1,8 @@
package gen
func min(a, b int) int {
if a < b {
return a
}
return b
}

View File

@ -0,0 +1,308 @@
// Generated by tmpl
// https://github.com/benbjohnson/tmpl
//
// DO NOT EDIT!
// Source: values_constant.gen.go.tmpl
package gen
import (
"time"
"github.com/influxdata/platform/tsdb/cursors"
)
type FloatConstantValuesSequence struct {
vals FloatArray
n int
t int64
state struct {
n int
t int64
d int64
v float64
}
}
func NewFloatConstantValuesSequence(n int, start time.Time, delta time.Duration, v float64) *FloatConstantValuesSequence {
g := &FloatConstantValuesSequence{
vals: *NewFloatArrayLen(cursors.DefaultMaxPointsPerBlock),
}
g.state.n = n
g.state.t = start.UnixNano()
g.state.d = int64(delta)
g.state.v = v
g.Reset()
return g
}
func (g *FloatConstantValuesSequence) Reset() {
g.n = g.state.n
g.t = g.state.t
}
func (g *FloatConstantValuesSequence) Next() bool {
if g.n == 0 {
return false
}
c := min(g.n, cursors.DefaultMaxPointsPerBlock)
g.n -= c
g.vals.Timestamps = g.vals.Timestamps[:c]
g.vals.Values = g.vals.Values[:c]
var (
t = g.t
ts = g.vals.Timestamps
vs = g.vals.Values
d = g.state.d
)
for i := 0; i < len(ts) && i < len(vs); i++ {
ts[i] = g.t
vs[i] = g.state.v
t += d
}
g.t = t
return true
}
func (g *FloatConstantValuesSequence) Values() Values {
return &g.vals
}
type IntegerConstantValuesSequence struct {
vals IntegerArray
n int
t int64
state struct {
n int
t int64
d int64
v int64
}
}
func NewIntegerConstantValuesSequence(n int, start time.Time, delta time.Duration, v int64) *IntegerConstantValuesSequence {
g := &IntegerConstantValuesSequence{
vals: *NewIntegerArrayLen(cursors.DefaultMaxPointsPerBlock),
}
g.state.n = n
g.state.t = start.UnixNano()
g.state.d = int64(delta)
g.state.v = v
g.Reset()
return g
}
func (g *IntegerConstantValuesSequence) Reset() {
g.n = g.state.n
g.t = g.state.t
}
func (g *IntegerConstantValuesSequence) Next() bool {
if g.n == 0 {
return false
}
c := min(g.n, cursors.DefaultMaxPointsPerBlock)
g.n -= c
g.vals.Timestamps = g.vals.Timestamps[:c]
g.vals.Values = g.vals.Values[:c]
var (
t = g.t
ts = g.vals.Timestamps
vs = g.vals.Values
d = g.state.d
)
for i := 0; i < len(ts) && i < len(vs); i++ {
ts[i] = g.t
vs[i] = g.state.v
t += d
}
g.t = t
return true
}
func (g *IntegerConstantValuesSequence) Values() Values {
return &g.vals
}
type UnsignedConstantValuesSequence struct {
vals UnsignedArray
n int
t int64
state struct {
n int
t int64
d int64
v uint64
}
}
func NewUnsignedConstantValuesSequence(n int, start time.Time, delta time.Duration, v uint64) *UnsignedConstantValuesSequence {
g := &UnsignedConstantValuesSequence{
vals: *NewUnsignedArrayLen(cursors.DefaultMaxPointsPerBlock),
}
g.state.n = n
g.state.t = start.UnixNano()
g.state.d = int64(delta)
g.state.v = v
g.Reset()
return g
}
func (g *UnsignedConstantValuesSequence) Reset() {
g.n = g.state.n
g.t = g.state.t
}
func (g *UnsignedConstantValuesSequence) Next() bool {
if g.n == 0 {
return false
}
c := min(g.n, cursors.DefaultMaxPointsPerBlock)
g.n -= c
g.vals.Timestamps = g.vals.Timestamps[:c]
g.vals.Values = g.vals.Values[:c]
var (
t = g.t
ts = g.vals.Timestamps
vs = g.vals.Values
d = g.state.d
)
for i := 0; i < len(ts) && i < len(vs); i++ {
ts[i] = g.t
vs[i] = g.state.v
t += d
}
g.t = t
return true
}
func (g *UnsignedConstantValuesSequence) Values() Values {
return &g.vals
}
type StringConstantValuesSequence struct {
vals StringArray
n int
t int64
state struct {
n int
t int64
d int64
v string
}
}
func NewStringConstantValuesSequence(n int, start time.Time, delta time.Duration, v string) *StringConstantValuesSequence {
g := &StringConstantValuesSequence{
vals: *NewStringArrayLen(cursors.DefaultMaxPointsPerBlock),
}
g.state.n = n
g.state.t = start.UnixNano()
g.state.d = int64(delta)
g.state.v = v
g.Reset()
return g
}
func (g *StringConstantValuesSequence) Reset() {
g.n = g.state.n
g.t = g.state.t
}
func (g *StringConstantValuesSequence) Next() bool {
if g.n == 0 {
return false
}
c := min(g.n, cursors.DefaultMaxPointsPerBlock)
g.n -= c
g.vals.Timestamps = g.vals.Timestamps[:c]
g.vals.Values = g.vals.Values[:c]
var (
t = g.t
ts = g.vals.Timestamps
vs = g.vals.Values
d = g.state.d
)
for i := 0; i < len(ts) && i < len(vs); i++ {
ts[i] = g.t
vs[i] = g.state.v
t += d
}
g.t = t
return true
}
func (g *StringConstantValuesSequence) Values() Values {
return &g.vals
}
type BooleanConstantValuesSequence struct {
vals BooleanArray
n int
t int64
state struct {
n int
t int64
d int64
v bool
}
}
func NewBooleanConstantValuesSequence(n int, start time.Time, delta time.Duration, v bool) *BooleanConstantValuesSequence {
g := &BooleanConstantValuesSequence{
vals: *NewBooleanArrayLen(cursors.DefaultMaxPointsPerBlock),
}
g.state.n = n
g.state.t = start.UnixNano()
g.state.d = int64(delta)
g.state.v = v
g.Reset()
return g
}
func (g *BooleanConstantValuesSequence) Reset() {
g.n = g.state.n
g.t = g.state.t
}
func (g *BooleanConstantValuesSequence) Next() bool {
if g.n == 0 {
return false
}
c := min(g.n, cursors.DefaultMaxPointsPerBlock)
g.n -= c
g.vals.Timestamps = g.vals.Timestamps[:c]
g.vals.Values = g.vals.Values[:c]
var (
t = g.t
ts = g.vals.Timestamps
vs = g.vals.Values
d = g.state.d
)
for i := 0; i < len(ts) && i < len(vs); i++ {
ts[i] = g.t
vs[i] = g.state.v
t += d
}
g.t = t
return true
}
func (g *BooleanConstantValuesSequence) Values() Values {
return &g.vals
}

View File

@ -0,0 +1,68 @@
package gen
import (
"time"
"github.com/influxdata/platform/tsdb/cursors"
)
{{range .}}
type {{.Name}}ConstantValuesSequence struct {
vals {{.Name}}Array
n int
t int64
state struct {
n int
t int64
d int64
v {{.Type}}
}
}
func New{{.Name}}ConstantValuesSequence(n int, start time.Time, delta time.Duration, v {{.Type}}) *{{.Name}}ConstantValuesSequence {
g := &{{.Name}}ConstantValuesSequence{
vals: *New{{.Name}}ArrayLen(cursors.DefaultMaxPointsPerBlock),
}
g.state.n = n
g.state.t = start.UnixNano()
g.state.d = int64(delta)
g.state.v = v
g.Reset()
return g
}
func (g *{{.Name}}ConstantValuesSequence) Reset() {
g.n = g.state.n
g.t = g.state.t
}
func (g *{{.Name}}ConstantValuesSequence) Next() bool {
if g.n == 0 {
return false
}
c := min(g.n, cursors.DefaultMaxPointsPerBlock)
g.n -= c
g.vals.Timestamps = g.vals.Timestamps[:c]
g.vals.Values = g.vals.Values[:c]
var (
t = g.t
ts = g.vals.Timestamps
vs = g.vals.Values
d = g.state.d
)
for i := 0; i < len(ts) && i < len(vs); i++ {
ts[i] = g.t
vs[i] = g.state.v
t += d
}
g.t = t
return true
}
func (g *{{.Name}}ConstantValuesSequence) Values() Values {
return &g.vals
}
{{end}}

View File

@ -0,0 +1,60 @@
package gen
import (
"math/rand"
"time"
"github.com/influxdata/platform/tsdb/cursors"
)
type FloatRandomValuesSequence struct {
buf FloatArray
vals FloatArray
n int
t int64
state struct {
n int
t int64
d int64
scale float64
}
}
func NewFloatRandomValuesSequence(n int, start time.Time, delta time.Duration, scale float64) *FloatRandomValuesSequence {
g := &FloatRandomValuesSequence{
buf: *NewFloatArrayLen(cursors.DefaultMaxPointsPerBlock),
}
g.state.n = n
g.state.t = start.UnixNano()
g.state.d = int64(delta)
g.state.scale = scale
g.Reset()
return g
}
func (g *FloatRandomValuesSequence) Reset() {
g.n = g.state.n
g.t = g.state.t
}
func (g *FloatRandomValuesSequence) Next() bool {
if g.n == 0 {
return false
}
c := min(g.n, cursors.DefaultMaxPointsPerBlock)
g.n -= c
g.vals.Timestamps = g.buf.Timestamps[:0]
g.vals.Values = g.buf.Values[:0]
for i := 0; i < c; i++ {
g.vals.Timestamps = append(g.vals.Timestamps, g.t)
g.vals.Values = append(g.vals.Values, rand.Float64()*g.state.scale)
g.t += g.state.d
}
return true
}
func (g *FloatRandomValuesSequence) Values() Values {
return &g.vals
}

View File

@ -7,7 +7,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/influxdata/platform/models"
"github.com/influxdata/platform/pkg/testing/gen"
"github.com/influxdata/platform/pkg/data/gen"
"github.com/influxdata/platform/storage/reads"
"github.com/influxdata/platform/storage/reads/datatypes"
)
@ -310,7 +310,7 @@ func (s *sliceSeriesCursor) Next() *reads.SeriesRow {
func BenchmarkNewGroupResultSet_GroupBy(b *testing.B) {
card := []int{10, 10, 10}
vals := make([]gen.Sequence, len(card))
vals := make([]gen.CountableSequence, len(card))
for i := range card {
vals[i] = gen.NewCounterByteSequenceCount(card[i])
}