-
Notifications
You must be signed in to change notification settings - Fork 29
/
iterator.go
174 lines (133 loc) · 2.95 KB
/
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
package catena
import (
"errors"
"github.com/Cistern/catena/partition"
)
// An Iterator is a cursor over an array of points
// for a source and metric.
type Iterator struct {
source, metric string
db *DB
curPartition partition.Partition
partition.Iterator
}
// NewIterator creates a new Iterator for the given source and metric.
func (db *DB) NewIterator(source, metric string) (*Iterator, error) {
var p partition.Partition = nil
i := db.partitionList.NewIterator()
for i.Next() {
val, _ := i.Value()
val.Hold()
if val.HasMetric(source, metric) {
if p != nil {
p.Release()
}
p = val
} else {
val.Release()
}
}
if p == nil {
return nil, errors.New("catena: couldn't find metric for iterator")
}
partitionIter, err := p.NewIterator(source, metric)
p.Release()
if err != nil {
return nil, err
}
return &Iterator{
source: source,
metric: metric,
db: db,
curPartition: p,
Iterator: partitionIter,
}, nil
}
// Next advances i to the next available point.
func (i *Iterator) Next() error {
currentPoint := i.Point()
err := i.Iterator.Next()
if err == nil {
return nil
}
err = i.Seek(currentPoint.Timestamp + 1)
return err
}
// Seek moves the iterator to the first timestamp greater than
// or equal to timestamp.
func (i *Iterator) Seek(timestamp int64) error {
if i.Iterator != nil {
i.Iterator.Close()
}
i.Iterator = nil
partitionListIter := i.db.partitionList.NewIterator()
for partitionListIter.Next() {
val, _ := partitionListIter.Value()
val.Hold()
if val.MaxTimestamp() < timestamp {
val.Release()
break
}
if val.HasMetric(i.source, i.metric) {
partitionIter, err := val.NewIterator(i.source, i.metric)
val.Release()
if err != nil {
continue
}
err = partitionIter.Seek(timestamp)
if err != nil {
partitionIter.Close()
continue
}
if i.Iterator != nil {
i.Iterator.Close()
}
i.Iterator = partitionIter
i.curPartition = val
} else {
val.Release()
}
}
if i.Iterator == nil {
return errors.New("catena: couldn't find metric for iterator")
}
return nil
}
// Reset moves i to the first available timestamp.
func (i *Iterator) Reset() error {
i.Iterator.Close()
var p partition.Partition
partitionListIter := i.db.partitionList.NewIterator()
for partitionListIter.Next() {
val, _ := partitionListIter.Value()
val.Hold()
if val.HasMetric(i.source, i.metric) {
if p != nil {
p.Release()
}
p = val
} else {
val.Release()
}
}
if p == nil {
return errors.New("catena: couldn't find metric for iterator")
}
defer p.Release()
i.curPartition = p
partitionIter, err := p.NewIterator(i.source, i.metric)
if err != nil {
return err
}
i.Iterator = partitionIter
return nil
}
// Close closes the iterator. Iterators MUST be closed to unblock
// the compactor!
func (i *Iterator) Close() {
if i.Iterator == nil {
return
}
i.Iterator.Close()
i.curPartition = nil
}