diff --git a/hamt/hamt.go b/hamt/hamt.go index ac1c5e458..593b64627 100644 --- a/hamt/hamt.go +++ b/hamt/hamt.go @@ -229,6 +229,26 @@ func (ds *Shard) Set(ctx context.Context, name string, nd ipld.Node) error { return err } +// Set sets 'name' = nd in the HAMT, using directly the information in the +// given link. This avoids writing the given node, then reading it to making a +// link out of it. +func (ds *Shard) SetLink(ctx context.Context, name string, lnk *ipld.Link) error { + hv := newHashBits(name) + + newLink := ipld.Link{ + Name: lnk.Name, + Size: lnk.Size, + Cid: lnk.Cid, + } + + // FIXME: We don't need to set the name here, it will get overwritten. + // This is confusing, confirm and remove this line. + newLink.Name = ds.linkNamePrefix(0) + name + + _, err := ds.swapValue(ctx, hv, name, &newLink) + return err +} + // Swap sets a link pointing to the passed node as the value under the // name key in this Shard or its children. It also returns the previous link // under that name key (if any). diff --git a/io/directory.go b/io/directory.go index 2ec862247..b602bf9ab 100644 --- a/io/directory.go +++ b/io/directory.go @@ -334,12 +334,7 @@ func (d *BasicDirectory) switchToSharding(ctx context.Context) (*HAMTDirectory, hamtDir.shard = shard for _, lnk := range d.node.Links() { - node, err := d.dserv.Get(ctx, lnk.Cid) - if err != nil { - return nil, err - } - - err = hamtDir.shard.Set(ctx, lnk.Name, node) + err = hamtDir.shard.SetLink(ctx, lnk.Name, lnk) if err != nil { return nil, err }