-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathblock.go
246 lines (217 loc) · 5.39 KB
/
block.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
package accum
import (
"context"
"errors"
"io"
"sync"
"github.com/filecoin-project/go-leb128"
"github.com/ipfs/go-cid"
"github.com/rpcpool/yellowstone-faithful/carreader"
"github.com/rpcpool/yellowstone-faithful/iplddecoders"
)
type ObjectAccumulator struct {
skipNodes uint64
flushOnKind iplddecoders.Kind
reader *carreader.CarReader
ignoreKinds iplddecoders.KindSlice
callback func(*ObjectWithMetadata, []ObjectWithMetadata) error
flushWg sync.WaitGroup
flushQueue chan *flushBuffer
}
var ErrStop = errors.New("stop")
func isStop(err error) bool {
return errors.Is(err, ErrStop)
}
func NewObjectAccumulator(
reader *carreader.CarReader,
flushOnKind iplddecoders.Kind,
callback func(*ObjectWithMetadata, []ObjectWithMetadata) error,
ignoreKinds ...iplddecoders.Kind,
) *ObjectAccumulator {
return &ObjectAccumulator{
reader: reader,
ignoreKinds: ignoreKinds,
flushOnKind: flushOnKind,
callback: callback,
flushQueue: make(chan *flushBuffer, 1000),
}
}
// SetSkip(n)
func (oa *ObjectAccumulator) SetSkip(n uint64) {
oa.skipNodes = n
}
var flushBufferPool = sync.Pool{
New: func() interface{} {
return &flushBuffer{}
},
}
func getFlushBuffer() *flushBuffer {
return flushBufferPool.Get().(*flushBuffer)
}
func putFlushBuffer(fb *flushBuffer) {
fb.Reset()
flushBufferPool.Put(fb)
}
type flushBuffer struct {
parent *ObjectWithMetadata
children []ObjectWithMetadata
}
// Reset resets the flushBuffer.
func (fb *flushBuffer) Reset() {
fb.parent = nil
fb.children = fb.children[:0]
}
type ObjectWithMetadata struct {
Cid cid.Cid
Offset uint64
SectionLength uint64
ObjectData []byte
}
func (oa *ObjectAccumulator) startFlusher(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case fb := <-oa.flushQueue:
if fb == nil {
return
}
if err := oa.flush(fb.parent, fb.children); err != nil {
if isStop(err) {
return
}
panic(err)
}
oa.flushWg.Done()
putFlushBuffer(fb)
}
}
}
func (oa *ObjectAccumulator) sendToFlusher(head *ObjectWithMetadata, other []ObjectWithMetadata) {
oa.flushWg.Add(1)
fb := getFlushBuffer()
fb.parent = head
fb.children = other
oa.flushQueue <- fb
}
func (oa *ObjectAccumulator) Run(ctx context.Context) error {
go oa.startFlusher(ctx)
defer func() {
oa.flushWg.Wait()
close(oa.flushQueue)
}()
totalOffset := uint64(0)
{
if size, err := oa.reader.HeaderSize(); err != nil {
return err
} else {
totalOffset += size
}
}
numSkipped := uint64(0)
objectCap := 5000
buffersLoop:
for {
children := make([]ObjectWithMetadata, 0, objectCap)
currentBufferLoop:
for {
if ctx.Err() != nil {
return ctx.Err()
}
cid_, sectionLength, data, err := oa.reader.NextNodeBytes()
if err != nil {
if errors.Is(err, io.EOF) {
oa.sendToFlusher(nil, children)
break buffersLoop
}
return err
}
currentOffset := totalOffset
totalOffset += sectionLength
if numSkipped < oa.skipNodes {
numSkipped++
continue
}
if data == nil {
oa.sendToFlusher(nil, children)
break buffersLoop
}
element := ObjectWithMetadata{
Cid: cid_,
Offset: currentOffset,
SectionLength: sectionLength,
ObjectData: data,
}
kind := iplddecoders.Kind(data[1])
if kind == oa.flushOnKind {
// element is parent
oa.sendToFlusher(&element, children)
break currentBufferLoop
} else {
if len(oa.ignoreKinds) > 0 && oa.ignoreKinds.Has(kind) {
continue
}
children = append(children, element)
}
}
}
return nil
}
func (oa *ObjectAccumulator) flush(head *ObjectWithMetadata, other []ObjectWithMetadata) error {
if head == nil && len(other) == 0 {
return nil
}
return oa.callback(head, other)
}
// RawSection returns the CAR object as it would be written to a CAR file.
func (obj ObjectWithMetadata) RawSection() ([]byte, error) {
buf := make([]byte, 0)
// section is an encoded CAR object
// length = len(cid) + len(data)
// section = leb128(length) || cid || data
sectionLen := len(obj.Cid.Bytes()) + len(obj.ObjectData)
// write uvarint length of the section
buf = append(buf, leb128.FromUInt64(uint64(sectionLen))...)
// write cid
buf = append(buf, obj.Cid.Bytes()...)
// write data
buf = append(buf, obj.ObjectData...)
return buf, nil
}
func (obj ObjectWithMetadata) RawSectionSize() int {
sectionLen := len(obj.Cid.Bytes()) + len(obj.ObjectData)
lenBytes := leb128.FromUInt64(uint64(sectionLen))
// Size is:
// length of LEB128-encoded section length +
// length of CID bytes +
// length of object data
return len(lenBytes) + sectionLen
}
// {
// raw, err := objm.RawSection()
// if err != nil {
// panic(err)
// }
// rawLen := (len(raw))
// if rawLen != int(sectionLength) {
// panic(fmt.Sprintf("section length mismatch: got %d, expected %d", rawLen, sectionLength))
// }
// _c, _sectionLen, _data, err := carreader.ReadNodeInfoWithData(bufio.NewReader(bytes.NewReader(raw)))
// if err != nil {
// panic(err)
// }
// if _c != c {
// panic(fmt.Sprintf("cid mismatch: got %s, expected %s", _c, c))
// }
// if _sectionLen != sectionLength {
// panic(fmt.Sprintf("section length mismatch: got %d, expected %d", _sectionLen, sectionLength))
// }
// if !bytes.Equal(_data, data) {
// panic(fmt.Sprintf("data mismatch: got %x, expected %x", _data, data))
// }
// }
func clone[T any](s []T) []T {
v := make([]T, len(s))
copy(v, s)
return v
}