Skip to content

Commit

Permalink
Merge pull request #5160 from schomatis/feat/unixfs/dir-interface
Browse files Browse the repository at this point in the history
unixfs: add a directory interface
  • Loading branch information
whyrusleeping authored Jul 16, 2018
2 parents 8fa1c88 + c47cd13 commit b126601
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 234 deletions.
65 changes: 45 additions & 20 deletions mfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ type Directory struct {
lock sync.Mutex
ctx context.Context

dirbuilder *uio.Directory
// UnixFS directory implementation used for creating,
// reading and editing directories.
unixfsDir uio.Directory

modTime time.Time

Expand All @@ -51,25 +53,25 @@ func NewDirectory(ctx context.Context, name string, node ipld.Node, parent child
}

return &Directory{
dserv: dserv,
ctx: ctx,
name: name,
dirbuilder: db,
parent: parent,
childDirs: make(map[string]*Directory),
files: make(map[string]*File),
modTime: time.Now(),
dserv: dserv,
ctx: ctx,
name: name,
unixfsDir: db,
parent: parent,
childDirs: make(map[string]*Directory),
files: make(map[string]*File),
modTime: time.Now(),
}, nil
}

// GetPrefix gets the CID prefix of the root node
func (d *Directory) GetPrefix() *cid.Prefix {
return d.dirbuilder.GetPrefix()
return d.unixfsDir.GetPrefix()
}

// SetPrefix sets the CID prefix
func (d *Directory) SetPrefix(prefix *cid.Prefix) {
d.dirbuilder.SetPrefix(prefix)
d.unixfsDir.SetPrefix(prefix)
}

// closeChild updates the child by the given name to the dag node 'nd'
Expand Down Expand Up @@ -103,7 +105,7 @@ func (d *Directory) closeChildUpdate(name string, nd ipld.Node, sync bool) (*dag
}

func (d *Directory) flushCurrentNode() (*dag.ProtoNode, error) {
nd, err := d.dirbuilder.GetNode()
nd, err := d.unixfsDir.GetNode()
if err != nil {
return nil, err
}
Expand All @@ -122,7 +124,7 @@ func (d *Directory) flushCurrentNode() (*dag.ProtoNode, error) {
}

func (d *Directory) updateChild(name string, nd ipld.Node) error {
err := d.dirbuilder.AddChild(d.ctx, name, nd)
err := d.AddUnixFSChild(name, nd)
if err != nil {
return err
}
Expand Down Expand Up @@ -206,7 +208,7 @@ func (d *Directory) Uncache(name string) {
// childFromDag searches through this directories dag node for a child link
// with the given name
func (d *Directory) childFromDag(name string) (ipld.Node, error) {
return d.dirbuilder.Find(d.ctx, name)
return d.unixfsDir.Find(d.ctx, name)
}

// childUnsync returns the child under this directory by the given name
Expand Down Expand Up @@ -237,7 +239,7 @@ func (d *Directory) ListNames(ctx context.Context) ([]string, error) {
defer d.lock.Unlock()

var out []string
err := d.dirbuilder.ForEachLink(ctx, func(l *ipld.Link) error {
err := d.unixfsDir.ForEachLink(ctx, func(l *ipld.Link) error {
out = append(out, l.Name)
return nil
})
Expand All @@ -262,7 +264,7 @@ func (d *Directory) List(ctx context.Context) ([]NodeListing, error) {
func (d *Directory) ForEachEntry(ctx context.Context, f func(NodeListing) error) error {
d.lock.Lock()
defer d.lock.Unlock()
return d.dirbuilder.ForEachLink(ctx, func(l *ipld.Link) error {
return d.unixfsDir.ForEachLink(ctx, func(l *ipld.Link) error {
c, err := d.childUnsync(l.Name)
if err != nil {
return err
Expand Down Expand Up @@ -315,7 +317,7 @@ func (d *Directory) Mkdir(name string) (*Directory, error) {
return nil, err
}

err = d.dirbuilder.AddChild(d.ctx, name, ndir)
err = d.AddUnixFSChild(name, ndir)
if err != nil {
return nil, err
}
Expand All @@ -336,7 +338,7 @@ func (d *Directory) Unlink(name string) error {
delete(d.childDirs, name)
delete(d.files, name)

return d.dirbuilder.RemoveChild(d.ctx, name)
return d.unixfsDir.RemoveChild(d.ctx, name)
}

func (d *Directory) Flush() error {
Expand All @@ -363,7 +365,7 @@ func (d *Directory) AddChild(name string, nd ipld.Node) error {
return err
}

err = d.dirbuilder.AddChild(d.ctx, name, nd)
err = d.AddUnixFSChild(name, nd)
if err != nil {
return err
}
Expand All @@ -372,6 +374,29 @@ func (d *Directory) AddChild(name string, nd ipld.Node) error {
return nil
}

// AddUnixFSChild adds a child to the inner UnixFS directory
// and transitions to a HAMT implementation if needed.
func (d *Directory) AddUnixFSChild(name string, node ipld.Node) error {
if uio.UseHAMTSharding {
// If the directory HAMT implementation is being used and this
// directory is actually a basic implementation switch it to HAMT.
if basicDir, ok := d.unixfsDir.(*uio.BasicDirectory); ok {
hamtDir, err := basicDir.SwitchToSharding(d.ctx)
if err != nil {
return err
}
d.unixfsDir = hamtDir
}
}

err := d.unixfsDir.AddChild(d.ctx, name, node)
if err != nil {
return err
}

return nil
}

func (d *Directory) sync() error {
for name, dir := range d.childDirs {
nd, err := dir.GetNode()
Expand Down Expand Up @@ -426,7 +451,7 @@ func (d *Directory) GetNode() (ipld.Node, error) {
return nil, err
}

nd, err := d.dirbuilder.GetNode()
nd, err := d.unixfsDir.GetNode()
if err != nil {
return nil, err
}
Expand Down
214 changes: 0 additions & 214 deletions unixfs/io/dirbuilder.go

This file was deleted.

Loading

0 comments on commit b126601

Please sign in to comment.