-
Notifications
You must be signed in to change notification settings - Fork 3
/
sector_iterator.go
77 lines (61 loc) · 1.3 KB
/
sector_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
package schemav2
import (
"errors"
"fmt"
)
type sectorIterator struct {
f *File
sectorsToFetch []string
sectorPtr *Sector
cachedSectors []Sector
err error
}
type Sector struct {
Valid bool
Data []byte
ID string
idx int
}
func (f *File) newSectorIterator(sectorPtr *Sector, sectors []string) *sectorIterator {
return §orIterator{
f: f,
sectorsToFetch: sectors,
sectorPtr: sectorPtr,
}
}
func (i *sectorIterator) Next() bool {
if i.err != nil {
return false
}
if len(i.cachedSectors) == 0 && len(i.sectorsToFetch) == 0 {
return false
}
if len(i.cachedSectors) == 0 {
maxSectors := 100
if len(i.sectorsToFetch) < maxSectors {
maxSectors = len(i.sectorsToFetch)
}
sectorIDs := i.sectorsToFetch[:maxSectors]
i.sectorsToFetch = i.sectorsToFetch[maxSectors:]
sectors, err := i.f.getSectors(sectorIDs)
if err != nil {
i.err = err
return false
}
i.cachedSectors = sectors
}
if !i.cachedSectors[0].Valid {
i.err = fmt.Errorf("sector %s was not found", i.cachedSectors[0].ID)
return false
}
*i.sectorPtr = i.cachedSectors[0]
i.cachedSectors = i.cachedSectors[1:]
return true
}
func (i *sectorIterator) Close() error {
if i.err != nil {
return i.err
}
i.err = errors.New("iter closed")
return nil
}