dag: refactor trickle importer
License: MIT Signed-off-by: Lucas Molas <schomatis@gmail.com>
Lucas Molas committed
Feb 22, 2018 at 20:51 UTC
9eb864a1d34d0a6bb526bb76602dde09b5a7a518
1 file changed
+18
-19
importer/trickle/trickledag.go
+18
-19
@@ -38,20 +38,9 @@ const layerRepeat = 4
38
// explanation.
39
func Layout(db *h.DagBuilderHelper) (ipld.Node, error) {
40
root := db.NewUnixfsNode()
41
- if err := db.FillNodeLayer(root); err != nil {
41
+ if err := fillTrickleRec(db, root, -1); err != nil {
42
return nil, err
43
}
44
- for level := 1; !db.Done(); level++ {
45
- for i := 0; i < layerRepeat && !db.Done(); i++ {
46
- next := db.NewUnixfsNode()
47
- if err := fillTrickleRec(db, next, level); err != nil {
48
- return nil, err
49
- }
50
- if err := root.AddChild(next, db); err != nil {
51
- return nil, err
52
- }
53
- }
54
- }
44
45
out, err := db.Add(root)
46
if err != nil {
@@ -65,25 +54,35 @@ func Layout(db *h.DagBuilderHelper) (ipld.Node, error) {
54
return out, nil
55
}
56
68
-func fillTrickleRec(db *h.DagBuilderHelper, node *h.UnixfsNode, depth int) error {
57
+// fillTrickleRec creates a trickle (sub-)tree with an optional maximum specified depth
58
+// in the case maxDepth is greater than zero, or with unlimited depth otherwise
59
+// (where the DAG builder will signal the end of data to end the function).
60
+func fillTrickleRec(db *h.DagBuilderHelper, node *h.UnixfsNode, maxDepth int) error {
61
// Always do this, even in the base case
62
if err := db.FillNodeLayer(node); err != nil {
63
return err
64
}
65
74
- for i := 1; i < depth && !db.Done(); i++ {
75
- for j := 0; j < layerRepeat && !db.Done(); j++ {
76
- next := db.NewUnixfsNode()
77
- if err := fillTrickleRec(db, next, i); err != nil {
66
+ for depth := 1; ; depth++ {
67
+ // Apply depth limit only if the parameter is set (> 0).
68
+ if maxDepth > 0 && depth == maxDepth {
69
+ return nil
70
+ }
71
+ for layer := 0; layer < layerRepeat; layer++ {
72
+ if db.Done() {
73
+ return nil
74
+ }
75
+
76
+ nextChild := db.NewUnixfsNode()
77
+ if err := fillTrickleRec(db, nextChild, depth); err != nil {
78
return err
79
}
80
81
- if err := node.AddChild(next, db); err != nil {
81
+ if err := node.AddChild(nextChild, db); err != nil {
82
return err
83
}
84
}
85
}
86
- return nil
86
}
87
88
// Append appends the data in `db` to the dag, using the Trickledag format