@@ -198,17 +198,28 @@ func (d *BasicDirectory) SetCidBuilder(builder cid.Builder) {
198
198
// AddChild implements the `Directory` interface. It adds (or replaces)
199
199
// a link to the given `node` under `name`.
200
200
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 {
201
212
// Remove old link (if it existed; ignore `ErrNotExist` otherwise).
202
213
err := d .RemoveChild (ctx , name )
203
214
if err != nil && err != os .ErrNotExist {
204
215
return err
205
216
}
206
217
207
- err = d .node .AddNodeLink (name , node )
218
+ err = d .node .AddRawLink (name , link )
208
219
if err != nil {
209
220
return err
210
221
}
211
- d .addToEstimatedSize (name , node .Cid () )
222
+ d .addToEstimatedSize (name , link .Cid )
212
223
return nil
213
224
}
214
225
@@ -398,22 +409,14 @@ func (d *HAMTDirectory) switchToBasic(ctx context.Context) (*BasicDirectory, err
398
409
basicDir .SetCidBuilder (d .GetCidBuilder ())
399
410
400
411
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 )
407
413
if err != nil {
408
414
return err
409
415
}
410
416
411
417
return nil
412
418
})
413
419
// 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.
417
420
// 2. Do not enumerate all link from scratch (harder than previous
418
421
// item). We call this function only from the UpgradeableDirectory
419
422
// when we went below the threshold: the detection will be done through
0 commit comments