Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add copy dagservice function #41

Merged
merged 4 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions daghelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ func GetNodes(ctx context.Context, ds NodeGetter, keys []*cid.Cid) []*NodePromis
return promises
}

func Copy(ctx context.Context, from, to DAGService, root *cid.Cid) error {
node, err := from.Get(ctx, root)
if err != nil {
return err
}
links := node.Links()
if len(links) == 0 {
err := to.Add(ctx, node)
if err != nil {
return err
}
return nil
}
for _, link := range links {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to copy the intermediate nodes, right? This just copies the leaves.

err := Copy(ctx, from, to, link.Cid)
if err != nil {
return err
}
err = to.Add(ctx, node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to do this once per link, just once.

if err != nil {
return err
}
}
return nil
}

// Remove duplicates from a list of keys
func dedupeKeys(cids []*cid.Cid) []*cid.Cid {
set := cid.NewSet()
Expand Down
144 changes: 144 additions & 0 deletions daghelpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package format

import (
"context"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/stretchr/testify/assert"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try not to add too many external deps unless we need them. I'd prefer it if we could stick with go's testing tools.

"testing"
)

type TestNode struct {
links []*Link
data []byte
builder cid.Builder
}

var v0CidPrefix = cid.Prefix{
Codec: cid.DagProtobuf,
MhLength: -1,
MhType: mh.SHA2_256,
Version: 0,
}

func InitNode(d []byte) *TestNode {
return &TestNode{
data: d,
builder: v0CidPrefix,
}
}

func (n *TestNode) Resolve([]string) (interface{}, []string, error) {
return nil, nil, EmptyNodeError
}

func (n *TestNode) Tree(string, int) []string {
return nil
}

func (n *TestNode) ResolveLink([]string) (*Link, []string, error) {
return nil, nil, EmptyNodeError
}

func (n *TestNode) Copy() Node {
return &EmptyNode{}
}

func (n *TestNode) Cid() *cid.Cid {
c, err := n.builder.Sum(n.RawData())
if err != nil {
return nil
}
return c
}

func (n *TestNode) Links() []*Link {
return n.links
}

func (n *TestNode) Loggable() map[string]interface{} {
return nil
}

func (n *TestNode) String() string {
return string(n.data)
}

func (n *TestNode) RawData() []byte {
return n.data
}

func (n *TestNode) Size() (uint64, error) {
return 0, nil
}

func (n *TestNode) Stat() (*NodeStat, error) {
return &NodeStat{}, nil
}

// AddNodeLink adds a link to another node.
func (n *TestNode) AddNodeLink(name string, that Node) error {

lnk, err := MakeLink(that)
if err != nil {
return err
}

lnk.Name = name

n.AddRawLink(name, lnk)

return nil
}

func (n *TestNode) AddRawLink(name string, l *Link) error {

n.links = append(n.links, &Link{
Name: name,
Size: l.Size,
Cid: l.Cid,
})
return nil
}

func TestCopy(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably test with a dag that has children.

defer cancel()
from := newTestDag()

root := InitNode([]byte("level0"))
l11 := InitNode([]byte("leve1_node1"))
l12 := InitNode([]byte("leve1_node2"))
l21 := InitNode([]byte("leve2_node1"))
l22 := InitNode([]byte("leve2_node2"))
l23 := InitNode([]byte("leve2_node3"))

l11.AddNodeLink(l21.Cid().String(), l21)
l11.AddNodeLink(l22.Cid().String(), l22)
l11.AddNodeLink(l23.Cid().String(), l23)
root.AddNodeLink(l11.Cid().String(), l11)
root.AddNodeLink(l12.Cid().String(), l12)

for _, n := range []Node{l23, l22, l21, l12, l11, root} {
err := from.Add(ctx, n)
if err != nil {
t.Fatal(err)
}
}
to := newTestDag()
err := Copy(ctx, from, to, root.Cid())
if err != nil {
t.Error(err)
}

r, err := to.Get(ctx, root.Cid())
if err != nil {
t.Error(err)
}
l1, err := to.Get(ctx, l11.Cid())
if err != nil {
t.Error(err)
}
assert.Equal(t, len(r.Links()), 2)
assert.Equal(t, len(l1.Links()), 3)
}