forked from streamingfast/bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range.go
288 lines (249 loc) · 6.46 KB
/
range.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
package bstream
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"go.uber.org/zap/zapcore"
)
var ErrOpenEndedRange = errors.New("open ended range")
// ParseRange will parse a range of format 5-10, by default it will make an inclusive start & end
// use options to set exclusive boundaries
func ParseRange(in string, opts ...RangeOptions) (*Range, error) {
if in == "" {
return nil, fmt.Errorf("input is required")
}
ch := strings.FieldsFunc(in, splitBy)
for i, bound := range ch {
bound = strings.ReplaceAll(bound, " ", "")
bound = regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(bound, "")
ch[i] = bound
}
lo, err := strconv.ParseInt(ch[0], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid start block: %w", err)
}
hi, err := strconv.ParseInt(ch[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid stop block: %w", err)
}
v := uint64(hi)
r, err := newRange(uint64(lo), &v, opts...)
if err != nil {
return nil, fmt.Errorf("making range: %w", err)
}
return r, nil
}
func splitBy(r rune) bool {
return r == ':' || r == '-'
}
func MustParseRange(in string, opts ...RangeOptions) *Range {
r, err := ParseRange(in, opts...)
if err != nil {
panic(err)
}
return r
}
func NewRangeContaining(blockNum uint64, size uint64) (*Range, error) {
if size == 0 {
return nil, fmt.Errorf("range needs a size")
}
start := blockNum - (blockNum % size)
return NewInclusiveRange(start, start+size), nil
}
type Range struct {
startBlock uint64
endBlock *uint64
exclusiveStartBlock bool
exclusiveEndBlock bool
}
type RangeOptions func(p *Range) *Range
func WithExclusiveEnd() RangeOptions {
return func(p *Range) *Range {
p.exclusiveEndBlock = true
return p
}
}
func WithExclusiveStart() RangeOptions {
return func(p *Range) *Range {
p.exclusiveStartBlock = true
return p
}
}
func NewOpenRange(startBlock uint64) *Range {
return mustNewRange(startBlock, nil, WithExclusiveEnd())
}
func NewRangeExcludingEnd(startBlock, endBlock uint64) *Range {
return mustNewRange(startBlock, &endBlock, WithExclusiveEnd())
}
func NewInclusiveRange(startBlock, endBlock uint64) *Range {
return mustNewRange(startBlock, &endBlock)
}
// mustNewRange return a new range, by default it will make an inclusive start & end
// use options to set exclusive boundaries
func mustNewRange(startBlock uint64, endBlock *uint64, opts ...RangeOptions) *Range {
r, err := newRange(startBlock, endBlock, opts...)
if err != nil {
panic(err)
}
return r
}
// newRange return a new range, by default it will make an inclusive start & end
// use options to set exclusive boundaries
func newRange(startBlock uint64, endBlock *uint64, opts ...RangeOptions) (*Range, error) {
if endBlock != nil && *endBlock <= startBlock {
return nil, fmt.Errorf("invalid block range start %d, end %d", startBlock, *endBlock)
}
r := &Range{startBlock, endBlock, false, false}
for _, opt := range opts {
r = opt(r)
}
return r, nil
}
func (r *Range) StartBlock() uint64 { return r.startBlock }
func (r *Range) EndBlock() *uint64 { return r.endBlock }
func (r *Range) String() string {
if r == nil {
return fmt.Sprintf("[nil]")
}
startBlockDeli := "["
if r.exclusiveStartBlock {
startBlockDeli = "("
}
if r.endBlock == nil {
return fmt.Sprintf("%s%d, nil]", startBlockDeli, r.startBlock)
}
endBlockDeli := "]"
if r.exclusiveEndBlock {
endBlockDeli = ")"
}
return fmt.Sprintf("%s%d, %d%s", startBlockDeli, r.startBlock, *r.endBlock, endBlockDeli)
}
func (r *Range) MarshalLogObject(enc zapcore.ObjectEncoder) error {
if r == nil {
enc.AddBool("nil", true)
} else {
if r.exclusiveStartBlock {
enc.AddUint64("exclusive_start_block", r.startBlock)
} else {
enc.AddUint64("start_block", r.startBlock)
}
if r.endBlock == nil {
enc.AddString("end_block", "None")
} else {
if r.exclusiveEndBlock {
enc.AddUint64("exclusive_end_block", *r.endBlock)
} else {
enc.AddUint64("end_block", *r.endBlock)
}
}
}
return nil
}
func (r *Range) Contains(blockNum uint64) bool {
if blockNum < r.startBlock {
return false
}
if r.exclusiveStartBlock && blockNum == r.startBlock {
return false
}
if r.endBlock == nil {
return true
}
endBlock := *r.endBlock
if blockNum > endBlock {
return false
}
if r.exclusiveEndBlock && blockNum == endBlock {
return false
}
return true
}
// block Number = 5
func (r *Range) ReachedEndBlock(blockNum uint64) bool {
if r.endBlock == nil {
return false
}
endBlock := *r.endBlock
if blockNum >= endBlock {
return true
}
if r.exclusiveEndBlock && blockNum == (endBlock-1) {
return true
}
return false
}
func (r *Range) Next(size uint64) *Range {
nextRange := &Range{
exclusiveEndBlock: r.exclusiveEndBlock,
exclusiveStartBlock: r.exclusiveStartBlock,
}
if r.endBlock == nil {
nextRange.startBlock = r.startBlock + size
return nextRange
}
nextRange.startBlock = *r.endBlock
endBlock := (*r.endBlock + size)
nextRange.endBlock = &endBlock
return nextRange
}
func (r *Range) Previous(size uint64) *Range {
prevRange := &Range{
startBlock: r.startBlock - size,
exclusiveEndBlock: r.exclusiveEndBlock,
exclusiveStartBlock: r.exclusiveStartBlock,
}
if r.endBlock == nil {
return prevRange
}
prevRange.endBlock = &r.startBlock
return prevRange
}
func (r *Range) IsNext(next *Range, size uint64) bool {
return r.Next(size).Equals(next)
}
func (r *Range) Equals(other *Range) bool {
return r.startBlock == other.startBlock &&
r.endBlock == other.endBlock &&
r.exclusiveStartBlock == other.exclusiveStartBlock &&
r.exclusiveEndBlock == other.exclusiveEndBlock
}
func (r *Range) Size() (uint64, error) {
if r.endBlock == nil {
return 0, ErrOpenEndedRange
}
return *r.endBlock - r.startBlock, nil
}
func (r *Range) Split(chunkSize uint64) ([]*Range, error) {
if r.endBlock == nil {
return nil, ErrOpenEndedRange
}
endBlock := *r.endBlock
if endBlock-r.startBlock <= chunkSize {
return []*Range{r}, nil
}
var res []*Range
currentEnd := (r.startBlock + chunkSize) - (r.startBlock+chunkSize)%chunkSize
currentStart := r.startBlock
for {
res = append(res, &Range{
startBlock: currentStart,
endBlock: ptr(currentEnd),
exclusiveStartBlock: r.exclusiveStartBlock,
exclusiveEndBlock: r.exclusiveEndBlock,
})
if currentEnd >= endBlock {
break
}
currentStart = currentEnd
currentEnd = currentStart + chunkSize
if currentEnd > endBlock {
currentEnd = endBlock
}
}
return res, nil
}
func ptr(v uint64) *uint64 {
return &v
}