Skip to content

Commit

Permalink
fix go vet issues in hamt sharding PR
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <[email protected]>
  • Loading branch information
whyrusleeping committed Mar 24, 2017
1 parent c4c6653 commit 65b9716
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 40 deletions.
4 changes: 2 additions & 2 deletions core/commands/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ Examples:
case *mfs.Directory:
if !long {
var output []mfs.NodeListing
names, err := fsn.ListNames()
names, err := fsn.ListNames(req.Context())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand All @@ -361,7 +361,7 @@ Examples:
}
res.SetOutput(&FilesLsOutput{output})
} else {
listing, err := fsn.List()
listing, err := fsn.List(req.Context())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
2 changes: 1 addition & 1 deletion core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ The JSON output contains type information.
return
}

links, err := dir.Links()
links, err := dir.Links(req.Context())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
4 changes: 2 additions & 2 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (adder *Adder) Finalize() (node.Node, error) {

var name string
if !adder.Wrap {
children, err := root.(*mfs.Directory).ListNames()
children, err := root.(*mfs.Directory).ListNames(adder.ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func (adder *Adder) outputDirs(path string, fsn mfs.FSNode) error {
case *mfs.File:
return nil
case *mfs.Directory:
names, err := fsn.ListNames()
names, err := fsn.ListNames(adder.ctx)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion fuse/ipns/ipns_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (s *Directory) Lookup(ctx context.Context, name string) (fs.Node, error) {
// ReadDirAll reads the link structure as directory entries
func (dir *Directory) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
var entries []fuse.Dirent
listing, err := dir.dir.List()
listing, err := dir.dir.List(ctx)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions mfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ type NodeListing struct {
Hash string
}

func (d *Directory) ListNames() ([]string, error) {
func (d *Directory) ListNames(ctx context.Context) ([]string, error) {
d.lock.Lock()
defer d.lock.Unlock()

var out []string
err := d.dirbuilder.ForEachLink(func(l *node.Link) error {
err := d.dirbuilder.ForEachLink(ctx, func(l *node.Link) error {
out = append(out, l.Name)
return nil
})
Expand All @@ -235,9 +235,9 @@ func (d *Directory) ListNames() ([]string, error) {
return out, nil
}

func (d *Directory) List() ([]NodeListing, error) {
func (d *Directory) List(ctx context.Context) ([]NodeListing, error) {
var out []NodeListing
err := d.ForEachEntry(context.TODO(), func(nl NodeListing) error {
err := d.ForEachEntry(ctx, func(nl NodeListing) error {
out = append(out, nl)
return nil
})
Expand All @@ -247,7 +247,7 @@ func (d *Directory) List() ([]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(func(l *node.Link) error {
return d.dirbuilder.ForEachLink(ctx, func(l *node.Link) error {
c, err := d.childUnsync(l.Name)
if err != nil {
return err
Expand Down
16 changes: 10 additions & 6 deletions mfs/mfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func mkdirP(t *testing.T, root *Directory, pth string) *Directory {
}

func assertDirAtPath(root *Directory, pth string, children []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

fsn, err := DirLookup(root, pth)
if err != nil {
return err
Expand All @@ -92,7 +95,7 @@ func assertDirAtPath(root *Directory, pth string, children []string) error {
return fmt.Errorf("%s was not a directory", pth)
}

listing, err := dir.List()
listing, err := dir.List(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -496,7 +499,7 @@ func TestMfsFile(t *testing.T) {

func randomWalk(d *Directory, n int) (*Directory, error) {
for i := 0; i < n; i++ {
dirents, err := d.List()
dirents, err := d.List(context.Background())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -585,7 +588,7 @@ func actorRemoveFile(d *Directory) error {
return err
}

ents, err := d.List()
ents, err := d.List(context.Background())
if err != nil {
return err
}
Expand All @@ -605,7 +608,7 @@ func randomFile(d *Directory) (*File, error) {
return nil, err
}

ents, err := d.List()
ents, err := d.List(context.Background())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -953,6 +956,7 @@ func TestConcurrentReads(t *testing.T) {
}
wg.Wait()
}

func writeFile(rt *Root, path string, data []byte) error {
n, err := Lookup(rt, path)
if err != nil {
Expand All @@ -975,8 +979,8 @@ func writeFile(rt *Root, path string, data []byte) error {
return err
}

if nw != 10 {
fmt.Errorf("wrote incorrect amount")
if nw != len(data) {
return fmt.Errorf("wrote incorrect amount: %d != 10", nw)
}

return nil
Expand Down
14 changes: 7 additions & 7 deletions unixfs/hamt/hamt.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,17 @@ func (ds *HamtShard) getValue(ctx context.Context, hv *hashBits, key string, cb
return os.ErrNotExist
}

func (ds *HamtShard) EnumLinks() ([]*node.Link, error) {
func (ds *HamtShard) EnumLinks(ctx context.Context) ([]*node.Link, error) {
var links []*node.Link
err := ds.ForEachLink(func(l *node.Link) error {
err := ds.ForEachLink(ctx, 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 {
func (ds *HamtShard) ForEachLink(ctx context.Context, f func(*node.Link) error) error {
return ds.walkTrie(ctx, func(sv *shardValue) error {
lnk, err := node.MakeLink(sv.val)
if err != nil {
return err
Expand All @@ -373,7 +373,7 @@ func (ds *HamtShard) ForEachLink(f func(*node.Link) error) error {
})
}

func (ds *HamtShard) walkTrie(cb func(*shardValue) error) error {
func (ds *HamtShard) walkTrie(ctx context.Context, cb func(*shardValue) error) error {
for i := 0; i < ds.tableSize; i++ {
if ds.bitfield.Bit(i) == 0 {
continue
Expand All @@ -382,7 +382,7 @@ func (ds *HamtShard) walkTrie(cb func(*shardValue) error) error {
idx := ds.indexForBitPos(i)
// NOTE: an optimized version could simply iterate over each
// element in the 'children' array.
c, err := ds.getChild(context.TODO(), idx)
c, err := ds.getChild(ctx, idx)
if err != nil {
return err
}
Expand All @@ -395,7 +395,7 @@ func (ds *HamtShard) walkTrie(cb func(*shardValue) error) error {
}

case *HamtShard:
err := c.walkTrie(cb)
err := c.walkTrie(ctx, cb)
if err != nil {
return err
}
Expand Down
17 changes: 10 additions & 7 deletions unixfs/hamt/hamt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func assertLink(s *HamtShard, name string, found bool) error {
}

func assertSerializationWorks(ds dag.DAGService, s *HamtShard) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nd, err := s.Node()
if err != nil {
return err
Expand All @@ -82,12 +84,12 @@ func assertSerializationWorks(ds dag.DAGService, s *HamtShard) error {
return err
}

linksA, err := s.EnumLinks()
linksA, err := s.EnumLinks(ctx)
if err != nil {
return err
}

linksB, err := nds.EnumLinks()
linksB, err := nds.EnumLinks(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -160,7 +162,8 @@ func TestDirBuilding(t *testing.T) {
func TestShardReload(t *testing.T) {
ds := mdtest.Mock()
s, _ := NewHamtShard(ds, 256)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

_, s, err := makeDir(ds, 200)
if err != nil {
Expand All @@ -177,7 +180,7 @@ func TestShardReload(t *testing.T) {
t.Fatal(err)
}

lnks, err := nds.EnumLinks()
lnks, err := nds.EnumLinks(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -270,7 +273,7 @@ func TestSetAfterMarshal(t *testing.T) {
}
}

links, err := nds.EnumLinks()
links, err := nds.EnumLinks(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -301,7 +304,7 @@ func TestDuplicateAddShard(t *testing.T) {
t.Fatal(err)
}

lnks, err := dir.EnumLinks()
lnks, err := dir.EnumLinks(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -393,7 +396,7 @@ func TestRemoveElemsAfterMarshal(t *testing.T) {
}
}

links, err := nds.EnumLinks()
links, err := nds.EnumLinks(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion unixfs/io/dagreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ func TestMetadataNode(t *testing.T) {
ctx, closer := context.WithCancel(context.Background())
defer closer()

data, err := unixfs.BytesForMetadata(&unixfs.Metadata{"text", 125})
data, err := unixfs.BytesForMetadata(&unixfs.Metadata{
MimeType: "text",
Size: 125,
})
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions unixfs/io/dirbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (d *Directory) switchToSharding(ctx context.Context) error {
return nil
}

func (d *Directory) ForEachLink(f func(*node.Link) error) error {
func (d *Directory) ForEachLink(ctx context.Context, f func(*node.Link) error) error {
if d.shard == nil {
for _, l := range d.dirnode.Links() {
if err := f(l); err != nil {
Expand All @@ -129,15 +129,15 @@ func (d *Directory) ForEachLink(f func(*node.Link) error) error {
return nil
}

return d.shard.ForEachLink(f)
return d.shard.ForEachLink(ctx, f)
}

func (d *Directory) Links() ([]*node.Link, error) {
func (d *Directory) Links(ctx context.Context) ([]*node.Link, error) {
if d.shard == nil {
return d.dirnode.Links(), nil
}

return d.shard.EnumLinks()
return d.shard.EnumLinks(ctx)
}

func (d *Directory) Find(ctx context.Context, name string) (node.Node, error) {
Expand Down
8 changes: 4 additions & 4 deletions unixfs/io/dirbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestDirectoryGrowth(t *testing.T) {
t.Fatal(err)
}

links, err := dir.Links()
links, err := dir.Links(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestDuplicateAddDir(t *testing.T) {
t.Fatal(err)
}

lnks, err := dir.Links()
lnks, err := dir.Links(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestDirBuilder(t *testing.T) {
t.Fatal(err)
}

links, err := dir.Links()
links, err := dir.Links(ctx)
if err != nil {
t.Fatal(err)
}
Expand All @@ -135,7 +135,7 @@ func TestDirBuilder(t *testing.T) {
t.Fatal(err)
}

links, err = adir.Links()
links, err = adir.Links(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 65b9716

Please sign in to comment.