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

Commit

Permalink
iterator technique for unixfs dir listing
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <why@ipfs.io>
  • Loading branch information
whyrusleeping committed Mar 22, 2017
1 parent 21a5430 commit c9a8a08
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
18 changes: 10 additions & 8 deletions hamt/hamt.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,22 +321,24 @@ func (ds *HamtShard) getValue(ctx context.Context, hv *hashBits, key string, cb

func (ds *HamtShard) EnumLinks() ([]*node.Link, error) {
var links []*node.Link
err := ds.walkTrie(func(sv *shardValue) error {
err := ds.ForEachLink(func(l *node.Link) error {
links = append(links, l)
return nil
})
return links, err
}

func (ds *HamtShard) ForEachLink(f func(*node.Link) error) error {
return ds.walkTrie(func(sv *shardValue) error {
lnk, err := node.MakeLink(sv.val)
if err != nil {
return err
}

lnk.Name = sv.key

links = append(links, lnk)
return nil
return f(lnk)
})
if err != nil {
return nil, err
}

return links, nil
}

func (ds *HamtShard) walkTrie(cb func(*shardValue) error) error {
Expand Down
13 changes: 13 additions & 0 deletions io/dirbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ func (d *Directory) switchToSharding(ctx context.Context) error {
return nil
}

func (d *Directory) ForEachLink(f func(*node.Link) error) error {
if d.shard == nil {
for _, l := range d.dirnode.Links() {
if err := f(l); err != nil {
return err
}
}
return nil
}

return d.shard.ForEachLink(f)
}

func (d *Directory) Links() ([]*node.Link, error) {
if d.shard == nil {
return d.dirnode.Links(), nil
Expand Down

0 comments on commit c9a8a08

Please sign in to comment.