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

feat: add UpgradeableDirectory #89

Merged
merged 4 commits into from
May 6, 2021
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
37 changes: 32 additions & 5 deletions io/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ type HAMTDirectory struct {
dserv ipld.DAGService
}

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

dir := new(BasicDirectory)
dir.node = format.EmptyDirNode()
dir.dserv = dserv
return dir
basicDir := new(BasicDirectory)
basicDir.node = format.EmptyDirNode()
basicDir.dserv = dserv
return &UpgradeableDirectory{basicDir}
}

// ErrNotADir implies that the given node was not a unixfs directory
Expand Down Expand Up @@ -294,3 +297,27 @@ func (d *HAMTDirectory) GetNode() (ipld.Node, error) {
func (d *HAMTDirectory) GetCidBuilder() cid.Builder {
return d.shard.CidBuilder()
}

// UpgradeableDirectory wraps a Directory interface and provides extra logic
// to upgrade from its BasicDirectory implementation to HAMTDirectory.
type UpgradeableDirectory struct {
Directory
}

var _ Directory = (*UpgradeableDirectory)(nil)

// AddChild implements the `Directory` interface. We check when adding new entries
// if we should switch to HAMTDirectory according to global option(s).
func (d *UpgradeableDirectory) AddChild(ctx context.Context, name string, nd ipld.Node) error {
if UseHAMTSharding {
if basicDir, ok := d.Directory.(*BasicDirectory); ok {
Copy link
Member

Choose a reason for hiding this comment

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

nit: check both types and fail if we have some strange third type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, will address in upcoming PR, i already have some commits on top of this code

Copy link
Member

Choose a reason for hiding this comment

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

(note: for me, "nit" means "I don't really care but I had this random thought while reading this code"; totally optional)

hamtDir, err := basicDir.SwitchToSharding(ctx)
if err != nil {
return err
}
d.Directory = hamtDir
}
}

return d.Directory.AddChild(ctx, name, nd)
}
24 changes: 24 additions & 0 deletions io/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ func TestDuplicateAddDir(t *testing.T) {
}
}

func TestUpgradeableDirectory(t *testing.T) {
oldHamtOption := UseHAMTSharding
defer func() { UseHAMTSharding = oldHamtOption }()

ds := mdtest.Mock()
UseHAMTSharding = false // Create a BasicDirectory.
dir := NewDirectory(ds)
if _, ok := dir.(*UpgradeableDirectory).Directory.(*BasicDirectory); !ok {
t.Fatal("UpgradeableDirectory doesn't contain BasicDirectory")
}

// Any new directory entry will trigger the upgrade to HAMTDirectory
UseHAMTSharding = true

err := dir.AddChild(context.Background(), "test", ft.EmptyDirNode())
if err != nil {
t.Fatal(err)
}

if _, ok := dir.(*UpgradeableDirectory).Directory.(*HAMTDirectory); !ok {
t.Fatal("UpgradeableDirectory wasn't upgraded to HAMTDirectory")
}
}

func TestDirBuilder(t *testing.T) {
ds := mdtest.Mock()
dir := NewDirectory(ds)
Expand Down