forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
series_cursor.go
155 lines (135 loc) · 3.1 KB
/
series_cursor.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package tsdb
import (
"bytes"
"errors"
"sort"
"sync"
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxql"
)
type SeriesCursor interface {
Close() error
Next() (*SeriesCursorRow, error)
}
type SeriesCursorRequest struct {
Measurements MeasurementIterator
}
// seriesCursor is an implementation of SeriesCursor over an IndexSet.
type seriesCursor struct {
once sync.Once
indexSet IndexSet
mitr MeasurementIterator
keys [][]byte
ofs int
row SeriesCursorRow
cond influxql.Expr
}
type SeriesCursorRow struct {
Name []byte
Tags models.Tags
}
func (r *SeriesCursorRow) Compare(other *SeriesCursorRow) int {
if r == other {
return 0
} else if r == nil {
return -1
} else if other == nil {
return 1
}
cmp := bytes.Compare(r.Name, other.Name)
if cmp != 0 {
return cmp
}
return models.CompareTags(r.Tags, other.Tags)
}
// newSeriesCursor returns a new instance of SeriesCursor.
func newSeriesCursor(req SeriesCursorRequest, indexSet IndexSet, cond influxql.Expr) (_ SeriesCursor, err error) {
// Only equality operators are allowed.
influxql.WalkFunc(cond, func(node influxql.Node) {
switch n := node.(type) {
case *influxql.BinaryExpr:
switch n.Op {
case influxql.EQ, influxql.NEQ, influxql.EQREGEX, influxql.NEQREGEX, influxql.OR, influxql.AND:
default:
err = errors.New("invalid tag comparison operator")
}
}
})
if err != nil {
return nil, err
}
mitr := req.Measurements
if mitr == nil {
mitr, err = indexSet.MeasurementIterator()
if err != nil {
return nil, err
}
}
return &seriesCursor{
indexSet: indexSet,
mitr: mitr,
cond: cond,
}, nil
}
// Close closes the iterator.
func (cur *seriesCursor) Close() (err error) {
cur.once.Do(func() {
if cur.mitr != nil {
err = cur.mitr.Close()
}
})
return err
}
// Next emits the next point in the iterator.
func (cur *seriesCursor) Next() (*SeriesCursorRow, error) {
for {
// Read series keys for next measurement if no more keys remaining.
// Exit if there are no measurements remaining.
if cur.ofs == len(cur.keys) {
m, err := cur.mitr.Next()
if err != nil {
return nil, err
} else if m == nil {
return nil, nil
}
if err := cur.readSeriesKeys(m); err != nil {
return nil, err
}
continue
}
cur.row.Name, cur.row.Tags = ParseSeriesKey(cur.keys[cur.ofs])
cur.ofs++
//if itr.opt.Authorizer != nil && !itr.opt.Authorizer.AuthorizeSeriesRead(itr.indexSet.Database(), name, tags) {
// continue
//}
return &cur.row, nil
}
}
func (cur *seriesCursor) readSeriesKeys(name []byte) error {
sitr, err := cur.indexSet.MeasurementSeriesByExprIterator(name, cur.cond)
if err != nil {
return err
} else if sitr == nil {
return nil
}
defer sitr.Close()
// Slurp all series keys.
cur.ofs = 0
cur.keys = cur.keys[:0]
for {
elem, err := sitr.Next()
if err != nil {
return err
} else if elem.SeriesID == 0 {
break
}
key := cur.indexSet.SeriesFile.SeriesKey(elem.SeriesID)
if len(key) == 0 {
continue
}
cur.keys = append(cur.keys, key)
}
// Sort keys.
sort.Sort(seriesKeys(cur.keys))
return nil
}