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

Commit 8c3d5ec

Browse files
committed
feat: add UpgradeableDirectory
1 parent a20f6cb commit 8c3d5ec

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

io/directory.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ type HAMTDirectory struct {
8181
dserv ipld.DAGService
8282
}
8383

84-
// NewDirectory returns a Directory. It needs a `DAGService` to add the children.
84+
// NewDirectory returns a Directory that can either be a HAMTDirectory if the
85+
// UseHAMTSharding is set, or otherwise an UpgradeableDirectory containing a
86+
// BasicDirectory that can be converted to a HAMTDirectory if the option is
87+
// set in the future.
8588
func NewDirectory(dserv ipld.DAGService) Directory {
8689
if UseHAMTSharding {
8790
dir := new(HAMTDirectory)
@@ -94,10 +97,10 @@ func NewDirectory(dserv ipld.DAGService) Directory {
9497
return dir
9598
}
9699

97-
dir := new(BasicDirectory)
98-
dir.node = format.EmptyDirNode()
99-
dir.dserv = dserv
100-
return dir
100+
basicDir := new(BasicDirectory)
101+
basicDir.node = format.EmptyDirNode()
102+
basicDir.dserv = dserv
103+
return UpgradeableDirectory{basicDir}
101104
}
102105

103106
// ErrNotADir implies that the given node was not a unixfs directory
@@ -294,3 +297,27 @@ func (d *HAMTDirectory) GetNode() (ipld.Node, error) {
294297
func (d *HAMTDirectory) GetCidBuilder() cid.Builder {
295298
return d.shard.CidBuilder()
296299
}
300+
301+
// UpgradeableDirectory wraps a Directory interface and provides extra logic
302+
// to upgrade from its BasicDirectory implementation to HAMTDirectory.
303+
type UpgradeableDirectory struct {
304+
Directory
305+
}
306+
307+
var _ Directory = (*UpgradeableDirectory)(nil)
308+
309+
// AddChild implements the `Directory` interface. We check when adding new entries
310+
// if we should switch to HAMTDirectory according to global option(s).
311+
func (d UpgradeableDirectory) AddChild(ctx context.Context, name string, nd ipld.Node) error {
312+
if UseHAMTSharding {
313+
if basicDir, ok := d.Directory.(*BasicDirectory); ok {
314+
hamtDir, err := basicDir.SwitchToSharding(ctx)
315+
if err != nil {
316+
return err
317+
}
318+
d.Directory = hamtDir
319+
}
320+
}
321+
322+
return d.Directory.AddChild(ctx, name, nd)
323+
}

0 commit comments

Comments
 (0)