Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Finish basic support for raw nodes in dag modifier.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Jun 26, 2017
1 parent 982e553 commit ec9e96f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
34 changes: 18 additions & 16 deletions mod/dagmodifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,25 @@ func (dm *DagModifier) Write(b []byte) (int, error) {
var ErrNoRawYet = fmt.Errorf("currently only fully support protonodes in the dagmodifier")

func (dm *DagModifier) Size() (int64, error) {
pbnd, ok := dm.curNode.(*mdag.ProtoNode)
if !ok {
return 0, ErrNoRawYet
}

pbn, err := ft.FromBytes(pbnd.Data())
if err != nil {
return 0, err
}

if dm.wrBuf != nil {
if uint64(dm.wrBuf.Len())+dm.writeStart > pbn.GetFilesize() {
switch nd := dm.curNode.(type) {
case *mdag.ProtoNode:
pbn, err := ft.FromBytes(nd.Data())
if err != nil {
return 0, err
}
if dm.wrBuf != nil && uint64(dm.wrBuf.Len())+dm.writeStart > pbn.GetFilesize() {
return int64(dm.wrBuf.Len()) + int64(dm.writeStart), nil
}
return int64(pbn.GetFilesize()), nil
case *mdag.RawNode:
if dm.wrBuf != nil {
return 0, ErrNoRawYet
}
sz, err := nd.Size()
return int64(sz), err
default:
return 0, ErrNotUnixfs
}

return int64(pbn.GetFilesize()), nil
}

// Sync writes changes to this dag to disk
Expand Down Expand Up @@ -397,12 +399,12 @@ func (dm *DagModifier) CtxReadFull(ctx context.Context, b []byte) (int, error) {
}

// GetNode gets the modified DAG Node
func (dm *DagModifier) GetNode() (*mdag.ProtoNode, error) {
func (dm *DagModifier) GetNode() (node.Node, error) {
err := dm.Sync()
if err != nil {
return nil, err
}
return dm.curNode.Copy().(*mdag.ProtoNode), nil
return dm.curNode.Copy(), nil
}

// HasChanges returned whether or not there are unflushed changes to this dag
Expand Down
3 changes: 2 additions & 1 deletion mod/dagmodifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

h "github.com/ipfs/go-ipfs/importer/helpers"
trickle "github.com/ipfs/go-ipfs/importer/trickle"
mdag "github.com/ipfs/go-ipfs/merkledag"
ft "github.com/ipfs/go-ipfs/unixfs"
uio "github.com/ipfs/go-ipfs/unixfs/io"
testu "github.com/ipfs/go-ipfs/unixfs/test"
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestDagModifierBasic(t *testing.T) {
t.Fatal(err)
}

size, err := ft.DataSize(node.Data())
size, err := ft.DataSize(node.(*mdag.ProtoNode).Data())
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit ec9e96f

Please sign in to comment.