-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrange.go
More file actions
398 lines (347 loc) · 9.93 KB
/
range.go
File metadata and controls
398 lines (347 loc) · 9.93 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
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
package bstream
import (
"errors"
"fmt"
"strconv"
"strings"
"go.uber.org/zap/zapcore"
)
var ErrOpenEndedRange = errors.New("open ended range")
// ParseRangeOption is the interface for options passed to ParseRange.
// Both RangeOptions (for configuring the Range) and parse configuration
// options (like WithDefaultStartBlock) implement this interface.
type ParseRangeOption interface {
parseRangeOption()
}
// parseConfig holds configuration for parsing ranges
type parseConfig struct {
defaultStartBlock *uint64
}
func (*parseConfig) parseRangeOption() {}
// WithDefaultStartBlock returns a parse option that sets the default start block
// used when the input has an empty or relative start value.
//
// Examples with WithDefaultStartBlock(5):
// - ":+100" → 5:105 (start defaults to 5, end is 5+100)
// - "+10:+100" → 15:115 (start is 5+10, end is 15+100)
func WithDefaultStartBlock(block uint64) *parseConfig {
return &parseConfig{defaultStartBlock: &block}
}
// ParseRange will parse a range with support for relative values.
//
// Supported formats:
// - "5:10" or "5-10" → explicit start and end
// - "5:+100" → explicit start, end is start + offset (5:105)
// - ":+100" + option → start from default, end is start + offset
// - "+10:+100" + option → start is default + offset, end is start + offset
//
// By default it will make an inclusive start & end, use RangeOptions to set exclusive boundaries.
//
// For inputs with empty or relative start values (e.g., ":+100" or "+10:+100"),
// you must provide WithDefaultStartBlock option, otherwise an error is returned.
func ParseRange(in string, opts ...ParseRangeOption) (*Range, error) {
if in == "" {
return nil, fmt.Errorf("input is required")
}
// Separate parse options from range options
var cfg parseConfig
var rangeOpts []RangeOptions
for _, opt := range opts {
switch o := opt.(type) {
case *parseConfig:
if o.defaultStartBlock != nil {
cfg.defaultStartBlock = o.defaultStartBlock
}
case RangeOptions:
rangeOpts = append(rangeOpts, o)
}
}
// Split by : or - but preserve the parts with + prefix
parts := splitRangeParts(in)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid range format: expected 'start:end' or 'start-end', got %q", in)
}
startPart := strings.TrimSpace(parts[0])
endPart := strings.TrimSpace(parts[1])
var startBlock uint64
var endBlock uint64
// Parse start block
if startPart == "" {
// Empty start, use default
if cfg.defaultStartBlock == nil {
return nil, fmt.Errorf("empty start block requires WithDefaultStartBlock option")
}
startBlock = *cfg.defaultStartBlock
} else if strings.HasPrefix(startPart, "+") {
// Relative start
if cfg.defaultStartBlock == nil {
return nil, fmt.Errorf("relative start block %q requires WithDefaultStartBlock option", startPart)
}
offset, err := parseBlockNumber(strings.TrimPrefix(startPart, "+"))
if err != nil {
return nil, fmt.Errorf("invalid start block offset: %w", err)
}
startBlock = *cfg.defaultStartBlock + offset
} else {
// Absolute start
val, err := parseBlockNumber(startPart)
if err != nil {
return nil, fmt.Errorf("invalid start block: %w", err)
}
startBlock = val
}
// Parse end block
if strings.HasPrefix(endPart, "+") {
// Relative end (relative to start)
offset, err := parseBlockNumber(strings.TrimPrefix(endPart, "+"))
if err != nil {
return nil, fmt.Errorf("invalid end block offset: %w", err)
}
endBlock = startBlock + offset
} else {
// Absolute end
val, err := parseBlockNumber(endPart)
if err != nil {
return nil, fmt.Errorf("invalid stop block: %w", err)
}
endBlock = val
}
r, err := newRange(startBlock, &endBlock, rangeOpts...)
if err != nil {
return nil, fmt.Errorf("making range: %w", err)
}
return r, nil
}
// splitRangeParts splits the input by : or - while preserving + prefixes
func splitRangeParts(in string) []string {
// Find the separator (: or -)
for i, r := range in {
if r == ':' || r == '-' {
// Check it's not within a number (e.g., not the minus in a negative number)
// For our purposes, : and - are always separators
return []string{in[:i], in[i+1:]}
}
}
return []string{in}
}
// parseBlockNumber parses a block number, stripping commas, underscores and spaces
func parseBlockNumber(s string) (uint64, error) {
s = strings.ReplaceAll(s, " ", "")
s = strings.ReplaceAll(s, ",", "")
s = strings.ReplaceAll(s, "_", "")
val, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, err
}
return val, nil
}
func MustParseRange(in string, opts ...ParseRangeOption) *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 (RangeOptions) parseRangeOption() {}
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 "[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
}