-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathreader_range_iterator.go
199 lines (168 loc) · 4.38 KB
/
reader_range_iterator.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
package tsm1
import (
"github.com/influxdata/influxdb/tsdb"
"github.com/influxdata/influxdb/tsdb/cursors"
)
// TimeRangeIterator will iterate over the keys of a TSM file, starting at
// the provided key. It is used to determine if each key has data which exists
// within a specified time interval.
type TimeRangeIterator struct {
r *TSMReader
iter *TSMIndexIterator
tr TimeRange
err error
stats cursors.CursorStats
// temporary storage
trbuf []TimeRange
buf []byte
a tsdb.TimestampArray
}
func (b *TimeRangeIterator) Err() error {
if b.err != nil {
return b.err
}
return b.iter.Err()
}
// Next advances the iterator and reports if it is still valid.
func (b *TimeRangeIterator) Next() bool {
if b.Err() != nil {
return false
}
return b.iter.Next()
}
// Seek points the iterator at the smallest key greater than or equal to the
// given key, returning true if it was an exact match. It returns false for
// ok if the key does not exist.
func (b *TimeRangeIterator) Seek(key []byte) (exact, ok bool) {
if b.Err() != nil {
return false, false
}
return b.iter.Seek(key)
}
// Key reports the current key.
func (b *TimeRangeIterator) Key() []byte {
return b.iter.Key()
}
// HasData reports true if the current key has data for the time range.
func (b *TimeRangeIterator) HasData() bool {
if b.Err() != nil {
return false
}
e := excludeEntries(b.iter.Entries(), b.tr)
if len(e) == 0 {
return false
}
b.trbuf = b.r.TombstoneRange(b.iter.Key(), b.trbuf[:0])
var ts []TimeRange
if len(b.trbuf) > 0 {
ts = excludeTimeRanges(b.trbuf, b.tr)
}
if len(ts) == 0 {
// no tombstones, fast path will avoid decoding blocks
// if queried time interval intersects with one of the entries
if intersectsEntry(e, b.tr) {
return true
}
for i := range e {
if !b.readBlock(&e[i]) {
return false
}
if b.a.Contains(b.tr.Min, b.tr.Max) {
return true
}
}
} else {
for i := range e {
if !b.readBlock(&e[i]) {
return false
}
// remove tombstoned timestamps
for i := range ts {
b.a.Exclude(ts[i].Min, ts[i].Max)
}
if b.a.Contains(b.tr.Min, b.tr.Max) {
return true
}
}
}
return false
}
// readBlock reads the block identified by IndexEntry e and accumulates
// statistics. readBlock returns true on success.
func (b *TimeRangeIterator) readBlock(e *IndexEntry) bool {
_, b.buf, b.err = b.r.ReadBytes(e, b.buf)
if b.err != nil {
return false
}
b.err = DecodeTimestampArrayBlock(b.buf, &b.a)
if b.err != nil {
return false
}
b.stats.ScannedBytes += b.a.Len() * 8 // sizeof Timestamp (int64)
b.stats.ScannedValues += b.a.Len()
return true
}
// Stats returns statistics accumulated by the iterator for any block reads.
func (b *TimeRangeIterator) Stats() cursors.CursorStats {
return b.stats
}
/*
intersectsEntry determines whether the range [min, max]
intersects one or both boundaries of IndexEntry.
+------------------+
| IndexEntry |
+---------+------------------+---------+
| RANGE | | RANGE |
+-+-------+-+ +----+----+----+
| RANGE | | RANGE |
+----+----+-----------+---------+
| RANGE |
+--------------------------+
*/
// intersectsEntry determines if tr overlaps one or both boundaries
// of at least one element of e. If that is the case,
// and the block has no tombstones, the block timestamps do not
// need to be decoded.
func intersectsEntry(e []IndexEntry, tr TimeRange) bool {
for i := range e {
min, max := e[i].MinTime, e[i].MaxTime
if tr.Overlaps(min, max) && !tr.Within(min, max) {
return true
}
}
return false
}
// excludeEntries returns a slice which excludes leading and trailing
// elements of e that are outside the time range specified by tr.
func excludeEntries(e []IndexEntry, tr TimeRange) []IndexEntry {
for i := range e {
if e[i].OverlapsTimeRange(tr.Min, tr.Max) {
e = e[i:]
break
}
}
for i := range e {
if !e[i].OverlapsTimeRange(tr.Min, tr.Max) {
e = e[:i]
break
}
}
return e
}
// excludeTimeRanges returns a slice which excludes leading and trailing
// elements of e that are outside the time range specified by tr.
func excludeTimeRanges(e []TimeRange, tr TimeRange) []TimeRange {
for i := range e {
if e[i].Overlaps(tr.Min, tr.Max) {
e = e[i:]
break
}
}
for i := range e {
if !e[i].Overlaps(tr.Min, tr.Max) {
e = e[:i]
break
}
}
return e
}