-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Golint: fix golint warnings in merkledag submodule #4665
Changes from 3 commits
2d138b0
2743dd2
d04de00
69497a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// package merkledag implements the IPFS Merkle DAG datastructures. | ||
// Package merkledag implements the IPFS Merkle DAG data structures. | ||
package merkledag | ||
|
||
import ( | ||
|
@@ -23,7 +23,13 @@ func init() { | |
ipld.Register(cid.DagCBOR, ipldcbor.DecodeBlock) | ||
} | ||
|
||
// contextKey is a type to use as value for the ProgressTracker contexts. | ||
type contextKey string | ||
|
||
const progressContextKey contextKey = "progress" | ||
|
||
// NewDAGService constructs a new DAGService (using the default implementation). | ||
// Note that the default implementation is also an ipld.LinkGetter. | ||
func NewDAGService(bs bserv.BlockService) *dagService { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does golint really complain about this? I actually prefer to return concrete types whenever possible. It helps to deal with multiple potentially fulfilled interfaces. For example, if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Want me to export |
||
return &dagService{Blocks: bs} | ||
} | ||
|
@@ -147,8 +153,8 @@ func (sg *sesGetter) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *ipld. | |
} | ||
|
||
// Session returns a NodeGetter using a new session for block fetches. | ||
func (ds *dagService) Session(ctx context.Context) ipld.NodeGetter { | ||
return &sesGetter{bserv.NewSession(ctx, ds.Blocks)} | ||
func (n *dagService) Session(ctx context.Context) ipld.NodeGetter { | ||
return &sesGetter{bserv.NewSession(ctx, n.Blocks)} | ||
} | ||
|
||
// FetchGraph fetches all nodes that are children of the given node | ||
|
@@ -159,7 +165,7 @@ func FetchGraph(ctx context.Context, root *cid.Cid, serv ipld.DAGService) error | |
ng = &sesGetter{bserv.NewSession(ctx, ds.Blocks)} | ||
} | ||
|
||
v, _ := ctx.Value("progress").(*ProgressTracker) | ||
v, _ := ctx.Value(progressContextKey).(*ProgressTracker) | ||
if v == nil { | ||
return EnumerateChildrenAsync(ctx, GetLinksDirect(ng), root, cid.NewSet().Visit) | ||
} | ||
|
@@ -168,9 +174,8 @@ func FetchGraph(ctx context.Context, root *cid.Cid, serv ipld.DAGService) error | |
if set.Visit(c) { | ||
v.Increment() | ||
return true | ||
} else { | ||
return false | ||
} | ||
return false | ||
} | ||
return EnumerateChildrenAsync(ctx, GetLinksDirect(ng), root, visit) | ||
} | ||
|
@@ -179,8 +184,8 @@ func FetchGraph(ctx context.Context, root *cid.Cid, serv ipld.DAGService) error | |
// returns the indexes of any links pointing to it | ||
func FindLinks(links []*cid.Cid, c *cid.Cid, start int) []int { | ||
var out []int | ||
for i, lnk_c := range links[start:] { | ||
if c.Equals(lnk_c) { | ||
for i, lnkC := range links[start:] { | ||
if c.Equals(lnkC) { | ||
out = append(out, i+start) | ||
} | ||
} | ||
|
@@ -265,21 +270,26 @@ func EnumerateChildren(ctx context.Context, getLinks GetLinks, root *cid.Cid, vi | |
return nil | ||
} | ||
|
||
// ProgressTracker is used to show progress when fetching nodes. | ||
type ProgressTracker struct { | ||
Total int | ||
lk sync.Mutex | ||
} | ||
|
||
// DeriveContext returns a new context with value "progress" derived from | ||
// the given one. | ||
func (p *ProgressTracker) DeriveContext(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, "progress", p) | ||
return context.WithValue(ctx, progressContextKey, p) | ||
} | ||
|
||
// Increment adds one to the total progress. | ||
func (p *ProgressTracker) Increment() { | ||
p.lk.Lock() | ||
defer p.lk.Unlock() | ||
p.Total++ | ||
} | ||
|
||
// Value returns the current progress. | ||
func (p *ProgressTracker) Value() int { | ||
p.lk.Lock() | ||
defer p.lk.Unlock() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ import ( | |
"testing" | ||
"time" | ||
|
||
blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be grouped with |
||
|
||
bserv "github.com/ipfs/go-ipfs/blockservice" | ||
bstest "github.com/ipfs/go-ipfs/blockservice/test" | ||
offline "github.com/ipfs/go-ipfs/exchange/offline" | ||
|
@@ -22,7 +24,6 @@ import ( | |
mdpb "github.com/ipfs/go-ipfs/merkledag/pb" | ||
dstest "github.com/ipfs/go-ipfs/merkledag/test" | ||
uio "github.com/ipfs/go-ipfs/unixfs/io" | ||
blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format" | ||
|
||
u "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util" | ||
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" | ||
|
@@ -241,9 +242,9 @@ func TestFetchGraph(t *testing.T) { | |
// create an offline dagstore and ensure all blocks were fetched | ||
bs := bserv.New(bsis[1].Blockstore(), offline.Exchange(bsis[1].Blockstore())) | ||
|
||
offline_ds := NewDAGService(bs) | ||
offlineDS := NewDAGService(bs) | ||
|
||
err = EnumerateChildren(context.Background(), offline_ds.GetLinks, root.Cid(), func(_ *cid.Cid) bool { return true }) | ||
err = EnumerateChildren(context.Background(), offlineDS.GetLinks, root.Cid(), func(_ *cid.Cid) bool { return true }) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -260,6 +261,7 @@ func TestEnumerateChildren(t *testing.T) { | |
} | ||
|
||
set := cid.NewSet() | ||
|
||
err = EnumerateChildren(context.Background(), ds.GetLinks, root.Cid(), set.Visit) | ||
if err != nil { | ||
t.Fatal(err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would export this type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has no use outside. If someone ever has a use, it can be exported then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, your right. Sorry.