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

Make switchToSharding more efficient #120

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions hamt/hamt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

@aschmahmann aschmahmann Jun 16, 2022

Choose a reason for hiding this comment

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

nit: Not required, but if in the future someone wants to extract this into reusable ds.SwapLink function for similar types of optimizations they can do so. Although it's really just a few copied lines of code.


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
Comment on lines +244 to +246
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is copy-pasted from Set() above.


_, 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).
Expand Down
7 changes: 1 addition & 6 deletions io/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down