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

Commit

Permalink
Enable CidV1 (and other prefixes) in the Dag Modifier.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <[email protected]>
  • Loading branch information
kevina committed Oct 19, 2017
1 parent e3ad5de commit 109d198
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 161 deletions.
12 changes: 6 additions & 6 deletions io/dagreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func TestBasicRead(t *testing.T) {
dserv := testu.GetDAGServ()
inbuf, node := testu.GetRandomNode(t, dserv, 1024, testu.ProtoBufLeaves)
inbuf, node := testu.GetRandomNode(t, dserv, 1024, testu.UseProtoBufLeaves)
ctx, closer := context.WithCancel(context.Background())
defer closer()

Expand All @@ -44,7 +44,7 @@ func TestSeekAndRead(t *testing.T) {
inbuf[i] = byte(i)
}

node := testu.GetNode(t, dserv, inbuf, testu.ProtoBufLeaves)
node := testu.GetNode(t, dserv, inbuf, testu.UseProtoBufLeaves)
ctx, closer := context.WithCancel(context.Background())
defer closer()

Expand Down Expand Up @@ -84,7 +84,7 @@ func TestRelativeSeek(t *testing.T) {
}

inbuf[1023] = 1 // force the reader to be 1024 bytes
node := testu.GetNode(t, dserv, inbuf, testu.ProtoBufLeaves)
node := testu.GetNode(t, dserv, inbuf, testu.UseProtoBufLeaves)

reader, err := NewDagReader(ctx, node, dserv)
if err != nil {
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestBadPBData(t *testing.T) {

func TestMetadataNode(t *testing.T) {
dserv := testu.GetDAGServ()
rdata, rnode := testu.GetRandomNode(t, dserv, 512, testu.ProtoBufLeaves)
rdata, rnode := testu.GetRandomNode(t, dserv, 512, testu.UseProtoBufLeaves)
_, err := dserv.Add(rnode)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestMetadataNode(t *testing.T) {

func TestWriteTo(t *testing.T) {
dserv := testu.GetDAGServ()
inbuf, node := testu.GetRandomNode(t, dserv, 1024, testu.ProtoBufLeaves)
inbuf, node := testu.GetRandomNode(t, dserv, 1024, testu.UseProtoBufLeaves)
ctx, closer := context.WithCancel(context.Background())
defer closer()

Expand All @@ -225,7 +225,7 @@ func TestWriteTo(t *testing.T) {
func TestReaderSzie(t *testing.T) {
dserv := testu.GetDAGServ()
size := int64(1024)
_, node := testu.GetRandomNode(t, dserv, size, testu.ProtoBufLeaves)
_, node := testu.GetRandomNode(t, dserv, size, testu.UseProtoBufLeaves)
ctx, closer := context.WithCancel(context.Background())
defer closer()

Expand Down
24 changes: 20 additions & 4 deletions mod/dagmodifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ type DagModifier struct {
curWrOff uint64
wrBuf *bytes.Buffer

Prefix cid.Prefix
RawLeaves bool

read uio.DagReader
}

var ErrNotUnixfs = fmt.Errorf("dagmodifier only supports unixfs nodes (proto or raw)")

// NewDagModifier returns a new DagModifier, the Cid prefix for newly
// created nodes will be inherted from the passed in node. If the Cid
// version if not 0 raw leaves will also be enabled. The Prefix and
// RawLeaves options can be overridden by changing them after the call.
func NewDagModifier(ctx context.Context, from node.Node, serv mdag.DAGService, spl chunk.SplitterGen) (*DagModifier, error) {
switch from.(type) {
case *mdag.ProtoNode, *mdag.RawNode:
Expand All @@ -55,11 +60,20 @@ func NewDagModifier(ctx context.Context, from node.Node, serv mdag.DAGService, s
return nil, ErrNotUnixfs
}

prefix := from.Cid().Prefix()
prefix.Codec = cid.DagProtobuf
rawLeaves := false
if prefix.Version > 0 {
rawLeaves = true
}

return &DagModifier{
curNode: from.Copy(),
dagserv: serv,
splitter: spl,
ctx: ctx,
curNode: from.Copy(),
dagserv: serv,
splitter: spl,
ctx: ctx,
Prefix: prefix,
RawLeaves: rawLeaves,
}, nil
}

Expand Down Expand Up @@ -240,6 +254,7 @@ func (dm *DagModifier) modifyDag(n node.Node, offset uint64, data io.Reader) (*c

nd := new(mdag.ProtoNode)
nd.SetData(b)
nd.SetPrefix(&nd0.Prefix)
k, err := dm.dagserv.Add(nd)
if err != nil {
return nil, false, err
Expand Down Expand Up @@ -345,6 +360,7 @@ func (dm *DagModifier) appendData(nd node.Node, spl chunk.Splitter) (node.Node,
dbp := &help.DagBuilderParams{
Dagserv: dm.dagserv,
Maxlinks: help.DefaultLinksPerBlock,
Prefix: &dm.Prefix,
RawLeaves: dm.RawLeaves,
}
return trickle.TrickleAppend(dm.ctx, nd, dbp.New(spl))
Expand Down
Loading

0 comments on commit 109d198

Please sign in to comment.