-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.go
More file actions
257 lines (224 loc) · 5.41 KB
/
buffer.go
File metadata and controls
257 lines (224 loc) · 5.41 KB
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
package pipescript
import (
"container/list"
"errors"
"fmt"
)
var ErrBeforeStart = errors.New("There is no value for this query yet")
const bufferPageSize = 100
const backPages = 1
const uintMax = ^uint64(0)
type bufferPage struct {
DP []*Datapoint // Array of datapoints that make up page
I uint64 // Page index
}
type Buffer struct {
Input Iterator
Pages *list.List
// The last page, after which there are no more datapoints
// Initially set to max int64
EndPage uint64
// An error that was encountered
Error error
Iterators []*BufferIterator
}
func NewBuffer(n Iterator) *Buffer {
return &Buffer{
Input: n,
Pages: list.New(),
EndPage: uintMax,
Iterators: make([]*BufferIterator, 0, 1),
}
}
func (b *Buffer) Iterator() *BufferIterator {
it := &BufferIterator{
Buf: b,
Elem: b.Pages.Front(),
J: 0,
}
b.Iterators = append(b.Iterators, it)
return it
}
func (b *Buffer) nextPage() {
// Gets a new page of data. Assumes that it was called by an iterator
// Find the next page index
le := b.Pages.Back()
nextIndex := uint64(0)
if le != nil {
nextIndex = le.Value.(*bufferPage).I + 1
}
// First, check the lowest page index of any iterator
lowestIndex := uintMax
for _, it := range b.Iterators {
if it.Elem == nil {
lowestIndex = 0
break
}
curpage := it.Elem.Value.(*bufferPage).I
if curpage < lowestIndex {
lowestIndex = curpage
}
}
// Now, determine if we need to add a page, or if we can use an existing page
// by taking it from the beginning of the list
fp := b.Pages.Front()
if fp == nil || fp.Value.(*bufferPage).I+backPages >= lowestIndex {
// Need to add a new page
bp := &bufferPage{
DP: make([]*Datapoint, bufferPageSize),
}
b.Pages.PushBack(bp)
} else {
b.Pages.MoveToBack(fp)
}
// We now have a valid page at the back
bp := b.Pages.Back().Value.(*bufferPage)
bp.I = nextIndex
// ... and now populate it with values
for i := range bp.DP {
if bp.DP[i] == nil {
bp.DP[i] = &Datapoint{}
}
bp.DP[i], b.Error = b.Input.Next(bp.DP[i])
if b.Error != nil {
return
}
if bp.DP[i] == nil {
// This is the end of the stream - clear the rest of the array,
// and set the end index
for j := i + 1; j < len(bp.DP); j++ {
bp.DP[j] = nil
}
b.EndPage = nextIndex
break
}
}
}
type BufferIterator struct {
Buf *Buffer
Elem *list.Element
J int
}
func (i *BufferIterator) init() {
if i.Elem == nil {
i.Elem = i.Buf.Pages.Front()
if i.Elem == nil && i.Buf.Error == nil {
i.Buf.nextPage()
}
i.Elem = i.Buf.Pages.Front()
}
}
func (i *BufferIterator) Next() (*Datapoint, error) {
i.init()
if i.Buf.Error != nil {
return nil, i.Buf.Error
}
bp := i.Elem.Value.(*bufferPage)
if i.J >= bufferPageSize {
// We are at the end of a page. Move to the next one
// ...but if it is the last page, just return nil
if bp.I >= i.Buf.EndPage {
return nil, nil
}
ne := i.Elem.Next()
if ne == nil {
// Ask the buffer for another page
i.Buf.nextPage()
if i.Buf.Error != nil {
return nil, i.Buf.Error
}
ne = i.Elem.Next()
}
i.Elem = ne
bp = i.Elem.Value.(*bufferPage)
i.J = 0
}
dp := bp.DP[i.J]
i.J++
return dp, nil
}
func (i *BufferIterator) Peek(idx int) (*Datapoint, error) {
i.init()
if i.Buf.Error != nil {
return nil, i.Buf.Error
}
// The actual index relative to the start of the page
idx = idx + i.J
if idx >= 0 {
pages := uint64(idx / bufferPageSize)
pageIndex := idx % bufferPageSize
curp := i.Elem
pval := curp.Value.(*bufferPage)
// If peeking past end of stream, return nils
if pval.I+pages > i.Buf.EndPage {
return nil, nil
}
// Move to the desired page
for j := uint64(0); j < pages; j++ {
nextp := curp.Next()
if nextp == nil {
i.Buf.nextPage()
if i.Buf.Error != nil {
return nil, i.Buf.Error
}
nextp = curp.Next()
// Check once more if we're peeking past end of stream,
// since we might have just reached end
if pval.I+pages-j > i.Buf.EndPage {
return nil, nil
}
}
curp = nextp
pval = curp.Value.(*bufferPage)
}
// Now return the datapoint at the desired index
return pval.DP[pageIndex], nil
}
// The index is negative, so we look *back*
pageIndex := bufferPageSize + idx%bufferPageSize
pages := uint64(1 - idx/bufferPageSize)
if pageIndex == 0 {
pages--
}
// See if we have the capability to look back this far
curp := i.Elem
curv := curp.Value.(*bufferPage)
if curv.I < pages || i.Buf.Pages.Front().Value.(*bufferPage).I > curv.I-pages {
if curv.I < pages {
return nil, ErrBeforeStart
}
return nil, fmt.Errorf("Peeking to relative index %d failed", idx-i.J)
}
// We can look back that far, so go directly to the page
for j := uint64(0); j < pages; j++ {
curp = curp.Prev()
}
curv = curp.Value.(*bufferPage)
return curv.DP[pageIndex], nil
}
func (i *BufferIterator) Close() {
for j, it := range i.Buf.Iterators {
if it == i {
i.Buf.Iterators[j] = i.Buf.Iterators[len(i.Buf.Iterators)-1]
i.Buf.Iterators = i.Buf.Iterators[:len(i.Buf.Iterators)-1]
break
}
}
}
type IteratorFromBI struct {
BI *BufferIterator
}
func (it IteratorFromBI) Next(out *Datapoint) (*Datapoint, error) {
dp, err := it.BI.Next()
if err != nil || dp == nil {
return dp, err
}
out.Timestamp = dp.Timestamp
out.Duration = dp.Duration
out.Data = dp.Data
return out, nil
}
type EmptyIterator struct{}
func (e EmptyIterator) Next(dp *Datapoint) (*Datapoint, error) {
return nil, nil
}