Skip to content

Commit

Permalink
dag: add fsNodeType in NewLeafNode and NewLeafDataNode
Browse files Browse the repository at this point in the history
NewLeafNode and NewLeafDataNode is introduced in commit 474b77a2bdb1c
("importer: remove `UnixfsNode` from the balanced builder"). It is
intended to return ipfs.Node instead of UnixfsNode. But it only
support creating the TFile leaf node for merkledag.

This commit add fsNodeType to above two functions and update the code
in dagbuild.go. Further patches of trickledag will make use of them
and pass TRaw to create leaf node.

License: MIT
Signed-off-by: Bamvor Zhang <jian.zhang@ipfsbit.com>
  • Loading branch information
bjzhang committed Aug 15, 2018
1 parent e86ca81 commit 85897b3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions importer/balanced/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ import (
func Layout(db *h.DagBuilderHelper) (ipld.Node, error) {
if db.Done() {
// No data, return just an empty node.
root, err := db.NewLeafNode(nil)
root, err := db.NewLeafNode(nil, ft.TFile)
if err != nil {
return nil, err
}
Expand All @@ -137,7 +137,7 @@ func Layout(db *h.DagBuilderHelper) (ipld.Node, error) {
// (corner case), after that subsequent `root` nodes will
// always be internal nodes (with a depth > 0) that can
// be handled by the loop.
root, fileSize, err := db.NewLeafDataNode()
root, fileSize, err := db.NewLeafDataNode(ft.TFile)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -224,7 +224,7 @@ func fillNodeRec(db *h.DagBuilderHelper, node *h.FSNodeOverDag, depth int) (fill

if depth == 1 {
// Base case: add leaf node with data.
childNode, childFileSize, err = db.NewLeafDataNode()
childNode, childFileSize, err = db.NewLeafDataNode(ft.TFile)
if err != nil {
return nil, 0, err
}
Expand Down
8 changes: 4 additions & 4 deletions importer/helpers/dagbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (db *DagBuilderHelper) NewLeaf(data []byte) (*UnixfsNode, error) {

// NewLeafNode is a variation from `NewLeaf` (see its description) that
// returns an `ipld.Node` instead.
func (db *DagBuilderHelper) NewLeafNode(data []byte) (ipld.Node, error) {
func (db *DagBuilderHelper) NewLeafNode(data []byte, fsNodeType pb.Data_DataType) (ipld.Node, error) {
if len(data) > BlockSizeLimit {
return nil, ErrSizeLimitExceeded
}
Expand All @@ -205,7 +205,7 @@ func (db *DagBuilderHelper) NewLeafNode(data []byte) (ipld.Node, error) {
}

// Encapsulate the data in UnixFS node (instead of a raw node).
fsNodeOverDag := db.NewFSNodeOverDag(ft.TFile)
fsNodeOverDag := db.NewFSNodeOverDag(fsNodeType)
fsNodeOverDag.SetFileData(data)
node, err := fsNodeOverDag.Commit()
if err != nil {
Expand Down Expand Up @@ -274,15 +274,15 @@ func (db *DagBuilderHelper) GetNextDataNode() (*UnixfsNode, error) {
// used to keep track of the DAG file size). The size of the data is
// computed here because after that it will be hidden by `NewLeafNode`
// inside a generic `ipld.Node` representation.
func (db *DagBuilderHelper) NewLeafDataNode() (node ipld.Node, dataSize uint64, err error) {
func (db *DagBuilderHelper) NewLeafDataNode(fsNodeType pb.Data_DataType) (node ipld.Node, dataSize uint64, err error) {
fileData, err := db.Next()
if err != nil {
return nil, 0, err
}
dataSize = uint64(len(fileData))

// Create a new leaf node containing the file chunk data.
node, err = db.NewLeafNode(fileData)
node, err = db.NewLeafNode(fileData, fsNodeType)
if err != nil {
return nil, 0, err
}
Expand Down

0 comments on commit 85897b3

Please sign in to comment.