This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathtrickledag.go
397 lines (341 loc) · 10.6 KB
/
trickledag.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
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
// Package trickle allows to build trickle DAGs.
// In this type of DAG, non-leave nodes are first filled
// with data leaves, and then incorporate "layers" of subtrees
// as additional links.
//
// Each layer is a trickle sub-tree and is limited by an increasing
// maximum depth. Thus, the nodes first layer
// can only hold leaves (depth 1) but subsequent layers can grow deeper.
// By default, this module places 4 nodes per layer (that is, 4 subtrees
// of the same maximum depth before increasing it).
//
// Trickle DAGs are very good for sequentially reading data, as the
// first data leaves are directly reachable from the root and those
// coming next are always nearby. They are
// suited for things like streaming applications.
package trickle
import (
"context"
"errors"
"fmt"
ft "github.com/ipfs/go-unixfs"
h "github.com/ipfs/go-unixfs/importer/helpers"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
dag "github.com/ipfs/go-merkledag"
)
// depthRepeat specifies how many times to append a child tree of a
// given depth. Higher values increase the width of a given node, which
// improves seek speeds.
const depthRepeat = 4
// Layout builds a new DAG with the trickle format using the provided
// DagBuilderHelper. See the module's description for a more detailed
// explanation.
//
// Deprecated: use github.com/ipfs/boxo/ipld/unixfs/importer/trickle.Layout
func Layout(db *h.DagBuilderHelper) (ipld.Node, error) {
newRoot := db.NewFSNodeOverDag(ft.TFile)
root, _, err := fillTrickleRec(db, newRoot, -1)
if err != nil {
return nil, err
}
return root, db.Add(root)
}
// fillTrickleRec creates a trickle (sub-)tree with an optional maximum specified depth
// in the case maxDepth is greater than zero, or with unlimited depth otherwise
// (where the DAG builder will signal the end of data to end the function).
func fillTrickleRec(db *h.DagBuilderHelper, node *h.FSNodeOverDag, maxDepth int) (filledNode ipld.Node, nodeFileSize uint64, err error) {
// Always do this, even in the base case
if err := db.FillNodeLayer(node); err != nil {
return nil, 0, err
}
// For each depth in [1, `maxDepth`) (or without limit if `maxDepth` is -1,
// initial call from `Layout`) add `depthRepeat` sub-graphs of that depth.
for depth := 1; maxDepth == -1 || depth < maxDepth; depth++ {
if db.Done() {
break
// No more data, stop here, posterior append calls will figure out
// where we left off.
}
for repeatIndex := 0; repeatIndex < depthRepeat && !db.Done(); repeatIndex++ {
childNode, childFileSize, err := fillTrickleRec(db, db.NewFSNodeOverDag(ft.TFile), depth)
if err != nil {
return nil, 0, err
}
if err := node.AddChild(childNode, childFileSize, db); err != nil {
return nil, 0, err
}
}
}
// Get the final `dag.ProtoNode` with the `FSNode` data encoded inside.
filledNode, err = node.Commit()
if err != nil {
return nil, 0, err
}
return filledNode, node.FileSize(), nil
}
// Append appends the data in `db` to the dag, using the Trickledag format
//
// Deprecated: use github.com/ipfs/boxo/ipld/unixfs/importer/trickle.Append
func Append(ctx context.Context, basen ipld.Node, db *h.DagBuilderHelper) (out ipld.Node, errOut error) {
base, ok := basen.(*dag.ProtoNode)
if !ok {
return nil, dag.ErrNotProtobuf
}
// Convert to unixfs node for working with easily
fsn, err := h.NewFSNFromDag(base)
if err != nil {
return nil, err
}
// Get depth of this 'tree'
depth, repeatNumber := trickleDepthInfo(fsn, db.Maxlinks())
if depth == 0 {
// If direct blocks not filled...
if err := db.FillNodeLayer(fsn); err != nil {
return nil, err
}
if db.Done() {
// TODO: If `FillNodeLayer` stop `Commit`ing this should be
// the place (besides the function end) to call it.
return fsn.GetDagNode()
}
// If continuing, our depth has increased by one
depth++
}
// Last child in this node may not be a full tree, lets fill it up.
if err := appendFillLastChild(ctx, fsn, depth-1, repeatNumber, db); err != nil {
return nil, err
}
// after appendFillLastChild, our depth is now increased by one
if !db.Done() {
depth++
}
// Now, continue filling out tree like normal
for i := depth; !db.Done(); i++ {
for j := 0; j < depthRepeat && !db.Done(); j++ {
nextChild := db.NewFSNodeOverDag(ft.TFile)
childNode, childFileSize, err := fillTrickleRec(db, nextChild, i)
if err != nil {
return nil, err
}
err = fsn.AddChild(childNode, childFileSize, db)
if err != nil {
return nil, err
}
}
}
_, err = fsn.Commit()
if err != nil {
return nil, err
}
return fsn.GetDagNode()
}
func appendFillLastChild(ctx context.Context, fsn *h.FSNodeOverDag, depth int, repeatNumber int, db *h.DagBuilderHelper) error {
if fsn.NumChildren() <= db.Maxlinks() {
return nil
}
// TODO: Why do we need this check, didn't the caller already take
// care of this?
// Recursive step, grab last child
last := fsn.NumChildren() - 1
lastChild, err := fsn.GetChild(ctx, last, db.GetDagServ())
if err != nil {
return err
}
// Fill out last child (may not be full tree)
newChild, nchildSize, err := appendRec(ctx, lastChild, db, depth-1)
if err != nil {
return err
}
// Update changed child in parent node
fsn.RemoveChild(last, db)
filledNode, err := newChild.Commit()
if err != nil {
return err
}
err = fsn.AddChild(filledNode, nchildSize, db)
if err != nil {
return err
}
// Partially filled depth layer
if repeatNumber != 0 {
for ; repeatNumber < depthRepeat && !db.Done(); repeatNumber++ {
nextChild := db.NewFSNodeOverDag(ft.TFile)
childNode, childFileSize, err := fillTrickleRec(db, nextChild, depth)
if err != nil {
return err
}
if err := fsn.AddChild(childNode, childFileSize, db); err != nil {
return err
}
}
}
return nil
}
// recursive call for Append
func appendRec(ctx context.Context, fsn *h.FSNodeOverDag, db *h.DagBuilderHelper, maxDepth int) (*h.FSNodeOverDag, uint64, error) {
if maxDepth == 0 || db.Done() {
return fsn, fsn.FileSize(), nil
}
// Get depth of this 'tree'
depth, repeatNumber := trickleDepthInfo(fsn, db.Maxlinks())
if depth == 0 {
// If direct blocks not filled...
if err := db.FillNodeLayer(fsn); err != nil {
return nil, 0, err
}
depth++
}
// TODO: Same as `appendFillLastChild`, when is this case possible?
// If at correct depth, no need to continue
if depth == maxDepth {
return fsn, fsn.FileSize(), nil
}
if err := appendFillLastChild(ctx, fsn, depth, repeatNumber, db); err != nil {
return nil, 0, err
}
// after appendFillLastChild, our depth is now increased by one
if !db.Done() {
depth++
}
// Now, continue filling out tree like normal
for i := depth; i < maxDepth && !db.Done(); i++ {
for j := 0; j < depthRepeat && !db.Done(); j++ {
nextChild := db.NewFSNodeOverDag(ft.TFile)
childNode, childFileSize, err := fillTrickleRec(db, nextChild, i)
if err != nil {
return nil, 0, err
}
if err := fsn.AddChild(childNode, childFileSize, db); err != nil {
return nil, 0, err
}
}
}
return fsn, fsn.FileSize(), nil
}
// Deduce where we left off in `fillTrickleRec`, returns the `depth`
// with which new sub-graphs were being added and, within that depth,
// in which `repeatNumber` of the total `depthRepeat` we should add.
func trickleDepthInfo(node *h.FSNodeOverDag, maxlinks int) (depth int, repeatNumber int) {
n := node.NumChildren()
if n < maxlinks {
// We didn't even added the initial `maxlinks` leaf nodes (`FillNodeLayer`).
return 0, 0
}
nonLeafChildren := n - maxlinks
// The number of non-leaf child nodes added in `fillTrickleRec` (after
// the `FillNodeLayer` call).
depth = nonLeafChildren/depthRepeat + 1
// "Deduplicate" the added `depthRepeat` sub-graphs at each depth
// (rounding it up since we may be on an unfinished depth with less
// than `depthRepeat` sub-graphs).
repeatNumber = nonLeafChildren % depthRepeat
// What's left after taking full depths of `depthRepeat` sub-graphs
// is the current `repeatNumber` we're at (this fractional part is
// what we rounded up before).
return
}
// VerifyParams is used by VerifyTrickleDagStructure
//
// Deprecated: use github.com/ipfs/boxo/ipld/unixfs/importer/trickle.VerifyParams
type VerifyParams struct {
Getter ipld.NodeGetter
Direct int
LayerRepeat int
Prefix *cid.Prefix
RawLeaves bool
}
// VerifyTrickleDagStructure checks that the given dag matches exactly the trickle dag datastructure
// layout
//
// Deprecated: use github.com/ipfs/boxo/ipld/unixfs/importer/trickle.VerifyTrickleDagStructure
func VerifyTrickleDagStructure(nd ipld.Node, p VerifyParams) error {
return verifyTDagRec(nd, -1, p)
}
// Recursive call for verifying the structure of a trickledag
func verifyTDagRec(n ipld.Node, depth int, p VerifyParams) error {
codec := cid.DagProtobuf
if depth == 0 {
if len(n.Links()) > 0 {
return errors.New("expected direct block")
}
// zero depth dag is raw data block
switch nd := n.(type) {
case *dag.ProtoNode:
fsn, err := ft.FSNodeFromBytes(nd.Data())
if err != nil {
return err
}
if fsn.Type() != ft.TRaw {
return errors.New("expected raw block")
}
if p.RawLeaves {
return errors.New("expected raw leaf, got a protobuf node")
}
case *dag.RawNode:
if !p.RawLeaves {
return errors.New("expected protobuf node as leaf")
}
codec = cid.Raw
default:
return errors.New("expected ProtoNode or RawNode")
}
}
// verify prefix
if p.Prefix != nil {
prefix := n.Cid().Prefix()
expect := *p.Prefix // make a copy
expect.Codec = uint64(codec)
if codec == cid.Raw && expect.Version == 0 {
expect.Version = 1
}
if expect.MhLength == -1 {
expect.MhLength = prefix.MhLength
}
if prefix != expect {
return fmt.Errorf("unexpected cid prefix: expected: %v; got %v", expect, prefix)
}
}
if depth == 0 {
return nil
}
nd, ok := n.(*dag.ProtoNode)
if !ok {
return errors.New("expected ProtoNode")
}
// Verify this is a branch node
fsn, err := ft.FSNodeFromBytes(nd.Data())
if err != nil {
return err
}
if fsn.Type() != ft.TFile {
return fmt.Errorf("expected file as branch node, got: %s", fsn.Type())
}
if len(fsn.Data()) > 0 {
return errors.New("branch node should not have data")
}
for i := 0; i < len(nd.Links()); i++ {
child, err := nd.Links()[i].GetNode(context.TODO(), p.Getter)
if err != nil {
return err
}
if i < p.Direct {
// Direct blocks
err := verifyTDagRec(child, 0, p)
if err != nil {
return err
}
} else {
// Recursive trickle dags
rdepth := ((i - p.Direct) / p.LayerRepeat) + 1
if rdepth >= depth && depth > 0 {
return errors.New("child dag was too deep")
}
err := verifyTDagRec(child, rdepth, p)
if err != nil {
return err
}
}
}
return nil
}