Commit 6aa0d7fd authored by Bamvor Zhang's avatar Bamvor Zhang Committed by Lucas Molas

dag: remove `UnixfsNode` in Layout of trickledag

This patch is the part of trickledag work which is similar to the
merkledag work in commit 474b77a2bdb1c ("importer: remove `UnixfsNode`
from the balanced builder"). Two helper functions(fillTrickleRecFSNode
and FillFSNodeLayer) is introduced temporarily for modifing the Layout
functions. These two funtions will be removed when all the code of
UnixfsNode is removed in trickledag.go.

Test ipfs add and get commands to check whether get the same hash of
file after the code changes.

License: MIT
Signed-off-by: default avatarBamvor Zhang <jian.zhang@ipfsbit.com>
parent b56bc955
......@@ -246,6 +246,25 @@ func (db *DagBuilderHelper) FillNodeLayer(node *UnixfsNode) error {
return nil
}
// FillFSNodeLayer do the same thing as FillNodeLayer.
func (db *DagBuilderHelper) FillFSNodeLayer(node *FSNodeOverDag) error {
// while we have room AND we're not done
for node.NumChildren() < db.maxlinks && !db.Done() {
child, childFileSize, err := db.NewLeafDataNode(ft.TRaw)
if err != nil {
return err
}
if err := node.AddChild(child, childFileSize, db); err != nil {
return err
}
}
node.Commit()
return nil
}
// GetNextDataNode builds a UnixFsNode with the data obtained from the
// Splitter, given the constraints (BlockSizeLimit, RawLeaves) specified
// when creating the DagBuilderHelper.
......
......@@ -37,12 +37,13 @@ const layerRepeat = 4
// DagBuilderHelper. See the module's description for a more detailed
// explanation.
func Layout(db *h.DagBuilderHelper) (ipld.Node, error) {
root := db.NewUnixfsNode()
if err := fillTrickleRec(db, root, -1); err != nil {
newRoot := db.NewFSNodeOverDag(ft.TFile)
root, _, err := fillTrickleRecFSNode(db, newRoot, -1)
if err != nil {
return nil, err
}
return db.AddUnixfsNode(root)
return root, db.Add(root)
}
// fillTrickleRec creates a trickle (sub-)tree with an optional maximum specified depth
......@@ -76,6 +77,47 @@ func fillTrickleRec(db *h.DagBuilderHelper, node *h.UnixfsNode, maxDepth int) er
}
}
// fillTrickleRecFSNode 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 fillTrickleRecFSNode(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.FillFSNodeLayer(node); err != nil {
return nil, 0, err
}
for depth := 1; ; depth++ {
// Apply depth limit only if the parameter is set (> 0).
if db.Done() || (maxDepth > 0 && depth == maxDepth) {
break
}
for layer := 0; layer < layerRepeat; layer++ {
if db.Done() {
break
}
nextChild := db.NewFSNodeOverDag(ft.TFile)
childNode, childFileSize, err := fillTrickleRecFSNode(db, nextChild, depth)
if err != nil {
return nil, 0, err
}
if err := node.AddChild(childNode, childFileSize, db); err != nil {
return nil, 0, err
}
}
}
nodeFileSize = node.FileSize()
// Get the final `dag.ProtoNode` with the `FSNode` data encoded inside.
filledNode, err = node.Commit()
if err != nil {
return nil, 0, err
}
return filledNode, nodeFileSize, nil
}
// Append appends the data in `db` to the dag, using the Trickledag format
func Append(ctx context.Context, basen ipld.Node, db *h.DagBuilderHelper) (out ipld.Node, errOut error) {
base, ok := basen.(*dag.ProtoNode)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment