forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
3223 lines (2810 loc) · 83.1 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tsdb
import (
"bytes"
"errors"
"fmt"
"os"
"regexp"
"sort"
"sync"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/influxql/query"
"github.com/influxdata/influxdb/v2/models"
"github.com/influxdata/influxdb/v2/pkg/bytesutil"
"github.com/influxdata/influxdb/v2/pkg/estimator"
"github.com/influxdata/influxdb/v2/pkg/slices"
"github.com/influxdata/influxql"
"go.uber.org/zap"
)
// Available index types.
const (
TSI1IndexName = "tsi1"
)
// ErrIndexClosing can be returned to from an Index method if the index is currently closing.
var ErrIndexClosing = errors.New("index is closing")
type Index interface {
Open() error
Close() error
WithLogger(*zap.Logger)
Database() string
MeasurementExists(name []byte) (bool, error)
MeasurementNamesByRegex(re *regexp.Regexp) ([][]byte, error)
DropMeasurement(name []byte) error
ForEachMeasurementName(fn func(name []byte) error) error
CreateSeriesIfNotExists(key, name []byte, tags models.Tags) error
CreateSeriesListIfNotExists(keys, names [][]byte, tags []models.Tags) error
DropSeries(seriesID uint64, key []byte, cascade bool) error
DropMeasurementIfSeriesNotExist(name []byte) (bool, error)
MeasurementsSketches() (estimator.Sketch, estimator.Sketch, error)
SeriesN() int64
SeriesSketches() (estimator.Sketch, estimator.Sketch, error)
SeriesIDSet() *SeriesIDSet
HasTagKey(name, key []byte) (bool, error)
HasTagValue(name, key, value []byte) (bool, error)
MeasurementTagKeysByExpr(name []byte, expr influxql.Expr) (map[string]struct{}, error)
TagKeyCardinality(name, key []byte) int
// InfluxQL system iterators
MeasurementIterator() (MeasurementIterator, error)
TagKeyIterator(name []byte) (TagKeyIterator, error)
TagValueIterator(name, key []byte) (TagValueIterator, error)
MeasurementSeriesIDIterator(name []byte) (SeriesIDIterator, error)
TagKeySeriesIDIterator(name, key []byte) (SeriesIDIterator, error)
TagValueSeriesIDIterator(name, key, value []byte) (SeriesIDIterator, error)
// Sets a shared fieldset from the engine.
FieldSet() *MeasurementFieldSet
SetFieldSet(fs *MeasurementFieldSet)
// Size of the index on disk, if applicable.
DiskSizeBytes() int64
// Bytes estimates the memory footprint of this Index, in bytes.
Bytes() int
Type() string
// Returns a unique reference ID to the index instance.
UniqueReferenceID() uintptr
}
// SeriesElem represents a generic series element.
type SeriesElem interface {
Name() []byte
Tags() models.Tags
Deleted() bool
// InfluxQL expression associated with series during filtering.
Expr() influxql.Expr
}
// SeriesIterator represents a iterator over a list of series.
type SeriesIterator interface {
Close() error
Next() (SeriesElem, error)
}
// NewSeriesIteratorAdapter returns an adapter for converting series ids to series.
func NewSeriesIteratorAdapter(sfile *SeriesFile, itr SeriesIDIterator) SeriesIterator {
return &seriesIteratorAdapter{
sfile: sfile,
itr: itr,
}
}
type seriesIteratorAdapter struct {
sfile *SeriesFile
itr SeriesIDIterator
}
func (itr *seriesIteratorAdapter) Close() error { return itr.itr.Close() }
func (itr *seriesIteratorAdapter) Next() (SeriesElem, error) {
for {
elem, err := itr.itr.Next()
if err != nil {
return nil, err
} else if elem.SeriesID == 0 {
return nil, nil
}
// Skip if this key has been tombstoned.
key := itr.sfile.SeriesKey(elem.SeriesID)
if len(key) == 0 {
continue
}
name, tags := ParseSeriesKey(key)
deleted := itr.sfile.IsDeleted(elem.SeriesID)
return &seriesElemAdapter{
name: name,
tags: tags,
deleted: deleted,
expr: elem.Expr,
}, nil
}
}
type seriesElemAdapter struct {
name []byte
tags models.Tags
deleted bool
expr influxql.Expr
}
func (e *seriesElemAdapter) Name() []byte { return e.name }
func (e *seriesElemAdapter) Tags() models.Tags { return e.tags }
func (e *seriesElemAdapter) Deleted() bool { return e.deleted }
func (e *seriesElemAdapter) Expr() influxql.Expr { return e.expr }
var _ SeriesIDIterator = (*PredicateSeriesIDIterator)(nil)
type PredicateSeriesIDIterator struct {
itr SeriesIDIterator
sfile *SeriesFile
pred influxdb.Predicate
}
func NewPredicateSeriesIDIterator(itr SeriesIDIterator, sfile *SeriesFile, pred influxdb.Predicate) SeriesIDIterator {
if pred == nil {
return itr
}
return &PredicateSeriesIDIterator{
itr: itr,
sfile: sfile,
pred: pred,
}
}
func (itr *PredicateSeriesIDIterator) Close() error { return itr.itr.Close() }
func (itr *PredicateSeriesIDIterator) Next() (SeriesIDElem, error) {
for {
elem, err := itr.itr.Next()
if elem.SeriesID == 0 || err != nil {
return elem, err
}
// Skip if this key has been tombstoned.
seriesKey := itr.sfile.SeriesKey(elem.SeriesID)
if len(seriesKey) == 0 {
continue
}
name, tags := ParseSeriesKey(seriesKey)
tags = append(models.Tags{{Key: models.MeasurementTagKeyBytes, Value: name}}, tags...)
key := models.MakeKey(name, tags)
if !itr.pred.Matches(key) {
continue
}
return elem, nil
}
}
// SeriesIDElem represents a single series and optional expression.
type SeriesIDElem struct {
SeriesID uint64
Expr influxql.Expr
}
// SeriesIDElems represents a list of series id elements.
type SeriesIDElems []SeriesIDElem
func (a SeriesIDElems) Len() int { return len(a) }
func (a SeriesIDElems) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SeriesIDElems) Less(i, j int) bool { return a[i].SeriesID < a[j].SeriesID }
// SeriesIDIterator represents a iterator over a list of series ids.
type SeriesIDIterator interface {
Next() (SeriesIDElem, error)
Close() error
}
// SeriesKeyIterator represents an iterator over a list of SeriesKeys
type SeriesKeyIterator interface {
Next() ([]byte, error)
Close() error
}
// SeriesIDSetIterator represents an iterator that can produce a SeriesIDSet.
type SeriesIDSetIterator interface {
SeriesIDIterator
SeriesIDSet() *SeriesIDSet
}
type seriesIDSetIterator struct {
ss *SeriesIDSet
itr SeriesIDSetIterable
}
func NewSeriesIDSetIterator(ss *SeriesIDSet) SeriesIDSetIterator {
if ss == nil || ss.bitmap == nil {
return nil
}
return &seriesIDSetIterator{ss: ss, itr: ss.Iterator()}
}
func (itr *seriesIDSetIterator) Next() (SeriesIDElem, error) {
if !itr.itr.HasNext() {
return SeriesIDElem{}, nil
}
return SeriesIDElem{SeriesID: uint64(itr.itr.Next())}, nil
}
func (itr *seriesIDSetIterator) Close() error { return nil }
func (itr *seriesIDSetIterator) SeriesIDSet() *SeriesIDSet { return itr.ss }
// NewSeriesIDSetIterators returns a slice of SeriesIDSetIterator if all itrs
// can be type casted. Otherwise returns nil.
func NewSeriesIDSetIterators(itrs []SeriesIDIterator) []SeriesIDSetIterator {
if len(itrs) == 0 {
return nil
}
a := make([]SeriesIDSetIterator, len(itrs))
for i := range itrs {
if itr, ok := itrs[i].(SeriesIDSetIterator); ok {
a[i] = itr
} else {
return nil
}
}
return a
}
// ReadAllSeriesIDIterator returns all ids from the iterator.
func ReadAllSeriesIDIterator(itr SeriesIDIterator) ([]uint64, error) {
if itr == nil {
return nil, nil
}
var a []uint64
for {
e, err := itr.Next()
if err != nil {
return nil, err
} else if e.SeriesID == 0 {
break
}
a = append(a, e.SeriesID)
}
return a, nil
}
// NewSeriesIDSliceIterator returns a SeriesIDIterator that iterates over a slice.
func NewSeriesIDSliceIterator(ids []uint64) *SeriesIDSliceIterator {
return &SeriesIDSliceIterator{ids: ids}
}
// SeriesIDSliceIterator iterates over a slice of series ids.
type SeriesIDSliceIterator struct {
ids []uint64
}
// Next returns the next series id in the slice.
func (itr *SeriesIDSliceIterator) Next() (SeriesIDElem, error) {
if len(itr.ids) == 0 {
return SeriesIDElem{}, nil
}
id := itr.ids[0]
itr.ids = itr.ids[1:]
return SeriesIDElem{SeriesID: id}, nil
}
func (itr *SeriesIDSliceIterator) Close() error { return nil }
// SeriesIDSet returns a set of all remaining ids.
func (itr *SeriesIDSliceIterator) SeriesIDSet() *SeriesIDSet {
s := NewSeriesIDSet()
for _, id := range itr.ids {
s.AddNoLock(id)
}
return s
}
type SeriesIDIterators []SeriesIDIterator
func (a SeriesIDIterators) Close() (err error) {
for i := range a {
if e := a[i].Close(); e != nil && err == nil {
err = e
}
}
return err
}
func (a SeriesIDIterators) filterNonNil() []SeriesIDIterator {
other := make([]SeriesIDIterator, 0, len(a))
for _, itr := range a {
if itr == nil {
continue
}
other = append(other, itr)
}
return other
}
// seriesQueryAdapterIterator adapts SeriesIDIterator to an influxql.Iterator.
type seriesQueryAdapterIterator struct {
once sync.Once
sfile *SeriesFile
itr SeriesIDIterator
fieldset *MeasurementFieldSet
opt query.IteratorOptions
point query.FloatPoint // reusable point
}
// NewSeriesQueryAdapterIterator returns a new instance of SeriesQueryAdapterIterator.
func NewSeriesQueryAdapterIterator(sfile *SeriesFile, itr SeriesIDIterator, fieldset *MeasurementFieldSet, opt query.IteratorOptions) query.Iterator {
return &seriesQueryAdapterIterator{
sfile: sfile,
itr: itr,
fieldset: fieldset,
point: query.FloatPoint{
Aux: make([]interface{}, len(opt.Aux)),
},
opt: opt,
}
}
// Stats returns stats about the points processed.
func (itr *seriesQueryAdapterIterator) Stats() query.IteratorStats { return query.IteratorStats{} }
// Close closes the iterator.
func (itr *seriesQueryAdapterIterator) Close() error {
itr.once.Do(func() {
itr.itr.Close()
})
return nil
}
// Next emits the next point in the iterator.
func (itr *seriesQueryAdapterIterator) Next() (*query.FloatPoint, error) {
for {
// Read next series element.
e, err := itr.itr.Next()
if err != nil {
return nil, err
} else if e.SeriesID == 0 {
return nil, nil
}
// Skip if key has been tombstoned.
seriesKey := itr.sfile.SeriesKey(e.SeriesID)
if len(seriesKey) == 0 {
continue
}
// Convert to a key.
name, tags := ParseSeriesKey(seriesKey)
key := string(models.MakeKey(name, tags))
// Write auxiliary fields.
for i, f := range itr.opt.Aux {
switch f.Val {
case "key":
itr.point.Aux[i] = key
}
}
return &itr.point, nil
}
}
// filterUndeletedSeriesIDIterator returns all series which are not deleted.
type filterUndeletedSeriesIDIterator struct {
sfile *SeriesFile
itr SeriesIDIterator
}
// FilterUndeletedSeriesIDIterator returns an iterator which filters all deleted series.
func FilterUndeletedSeriesIDIterator(sfile *SeriesFile, itr SeriesIDIterator) SeriesIDIterator {
if itr == nil {
return nil
}
return &filterUndeletedSeriesIDIterator{sfile: sfile, itr: itr}
}
func (itr *filterUndeletedSeriesIDIterator) Close() error {
return itr.itr.Close()
}
func (itr *filterUndeletedSeriesIDIterator) Next() (SeriesIDElem, error) {
for {
e, err := itr.itr.Next()
if err != nil {
return SeriesIDElem{}, err
} else if e.SeriesID == 0 {
return SeriesIDElem{}, nil
} else if itr.sfile.IsDeleted(e.SeriesID) {
continue
}
return e, nil
}
}
// seriesIDExprIterator is an iterator that attaches an associated expression.
type seriesIDExprIterator struct {
itr SeriesIDIterator
expr influxql.Expr
}
// newSeriesIDExprIterator returns a new instance of seriesIDExprIterator.
func newSeriesIDExprIterator(itr SeriesIDIterator, expr influxql.Expr) SeriesIDIterator {
if itr == nil {
return nil
}
return &seriesIDExprIterator{
itr: itr,
expr: expr,
}
}
func (itr *seriesIDExprIterator) Close() error {
return itr.itr.Close()
}
// Next returns the next element in the iterator.
func (itr *seriesIDExprIterator) Next() (SeriesIDElem, error) {
elem, err := itr.itr.Next()
if err != nil {
return SeriesIDElem{}, err
} else if elem.SeriesID == 0 {
return SeriesIDElem{}, nil
}
elem.Expr = itr.expr
return elem, nil
}
// MergeSeriesIDIterators returns an iterator that merges a set of iterators.
// Iterators that are first in the list take precedence and a deletion by those
// early iterators will invalidate elements by later iterators.
func MergeSeriesIDIterators(itrs ...SeriesIDIterator) SeriesIDIterator {
if n := len(itrs); n == 0 {
return nil
} else if n == 1 {
return itrs[0]
}
itrs = SeriesIDIterators(itrs).filterNonNil()
// Merge as series id sets, if available.
if a := NewSeriesIDSetIterators(itrs); a != nil {
sets := make([]*SeriesIDSet, len(a))
for i := range a {
sets[i] = a[i].SeriesIDSet()
}
ss := NewSeriesIDSet()
ss.Merge(sets...)
SeriesIDIterators(itrs).Close()
return NewSeriesIDSetIterator(ss)
}
return &seriesIDMergeIterator{
buf: make([]SeriesIDElem, len(itrs)),
itrs: itrs,
}
}
// seriesIDMergeIterator is an iterator that merges multiple iterators together.
type seriesIDMergeIterator struct {
buf []SeriesIDElem
itrs []SeriesIDIterator
}
func (itr *seriesIDMergeIterator) Close() error {
SeriesIDIterators(itr.itrs).Close()
return nil
}
// Next returns the element with the next lowest name/tags across the iterators.
func (itr *seriesIDMergeIterator) Next() (SeriesIDElem, error) {
// Find next lowest id amongst the buffers.
var elem SeriesIDElem
for i := range itr.buf {
buf := &itr.buf[i]
// Fill buffer.
if buf.SeriesID == 0 {
elem, err := itr.itrs[i].Next()
if err != nil {
return SeriesIDElem{}, nil
} else if elem.SeriesID == 0 {
continue
}
itr.buf[i] = elem
}
if elem.SeriesID == 0 || buf.SeriesID < elem.SeriesID {
elem = *buf
}
}
// Return EOF if no elements remaining.
if elem.SeriesID == 0 {
return SeriesIDElem{}, nil
}
// Clear matching buffers.
for i := range itr.buf {
if itr.buf[i].SeriesID == elem.SeriesID {
itr.buf[i].SeriesID = 0
}
}
return elem, nil
}
// IntersectSeriesIDIterators returns an iterator that only returns series which
// occur in both iterators. If both series have associated expressions then
// they are combined together.
func IntersectSeriesIDIterators(itr0, itr1 SeriesIDIterator) SeriesIDIterator {
if itr0 == nil || itr1 == nil {
if itr0 != nil {
itr0.Close()
}
if itr1 != nil {
itr1.Close()
}
return nil
}
// Create series id set, if available.
if a := NewSeriesIDSetIterators([]SeriesIDIterator{itr0, itr1}); a != nil {
ss := a[0].SeriesIDSet().And(a[1].SeriesIDSet())
// `a` holds references to itr0/itr1 so itr0/itr1 should not be closed when `a` is still in use
itr0.Close()
itr1.Close()
return NewSeriesIDSetIterator(ss)
}
return &seriesIDIntersectIterator{itrs: [2]SeriesIDIterator{itr0, itr1}}
}
// seriesIDIntersectIterator is an iterator that merges two iterators together.
type seriesIDIntersectIterator struct {
buf [2]SeriesIDElem
itrs [2]SeriesIDIterator
}
func (itr *seriesIDIntersectIterator) Close() (err error) {
if e := itr.itrs[0].Close(); e != nil && err == nil {
err = e
}
if e := itr.itrs[1].Close(); e != nil && err == nil {
err = e
}
return err
}
// Next returns the next element which occurs in both iterators.
func (itr *seriesIDIntersectIterator) Next() (_ SeriesIDElem, err error) {
for {
// Fill buffers.
if itr.buf[0].SeriesID == 0 {
if itr.buf[0], err = itr.itrs[0].Next(); err != nil {
return SeriesIDElem{}, err
}
}
if itr.buf[1].SeriesID == 0 {
if itr.buf[1], err = itr.itrs[1].Next(); err != nil {
return SeriesIDElem{}, err
}
}
// Exit if either buffer is still empty.
if itr.buf[0].SeriesID == 0 || itr.buf[1].SeriesID == 0 {
return SeriesIDElem{}, nil
}
// Skip if both series are not equal.
if a, b := itr.buf[0].SeriesID, itr.buf[1].SeriesID; a < b {
itr.buf[0].SeriesID = 0
continue
} else if a > b {
itr.buf[1].SeriesID = 0
continue
}
// Merge series together if equal.
elem := itr.buf[0]
// Attach expression.
expr0 := itr.buf[0].Expr
expr1 := itr.buf[1].Expr
if expr0 == nil {
elem.Expr = expr1
} else if expr1 == nil {
elem.Expr = expr0
} else {
elem.Expr = influxql.Reduce(&influxql.BinaryExpr{
Op: influxql.AND,
LHS: expr0,
RHS: expr1,
}, nil)
}
itr.buf[0].SeriesID, itr.buf[1].SeriesID = 0, 0
return elem, nil
}
}
// UnionSeriesIDIterators returns an iterator that returns series from both
// both iterators. If both series have associated expressions then they are
// combined together.
func UnionSeriesIDIterators(itr0, itr1 SeriesIDIterator) SeriesIDIterator {
// Return other iterator if either one is nil.
if itr0 == nil {
return itr1
} else if itr1 == nil {
return itr0
}
// Create series id set, if available.
if a := NewSeriesIDSetIterators([]SeriesIDIterator{itr0, itr1}); a != nil {
ss := NewSeriesIDSet()
ss.Merge(a[0].SeriesIDSet(), a[1].SeriesIDSet())
// `a` holds references to itr0/itr1 so itr0/itr1 should not be closed when `a` is still in use
itr0.Close()
itr1.Close()
return NewSeriesIDSetIterator(ss)
}
return &seriesIDUnionIterator{itrs: [2]SeriesIDIterator{itr0, itr1}}
}
// seriesIDUnionIterator is an iterator that unions two iterators together.
type seriesIDUnionIterator struct {
buf [2]SeriesIDElem
itrs [2]SeriesIDIterator
}
func (itr *seriesIDUnionIterator) Close() (err error) {
if e := itr.itrs[0].Close(); e != nil && err == nil {
err = e
}
if e := itr.itrs[1].Close(); e != nil && err == nil {
err = e
}
return err
}
// Next returns the next element which occurs in both iterators.
func (itr *seriesIDUnionIterator) Next() (_ SeriesIDElem, err error) {
// Fill buffers.
if itr.buf[0].SeriesID == 0 {
if itr.buf[0], err = itr.itrs[0].Next(); err != nil {
return SeriesIDElem{}, err
}
}
if itr.buf[1].SeriesID == 0 {
if itr.buf[1], err = itr.itrs[1].Next(); err != nil {
return SeriesIDElem{}, err
}
}
// Return non-zero or lesser series.
if a, b := itr.buf[0].SeriesID, itr.buf[1].SeriesID; a == 0 && b == 0 {
return SeriesIDElem{}, nil
} else if b == 0 || (a != 0 && a < b) {
elem := itr.buf[0]
itr.buf[0].SeriesID = 0
return elem, nil
} else if a == 0 || (b != 0 && a > b) {
elem := itr.buf[1]
itr.buf[1].SeriesID = 0
return elem, nil
}
// Attach element.
elem := itr.buf[0]
// Attach expression.
expr0 := itr.buf[0].Expr
expr1 := itr.buf[1].Expr
if expr0 != nil && expr1 != nil {
elem.Expr = influxql.Reduce(&influxql.BinaryExpr{
Op: influxql.OR,
LHS: expr0,
RHS: expr1,
}, nil)
} else {
elem.Expr = nil
}
itr.buf[0].SeriesID, itr.buf[1].SeriesID = 0, 0
return elem, nil
}
// DifferenceSeriesIDIterators returns an iterator that only returns series which
// occur the first iterator but not the second iterator.
func DifferenceSeriesIDIterators(itr0, itr1 SeriesIDIterator) SeriesIDIterator {
if itr0 == nil && itr1 == nil {
return nil
} else if itr1 == nil {
return itr0
} else if itr0 == nil {
itr1.Close()
return nil
}
// Create series id set, if available.
if a := NewSeriesIDSetIterators([]SeriesIDIterator{itr0, itr1}); a != nil {
ss := a[0].SeriesIDSet().AndNot(a[1].SeriesIDSet())
// `a` holds references to itr0/itr1 so itr0/itr1 should not be closed when `a` is still in use
itr0.Close()
itr1.Close()
return NewSeriesIDSetIterator(ss)
}
return &seriesIDDifferenceIterator{itrs: [2]SeriesIDIterator{itr0, itr1}}
}
// seriesIDDifferenceIterator is an iterator that merges two iterators together.
type seriesIDDifferenceIterator struct {
buf [2]SeriesIDElem
itrs [2]SeriesIDIterator
}
func (itr *seriesIDDifferenceIterator) Close() (err error) {
if e := itr.itrs[0].Close(); e != nil && err == nil {
err = e
}
if e := itr.itrs[1].Close(); e != nil && err == nil {
err = e
}
return err
}
// Next returns the next element which occurs only in the first iterator.
func (itr *seriesIDDifferenceIterator) Next() (_ SeriesIDElem, err error) {
for {
// Fill buffers.
if itr.buf[0].SeriesID == 0 {
if itr.buf[0], err = itr.itrs[0].Next(); err != nil {
return SeriesIDElem{}, err
}
}
if itr.buf[1].SeriesID == 0 {
if itr.buf[1], err = itr.itrs[1].Next(); err != nil {
return SeriesIDElem{}, err
}
}
// Exit if first buffer is still empty.
if itr.buf[0].SeriesID == 0 {
return SeriesIDElem{}, nil
} else if itr.buf[1].SeriesID == 0 {
elem := itr.buf[0]
itr.buf[0].SeriesID = 0
return elem, nil
}
// Return first series if it's less.
// If second series is less then skip it.
// If both series are equal then skip both.
if a, b := itr.buf[0].SeriesID, itr.buf[1].SeriesID; a < b {
elem := itr.buf[0]
itr.buf[0].SeriesID = 0
return elem, nil
} else if a > b {
itr.buf[1].SeriesID = 0
continue
} else {
itr.buf[0].SeriesID, itr.buf[1].SeriesID = 0, 0
continue
}
}
}
// seriesPointIterator adapts SeriesIterator to an influxql.Iterator.
type seriesPointIterator struct {
once sync.Once
indexSet IndexSet
mitr MeasurementIterator
keys [][]byte
opt query.IteratorOptions
point query.FloatPoint // reusable point
}
// newSeriesPointIterator returns a new instance of seriesPointIterator.
func NewSeriesPointIterator(indexSet IndexSet, opt query.IteratorOptions) (_ query.Iterator, err error) {
// Only equality operators are allowed.
influxql.WalkFunc(opt.Condition, func(n influxql.Node) {
switch n := n.(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, err := indexSet.MeasurementIterator()
if err != nil {
return nil, err
}
return &seriesPointIterator{
indexSet: indexSet,
mitr: mitr,
point: query.FloatPoint{
Aux: make([]interface{}, len(opt.Aux)),
},
opt: opt,
}, nil
}
// Stats returns stats about the points processed.
func (itr *seriesPointIterator) Stats() query.IteratorStats { return query.IteratorStats{} }
// Close closes the iterator.
func (itr *seriesPointIterator) Close() (err error) {
itr.once.Do(func() {
if itr.mitr != nil {
err = itr.mitr.Close()
}
})
return err
}
// Next emits the next point in the iterator.
func (itr *seriesPointIterator) Next() (*query.FloatPoint, error) {
for {
// Read series keys for next measurement if no more keys remaining.
// Exit if there are no measurements remaining.
if len(itr.keys) == 0 {
m, err := itr.mitr.Next()
if err != nil {
return nil, err
} else if m == nil {
return nil, nil
}
if err := itr.readSeriesKeys(m); err != nil {
return nil, err
}
continue
}
name, tags := ParseSeriesKey(itr.keys[0])
itr.keys = itr.keys[1:]
// TODO(edd): It seems to me like this authorisation check should be
// further down in the index. At this point we're going to be filtering
// series that have already been materialised in the LogFiles and
// IndexFiles.
if itr.opt.Authorizer != nil && !itr.opt.Authorizer.AuthorizeSeriesRead(itr.indexSet.Database(), name, tags) {
continue
}
// Convert to a key.
key := string(models.MakeKey(name, tags))
// Write auxiliary fields.
for i, f := range itr.opt.Aux {
switch f.Val {
case "key":
itr.point.Aux[i] = key
}
}
return &itr.point, nil
}
}
func (itr *seriesPointIterator) readSeriesKeys(name []byte) error {
sitr, err := itr.indexSet.MeasurementSeriesByExprIterator(name, itr.opt.Condition)
if err != nil {
return err
} else if sitr == nil {
return nil
}
defer sitr.Close()
// Slurp all series keys.
itr.keys = itr.keys[:0]
for i := 0; ; i++ {
elem, err := sitr.Next()
if err != nil {
return err
} else if elem.SeriesID == 0 {
break
}
// Periodically check for interrupt.
if i&0xFF == 0xFF {
select {
case <-itr.opt.InterruptCh:
return itr.Close()
default:
}
}
key := itr.indexSet.SeriesFile.SeriesKey(elem.SeriesID)
if len(key) == 0 {
continue
}
itr.keys = append(itr.keys, key)
}
// Sort keys.
sort.Sort(seriesKeys(itr.keys))
return nil
}
// MeasurementIterator represents a iterator over a list of measurements.
type MeasurementIterator interface {
Close() error
Next() ([]byte, error)
}
type MeasurementIterators []MeasurementIterator
func (a MeasurementIterators) Close() (err error) {
for i := range a {
if e := a[i].Close(); e != nil && err == nil {
err = e
}
}
return err
}
type MeasurementSliceIterator interface {
MeasurementIterator
UnderlyingSlice() [][]byte
}
type measurementSliceIterator struct {
names [][]byte
}
// NewMeasurementSliceIterator returns an iterator over a slice of in-memory measurement names.
func NewMeasurementSliceIterator(names [][]byte) *measurementSliceIterator {
return &measurementSliceIterator{names: names}
}
func (itr *measurementSliceIterator) Close() (err error) { return nil }
func (itr *measurementSliceIterator) Next() (name []byte, err error) {
if len(itr.names) == 0 {
return nil, nil
}
name, itr.names = itr.names[0], itr.names[1:]
return name, nil
}
func (itr *measurementSliceIterator) UnderlyingSlice() [][]byte {
return itr.names
}
// fileMeasurementSliceIterator is designed to allow a tag value slice
// iterator to use memory from a memory-mapped file, pinning it