-
Notifications
You must be signed in to change notification settings - Fork 44
/
car.go
259 lines (214 loc) · 5.13 KB
/
car.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
package carv1
import (
"context"
"fmt"
"io"
"github.com/ipld/go-car/v2/internal/carv1/util"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
)
func init() {
cbor.RegisterCborType(CarHeader{})
}
type Store interface {
Put(context.Context, blocks.Block) error
}
type ReadStore interface {
Get(context.Context, cid.Cid) (blocks.Block, error)
}
type CarHeader struct {
Roots []cid.Cid
Version uint64
}
type carWriter struct {
ds format.NodeGetter
w io.Writer
}
func WriteCar(ctx context.Context, ds format.NodeGetter, roots []cid.Cid, w io.Writer) error {
h := &CarHeader{
Roots: roots,
Version: 1,
}
if err := WriteHeader(h, w); err != nil {
return fmt.Errorf("failed to write car header: %s", err)
}
cw := &carWriter{ds: ds, w: w}
seen := cid.NewSet()
for _, r := range roots {
if err := merkledag.Walk(ctx, cw.enumGetLinks, r, seen.Visit); err != nil {
return err
}
}
return nil
}
func ReadHeader(r io.Reader) (*CarHeader, error) {
hb, err := util.LdRead(r, false)
if err != nil {
return nil, err
}
var ch CarHeader
if err := cbor.DecodeInto(hb, &ch); err != nil {
return nil, fmt.Errorf("invalid header: %v", err)
}
return &ch, nil
}
func WriteHeader(h *CarHeader, w io.Writer) error {
hb, err := cbor.DumpObject(h)
if err != nil {
return err
}
return util.LdWrite(w, hb)
}
func HeaderSize(h *CarHeader) (uint64, error) {
hb, err := cbor.DumpObject(h)
if err != nil {
return 0, err
}
return util.LdSize(hb), nil
}
func (cw *carWriter) enumGetLinks(ctx context.Context, c cid.Cid) ([]*format.Link, error) {
nd, err := cw.ds.Get(ctx, c)
if err != nil {
return nil, err
}
if err := cw.writeNode(ctx, nd); err != nil {
return nil, err
}
return nd.Links(), nil
}
func (cw *carWriter) writeNode(ctx context.Context, nd format.Node) error {
return util.LdWrite(cw.w, nd.Cid().Bytes(), nd.RawData())
}
type CarReader struct {
r io.Reader
Header *CarHeader
zeroLenAsEOF bool
}
func NewCarReaderWithZeroLengthSectionAsEOF(r io.Reader) (*CarReader, error) {
return newCarReader(r, true)
}
func NewCarReader(r io.Reader) (*CarReader, error) {
return newCarReader(r, false)
}
func newCarReader(r io.Reader, zeroLenAsEOF bool) (*CarReader, error) {
ch, err := ReadHeader(r)
if err != nil {
return nil, err
}
if ch.Version != 1 {
return nil, fmt.Errorf("invalid car version: %d", ch.Version)
}
if len(ch.Roots) == 0 {
return nil, fmt.Errorf("empty car, no roots")
}
return &CarReader{
r: r,
Header: ch,
zeroLenAsEOF: zeroLenAsEOF,
}, nil
}
func (cr *CarReader) Next() (blocks.Block, error) {
c, data, err := util.ReadNode(cr.r, cr.zeroLenAsEOF)
if err != nil {
return nil, err
}
hashed, err := c.Prefix().Sum(data)
if err != nil {
return nil, err
}
if !hashed.Equals(c) {
return nil, fmt.Errorf("mismatch in content integrity, name: %s, data: %s", c, hashed)
}
return blocks.NewBlockWithCid(data, c)
}
type batchStore interface {
PutMany(context.Context, []blocks.Block) error
}
func LoadCar(s Store, r io.Reader) (*CarHeader, error) {
ctx := context.TODO()
cr, err := NewCarReader(r)
if err != nil {
return nil, err
}
if bs, ok := s.(batchStore); ok {
return loadCarFast(ctx, bs, cr)
}
return loadCarSlow(ctx, s, cr)
}
func loadCarFast(ctx context.Context, s batchStore, cr *CarReader) (*CarHeader, error) {
var buf []blocks.Block
for {
blk, err := cr.Next()
if err != nil {
if err == io.EOF {
if len(buf) > 0 {
if err := s.PutMany(ctx, buf); err != nil {
return nil, err
}
}
return cr.Header, nil
}
return nil, err
}
buf = append(buf, blk)
if len(buf) > 1000 {
if err := s.PutMany(ctx, buf); err != nil {
return nil, err
}
buf = buf[:0]
}
}
}
func loadCarSlow(ctx context.Context, s Store, cr *CarReader) (*CarHeader, error) {
for {
blk, err := cr.Next()
if err != nil {
if err == io.EOF {
return cr.Header, nil
}
return nil, err
}
if err := s.Put(ctx, blk); err != nil {
return nil, err
}
}
}
// Matches checks whether two headers match.
// Two headers are considered matching if:
// 1. They have the same version number, and
// 2. They contain the same root CIDs in any order.
// Note, this function explicitly ignores the order of roots.
// If order of roots matter use reflect.DeepEqual instead.
func (h CarHeader) Matches(other CarHeader) bool {
if h.Version != other.Version {
return false
}
thisLen := len(h.Roots)
if thisLen != len(other.Roots) {
return false
}
// Headers with a single root are popular.
// Implement a fast execution path for popular cases.
if thisLen == 1 {
return h.Roots[0].Equals(other.Roots[0])
}
// Check other contains all roots.
// TODO: should this be optimised for cases where the number of roots are large since it has O(N^2) complexity?
for _, r := range h.Roots {
if !other.containsRoot(r) {
return false
}
}
return true
}
func (h *CarHeader) containsRoot(root cid.Cid) bool {
for _, r := range h.Roots {
if r.Equals(root) {
return true
}
}
return false
}