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

Commit 24dbda9

Browse files
committed
feat(dir): add plumbing addLinkChild() to avoid fetching the node (#101)
1 parent 009757e commit 24dbda9

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

io/directory.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,28 @@ func (d *BasicDirectory) SetCidBuilder(builder cid.Builder) {
198198
// AddChild implements the `Directory` interface. It adds (or replaces)
199199
// a link to the given `node` under `name`.
200200
func (d *BasicDirectory) AddChild(ctx context.Context, name string, node ipld.Node) error {
201+
link, err := ipld.MakeLink(node)
202+
if err != nil {
203+
return err
204+
}
205+
206+
return d.addLinkChild(ctx, name, link)
207+
}
208+
209+
// addLinkChild adds the link as an entry to this directory under the given
210+
// name. Plumbing function for the AddChild API.
211+
func (d *BasicDirectory) addLinkChild(ctx context.Context, name string, link *ipld.Link) error {
201212
// Remove old link (if it existed; ignore `ErrNotExist` otherwise).
202213
err := d.RemoveChild(ctx, name)
203214
if err != nil && err != os.ErrNotExist {
204215
return err
205216
}
206217

207-
err = d.node.AddNodeLink(name, node)
218+
err = d.node.AddRawLink(name, link)
208219
if err != nil {
209220
return err
210221
}
211-
d.addToEstimatedSize(name, node.Cid())
222+
d.addToEstimatedSize(name, link.Cid)
212223
return nil
213224
}
214225

@@ -398,22 +409,14 @@ func (d *HAMTDirectory) switchToBasic(ctx context.Context) (*BasicDirectory, err
398409
basicDir.SetCidBuilder(d.GetCidBuilder())
399410

400411
d.ForEachLink(context.TODO(), func(lnk *ipld.Link) error {
401-
node, err := d.dserv.Get(ctx, lnk.Cid)
402-
if err != nil {
403-
return err
404-
}
405-
406-
err = basicDir.AddChild(ctx, lnk.Name, node)
412+
err := basicDir.addLinkChild(ctx, lnk.Name, lnk)
407413
if err != nil {
408414
return err
409415
}
410416

411417
return nil
412418
})
413419
// FIXME: The above was adapted from SwitchToSharding. We can probably optimize:
414-
// 1. Do not retrieve the full node but create the link from the
415-
// HAMT entry and add it to the BasicDirectory with a (new)
416-
// plumbing function that operates below the AddChild level.
417420
// 2. Do not enumerate all link from scratch (harder than previous
418421
// item). We call this function only from the UpgradeableDirectory
419422
// when we went below the threshold: the detection will be done through

0 commit comments

Comments
 (0)