Skip to content

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 Nov 16, 2016
1 parent 944f07f commit 07683a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion mfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,5 +433,5 @@ func (d *Directory) GetNode() (node.Node, error) {
return nil, err
}

return nd, err
return nd.Copy(), err
}
20 changes: 11 additions & 9 deletions unixfs/hamt/hamt.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewHamtFromDag(dserv dag.DAGService, nd node.Node) (*HamtShard, error) {
}

ds := makeHamtShard(dserv, int(pbd.GetFanout()))
ds.nd = pbnd.Copy()
ds.nd = pbnd.Copy().(*dag.ProtoNode)
ds.children = make([]child, len(pbnd.Links()))
ds.bitfield = new(big.Int).SetBytes(pbd.GetData())
ds.hashFunc = pbd.GetHashType()
Expand Down 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
16 changes: 14 additions & 2 deletions unixfs/io/dirbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
hamt "github.com/ipfs/go-ipfs/unixfs/hamt"

node "gx/ipfs/QmUsVJ7AEnGyjX8YWnrwq9vmECVGwBQNAKPpgz5KSg8dcq/go-ipld-node"
cid "gx/ipfs/QmcEcrBAMrwMyhSjXt4yfyPpzgSuV8HLHavnfmiKCSRqZU/go-cid"
)

// ShardSplitThreshold specifies how large of an unsharded directory
Expand Down Expand Up @@ -51,7 +50,7 @@ func NewDirectoryFromNode(dserv mdag.DAGService, nd node.Node) (*Directory, erro
case format.TDirectory:
return &Directory{
dserv: dserv,
dirnode: pbnd.Copy(),
dirnode: pbnd.Copy().(*mdag.ProtoNode),
}, nil
case format.THAMTShard:
shard, err := hamt.NewHamtFromDag(dserv, nd)
Expand Down Expand Up @@ -103,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 07683a7

Please sign in to comment.