influxdb/tsdb/tsm1/array_cursor_iterator.go

102 lines
2.9 KiB
Go
Raw Normal View History

2018-09-26 17:39:21 +00:00
package tsm1
import (
"context"
"fmt"
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/pkg/metrics"
"github.com/influxdata/influxdb/query"
"github.com/influxdata/influxdb/tsdb"
"github.com/influxdata/influxdb/tsdb/cursors"
2018-09-26 17:39:21 +00:00
)
type arrayCursorIterator struct {
e *Engine
key []byte
asc struct {
Float *floatArrayAscendingCursor
Integer *integerArrayAscendingCursor
Unsigned *unsignedArrayAscendingCursor
Boolean *booleanArrayAscendingCursor
String *stringArrayAscendingCursor
}
desc struct {
Float *floatArrayDescendingCursor
Integer *integerArrayDescendingCursor
Unsigned *unsignedArrayDescendingCursor
Boolean *booleanArrayDescendingCursor
String *stringArrayDescendingCursor
}
}
func (q *arrayCursorIterator) Next(ctx context.Context, r *tsdb.CursorRequest) (tsdb.Cursor, error) {
q.key = tsdb.AppendSeriesKey(q.key[:0], r.Name, r.Tags)
id := q.e.sfile.SeriesIDTypedBySeriesKey(q.key)
if id.IsZero() {
2018-09-26 17:39:21 +00:00
return nil, nil
}
if grp := metrics.GroupFromContext(ctx); grp != nil {
grp.GetCounter(numberOfRefCursorsCounter).Add(1)
}
var opt query.IteratorOptions
opt.Ascending = r.Ascending
opt.StartTime = r.StartTime
opt.EndTime = r.EndTime
// Return appropriate cursor based on type.
switch typ := id.Type(); typ {
case models.Float:
2018-09-26 17:39:21 +00:00
return q.buildFloatArrayCursor(ctx, r.Name, r.Tags, r.Field, opt), nil
case models.Integer:
2018-09-26 17:39:21 +00:00
return q.buildIntegerArrayCursor(ctx, r.Name, r.Tags, r.Field, opt), nil
case models.Unsigned:
2018-09-26 17:39:21 +00:00
return q.buildUnsignedArrayCursor(ctx, r.Name, r.Tags, r.Field, opt), nil
case models.String:
2018-09-26 17:39:21 +00:00
return q.buildStringArrayCursor(ctx, r.Name, r.Tags, r.Field, opt), nil
case models.Boolean:
2018-09-26 17:39:21 +00:00
return q.buildBooleanArrayCursor(ctx, r.Name, r.Tags, r.Field, opt), nil
default:
panic(fmt.Sprintf("unreachable: %v", typ))
2018-09-26 17:39:21 +00:00
}
}
func (q *arrayCursorIterator) seriesFieldKeyBytes(name []byte, tags models.Tags, field string) []byte {
q.key = models.AppendMakeKey(q.key[:0], name, tags)
2019-02-18 12:47:29 +00:00
q.key = append(q.key, KeyFieldSeparatorBytes...)
2018-09-26 17:39:21 +00:00
q.key = append(q.key, field...)
return q.key
}
// Stats returns the cumulative stats for all cursors.
func (q *arrayCursorIterator) Stats() cursors.CursorStats {
var stats cursors.CursorStats
if cur := q.asc.Float; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.asc.Integer; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.asc.Unsigned; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.asc.Boolean; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.asc.String; cur != nil {
stats.Add(cur.Stats())
}
if cur := q.desc.Float; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.desc.Integer; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.desc.Unsigned; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.desc.Boolean; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.desc.String; cur != nil {
stats.Add(cur.Stats())
}
return stats
}