Skip to content

Commit

Permalink
Merge pull request #10414 from seebs/seebs/valuerReuse
Browse files Browse the repository at this point in the history
reuse ValuerEval objects
  • Loading branch information
jsternberg authored Feb 6, 2019
2 parents def9589 + 5525240 commit 2811cde
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 57 deletions.
32 changes: 17 additions & 15 deletions query/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ type scannerCursorBase struct {
columns []influxql.VarRef
loc *time.Location

scan scannerFunc
scan scannerFunc
valuer influxql.ValuerEval
}

func newScannerCursorBase(scan scannerFunc, fields []*influxql.Field, loc *time.Location) scannerCursorBase {
Expand All @@ -162,12 +163,20 @@ func newScannerCursorBase(scan scannerFunc, fields []*influxql.Field, loc *time.
loc = time.UTC
}

m := make(map[string]interface{})
return scannerCursorBase{
fields: exprs,
m: make(map[string]interface{}),
m: m,
columns: columns,
loc: loc,
scan: scan,
valuer: influxql.ValuerEval{
Valuer: influxql.MultiValuer(
MathValuer{},
influxql.MapValuer(m),
),
IntegerFloatDivision: true,
},
}
}

Expand All @@ -189,20 +198,13 @@ func (cur *scannerCursorBase) Scan(row *Row) bool {
row.Values = make([]interface{}, len(cur.columns))
}

valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
MathValuer{},
influxql.MapValuer(cur.m),
),
IntegerFloatDivision: true,
}
for i, expr := range cur.fields {
// A special case if the field is time to reduce memory allocations.
if ref, ok := expr.(*influxql.VarRef); ok && ref.Val == "time" {
row.Values[i] = time.Unix(0, row.Time).In(cur.loc)
continue
}
v := valuer.Eval(expr)
v := cur.valuer.Eval(expr)
if fv, ok := v.(float64); ok && math.IsNaN(fv) {
// If the float value is NaN, convert it to a null float
// so this can be serialized correctly, but not mistaken for
Expand Down Expand Up @@ -339,6 +341,7 @@ type filterCursor struct {
fields map[string]IteratorMap
filter influxql.Expr
m map[string]interface{}
valuer influxql.ValuerEval
}

func newFilterCursor(cur Cursor, filter influxql.Expr) *filterCursor {
Expand All @@ -362,11 +365,13 @@ func newFilterCursor(cur Cursor, filter influxql.Expr) *filterCursor {
fields[name.Val] = TagMap(name.Val)
}
}
m := make(map[string]interface{})
return &filterCursor{
Cursor: cur,
fields: fields,
filter: filter,
m: make(map[string]interface{}),
m: m,
valuer: influxql.ValuerEval{Valuer: influxql.MapValuer(m)},
}
}

Expand All @@ -377,10 +382,7 @@ func (cur *filterCursor) Scan(row *Row) bool {
cur.m[name] = f.Value(row)
}

valuer := influxql.ValuerEval{
Valuer: influxql.MapValuer(cur.m),
}
if valuer.EvalBool(cur.filter) {
if cur.valuer.EvalBool(cur.filter) {
// Passes the filter! Return true. We no longer need to
// search for a suitable value.
return true
Expand Down
80 changes: 45 additions & 35 deletions tsdb/engine/tsm1/iterator.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type floatIterator struct {
statsLock sync.Mutex
stats query.IteratorStats
statsBuf query.IteratorStats
valuer influxql.ValuerEval
}

func newFloatIterator(name string, tags query.Tags, opt query.IteratorOptions, cur floatCursor, aux []cursorAt, conds []cursorAt, condNames []string) *floatIterator {
Expand Down Expand Up @@ -223,6 +224,13 @@ func newFloatIterator(name string, tags query.Tags, opt query.IteratorOptions, c
itr.conds.names = condNames
itr.conds.curs = conds

itr.valuer = influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}

return itr
}

Expand Down Expand Up @@ -270,13 +278,7 @@ func (itr *floatIterator) Next() (*query.FloatPoint, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
if itr.opt.Condition != nil && !itr.valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down Expand Up @@ -674,6 +676,7 @@ type integerIterator struct {
statsLock sync.Mutex
stats query.IteratorStats
statsBuf query.IteratorStats
valuer influxql.ValuerEval
}

func newIntegerIterator(name string, tags query.Tags, opt query.IteratorOptions, cur integerCursor, aux []cursorAt, conds []cursorAt, condNames []string) *integerIterator {
Expand Down Expand Up @@ -701,6 +704,13 @@ func newIntegerIterator(name string, tags query.Tags, opt query.IteratorOptions,
itr.conds.names = condNames
itr.conds.curs = conds

itr.valuer = influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}

return itr
}

Expand Down Expand Up @@ -748,13 +758,7 @@ func (itr *integerIterator) Next() (*query.IntegerPoint, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
if itr.opt.Condition != nil && !itr.valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down Expand Up @@ -1152,6 +1156,7 @@ type unsignedIterator struct {
statsLock sync.Mutex
stats query.IteratorStats
statsBuf query.IteratorStats
valuer influxql.ValuerEval
}

func newUnsignedIterator(name string, tags query.Tags, opt query.IteratorOptions, cur unsignedCursor, aux []cursorAt, conds []cursorAt, condNames []string) *unsignedIterator {
Expand Down Expand Up @@ -1179,6 +1184,13 @@ func newUnsignedIterator(name string, tags query.Tags, opt query.IteratorOptions
itr.conds.names = condNames
itr.conds.curs = conds

itr.valuer = influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}

return itr
}

Expand Down Expand Up @@ -1226,13 +1238,7 @@ func (itr *unsignedIterator) Next() (*query.UnsignedPoint, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
if itr.opt.Condition != nil && !itr.valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down Expand Up @@ -1630,6 +1636,7 @@ type stringIterator struct {
statsLock sync.Mutex
stats query.IteratorStats
statsBuf query.IteratorStats
valuer influxql.ValuerEval
}

func newStringIterator(name string, tags query.Tags, opt query.IteratorOptions, cur stringCursor, aux []cursorAt, conds []cursorAt, condNames []string) *stringIterator {
Expand Down Expand Up @@ -1657,6 +1664,13 @@ func newStringIterator(name string, tags query.Tags, opt query.IteratorOptions,
itr.conds.names = condNames
itr.conds.curs = conds

itr.valuer = influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}

return itr
}

Expand Down Expand Up @@ -1704,13 +1718,7 @@ func (itr *stringIterator) Next() (*query.StringPoint, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
if itr.opt.Condition != nil && !itr.valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down Expand Up @@ -2108,6 +2116,7 @@ type booleanIterator struct {
statsLock sync.Mutex
stats query.IteratorStats
statsBuf query.IteratorStats
valuer influxql.ValuerEval
}

func newBooleanIterator(name string, tags query.Tags, opt query.IteratorOptions, cur booleanCursor, aux []cursorAt, conds []cursorAt, condNames []string) *booleanIterator {
Expand Down Expand Up @@ -2135,6 +2144,13 @@ func newBooleanIterator(name string, tags query.Tags, opt query.IteratorOptions,
itr.conds.names = condNames
itr.conds.curs = conds

itr.valuer = influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}

return itr
}

Expand Down Expand Up @@ -2182,13 +2198,7 @@ func (itr *booleanIterator) Next() (*query.BooleanPoint, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
if itr.opt.Condition != nil && !itr.valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down
16 changes: 9 additions & 7 deletions tsdb/engine/tsm1/iterator.gen.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ type {{.name}}Iterator struct {
statsLock sync.Mutex
stats query.IteratorStats
statsBuf query.IteratorStats
valuer influxql.ValuerEval
}

func new{{.Name}}Iterator(name string, tags query.Tags, opt query.IteratorOptions, cur {{.name}}Cursor, aux []cursorAt, conds []cursorAt, condNames []string) *{{.name}}Iterator {
Expand Down Expand Up @@ -221,6 +222,13 @@ func new{{.Name}}Iterator(name string, tags query.Tags, opt query.IteratorOption
itr.conds.names = condNames
itr.conds.curs = conds

itr.valuer = influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}

return itr
}

Expand Down Expand Up @@ -268,13 +276,7 @@ func (itr *{{.name}}Iterator) Next() (*query.{{.Name}}Point, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
if itr.opt.Condition != nil && !itr.valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down

0 comments on commit 2811cde

Please sign in to comment.