Golint: fix golint warnings in merkledag submodule
License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
Hector Sanjuan committed
Feb 6, 2018 at 17:05 UTC
2d138b056e5e4a6a5cc9437e4b7485437a042bd3
10 files changed
+111
-41
merkledag/coding.go
+1
-1
@@ -102,7 +102,7 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
102
return n.encoded, nil
103
}
104
105
-// Decoded decodes raw data and returns a new Node instance.
105
+// DecodeProtobuf decodes raw data and returns a new Node instance.
106
func DecodeProtobuf(encoded []byte) (*ProtoNode, error) {
107
n := new(ProtoNode)
108
err := n.unmarshal(encoded)
merkledag/errservice.go
+6
@@ -14,28 +14,34 @@ type ErrorService struct {
14
15
var _ ipld.DAGService = (*ErrorService)(nil)
16
17
+// Add returns an error.
18
func (cs *ErrorService) Add(ctx context.Context, nd ipld.Node) error {
19
return cs.Err
20
}
21
22
+// AddMany returns an error.
23
func (cs *ErrorService) AddMany(ctx context.Context, nds []ipld.Node) error {
24
return cs.Err
25
}
26
27
+// Get returns an error.
28
func (cs *ErrorService) Get(ctx context.Context, c *cid.Cid) (ipld.Node, error) {
29
return nil, cs.Err
30
}
31
32
+// GetMany many returns an error.
33
func (cs *ErrorService) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *ipld.NodeOption {
34
ch := make(chan *ipld.NodeOption)
35
close(ch)
36
return ch
37
}
38
39
+// Remove returns an error.
40
func (cs *ErrorService) Remove(ctx context.Context, c *cid.Cid) error {
41
return cs.Err
42
}
43
44
+// RemoveMany returns an error.
45
func (cs *ErrorService) RemoveMany(ctx context.Context, cids []*cid.Cid) error {
46
return cs.Err
47
}
merkledag/merkledag.go
+20
-10
@@ -1,4 +1,4 @@
1
-// package merkledag implements the IPFS Merkle DAG datastructures.
1
+// Package merkledag implements the IPFS Merkle DAG data structures.
2
package merkledag
3
4
import (
@@ -23,8 +23,14 @@ func init() {
23
ipld.Register(cid.DagCBOR, ipldcbor.DecodeBlock)
24
}
25
26
+// contextKey is a type to use as value for the ProgressTracker contexts.
27
+type contextKey string
28
+
29
+const progressContextKey contextKey = "progress"
30
+
31
// NewDAGService constructs a new DAGService (using the default implementation).
27
-func NewDAGService(bs bserv.BlockService) *dagService {
32
+// Note that the default implementation is also an ipld.LinkGetter.
33
+func NewDAGService(bs bserv.BlockService) ipld.DAGService {
34
return &dagService{Blocks: bs}
35
}
36
@@ -147,8 +153,8 @@ func (sg *sesGetter) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *ipld.
153
}
154
155
// Session returns a NodeGetter using a new session for block fetches.
150
-func (ds *dagService) Session(ctx context.Context) ipld.NodeGetter {
151
- return &sesGetter{bserv.NewSession(ctx, ds.Blocks)}
156
+func (n *dagService) Session(ctx context.Context) ipld.NodeGetter {
157
+ return &sesGetter{bserv.NewSession(ctx, n.Blocks)}
158
}
159
160
// 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
165
ng = &sesGetter{bserv.NewSession(ctx, ds.Blocks)}
166
}
167
162
- v, _ := ctx.Value("progress").(*ProgressTracker)
168
+ v, _ := ctx.Value(progressContextKey).(*ProgressTracker)
169
if v == nil {
170
return EnumerateChildrenAsync(ctx, GetLinksDirect(ng), root, cid.NewSet().Visit)
171
}
@@ -168,9 +174,8 @@ func FetchGraph(ctx context.Context, root *cid.Cid, serv ipld.DAGService) error
174
if set.Visit(c) {
175
v.Increment()
176
return true
171
- } else {
172
- return false
177
}
178
+ return false
179
}
180
return EnumerateChildrenAsync(ctx, GetLinksDirect(ng), root, visit)
181
}
@@ -179,8 +184,8 @@ func FetchGraph(ctx context.Context, root *cid.Cid, serv ipld.DAGService) error
184
// returns the indexes of any links pointing to it
185
func FindLinks(links []*cid.Cid, c *cid.Cid, start int) []int {
186
var out []int
182
- for i, lnk_c := range links[start:] {
183
- if c.Equals(lnk_c) {
187
+ for i, lnkC := range links[start:] {
188
+ if c.Equals(lnkC) {
189
out = append(out, i+start)
190
}
191
}
@@ -265,21 +270,26 @@ func EnumerateChildren(ctx context.Context, getLinks GetLinks, root *cid.Cid, vi
270
return nil
271
}
272
273
+// ProgressTracker is used to show progress when fetching nodes.
274
type ProgressTracker struct {
275
Total int
276
lk sync.Mutex
277
}
278
279
+// DeriveContext returns a new context with value "progress" derived from
280
+// the given one.
281
func (p *ProgressTracker) DeriveContext(ctx context.Context) context.Context {
274
- return context.WithValue(ctx, "progress", p)
282
+ return context.WithValue(ctx, progressContextKey, p)
283
}
284
285
+// Increment adds one to the total progress.
286
func (p *ProgressTracker) Increment() {
287
p.lk.Lock()
288
defer p.lk.Unlock()
289
p.Total++
290
}
291
292
+// Value returns the current progress.
293
func (p *ProgressTracker) Value() int {
294
p.lk.Lock()
295
defer p.lk.Unlock()
merkledag/merkledag_test.go
+9
-5
@@ -13,6 +13,8 @@ import (
13
"testing"
14
"time"
15
16
+ blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
17
+
18
bserv "github.com/ipfs/go-ipfs/blockservice"
19
bstest "github.com/ipfs/go-ipfs/blockservice/test"
20
offline "github.com/ipfs/go-ipfs/exchange/offline"
@@ -22,7 +24,6 @@ import (
24
mdpb "github.com/ipfs/go-ipfs/merkledag/pb"
25
dstest "github.com/ipfs/go-ipfs/merkledag/test"
26
uio "github.com/ipfs/go-ipfs/unixfs/io"
25
- blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
27
28
u "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util"
29
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
@@ -241,9 +242,10 @@ func TestFetchGraph(t *testing.T) {
242
// create an offline dagstore and ensure all blocks were fetched
243
bs := bserv.New(bsis[1].Blockstore(), offline.Exchange(bsis[1].Blockstore()))
244
244
- offline_ds := NewDAGService(bs)
245
+ // we know the default dagService implements LinkGetter
246
+ offlineDS := NewDAGService(bs).(ipld.LinkGetter)
247
246
- err = EnumerateChildren(context.Background(), offline_ds.GetLinks, root.Cid(), func(_ *cid.Cid) bool { return true })
248
+ err = EnumerateChildren(context.Background(), offlineDS.GetLinks, root.Cid(), func(_ *cid.Cid) bool { return true })
249
if err != nil {
250
t.Fatal(err)
251
}
@@ -260,7 +262,9 @@ func TestEnumerateChildren(t *testing.T) {
262
}
263
264
set := cid.NewSet()
263
- err = EnumerateChildren(context.Background(), ds.GetLinks, root.Cid(), set.Visit)
265
+ lg := ds.(ipld.LinkGetter)
266
+
267
+ err = EnumerateChildren(context.Background(), lg.GetLinks, root.Cid(), set.Visit)
268
if err != nil {
269
t.Fatal(err)
270
}
@@ -491,7 +495,7 @@ func TestCidRetention(t *testing.T) {
495
}
496
497
func TestCidRawDoesnNeedData(t *testing.T) {
494
- srv := NewDAGService(dstest.Bserv())
498
+ srv := NewDAGService(dstest.Bserv()).(ipld.LinkGetter)
499
nd := NewRawNode([]byte("somedata"))
500
501
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
merkledag/node.go
+26
-5
@@ -10,10 +10,13 @@ import (
10
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
11
)
12
13
-var ErrNotProtobuf = fmt.Errorf("expected protobuf dag node")
14
-var ErrLinkNotFound = fmt.Errorf("no link by that name")
13
+// Common errors
14
+var (
15
+ ErrNotProtobuf = fmt.Errorf("expected protobuf dag node")
16
+ ErrLinkNotFound = fmt.Errorf("no link by that name")
17
+)
18
16
-// Node represents a node in the IPFS Merkle DAG.
19
+// ProtoNode represents a node in the IPFS Merkle DAG.
20
// nodes have opaque data and a set of navigable links.
21
type ProtoNode struct {
22
links []*ipld.Link
@@ -73,12 +76,14 @@ func (n *ProtoNode) SetPrefix(prefix *cid.Prefix) {
76
}
77
}
78
79
+// LinkSlice is a slice of ipld.Links
80
type LinkSlice []*ipld.Link
81
82
func (ls LinkSlice) Len() int { return len(ls) }
83
func (ls LinkSlice) Swap(a, b int) { ls[a], ls[b] = ls[b], ls[a] }
84
func (ls LinkSlice) Less(a, b int) bool { return ls[a].Name < ls[b].Name }
85
86
+// NodeWithData builds a new Protonode with the given data.
87
func NodeWithData(d []byte) *ProtoNode {
88
return &ProtoNode{data: d}
89
}
@@ -204,15 +209,18 @@ func (n *ProtoNode) Copy() ipld.Node {
209
return nnode
210
}
211
212
+// RawData returns the protobuf-encoded version of the node.
213
func (n *ProtoNode) RawData() []byte {
214
out, _ := n.EncodeProtobuf(false)
215
return out
216
}
217
218
+// Data returns the data stored by this node.
219
func (n *ProtoNode) Data() []byte {
220
return n.data
221
}
222
223
+// SetData stores data in this nodes.
224
func (n *ProtoNode) SetData(d []byte) {
225
n.encoded = nil
226
n.cached = nil
@@ -265,12 +273,14 @@ func (n *ProtoNode) Stat() (*ipld.NodeStat, error) {
273
}, nil
274
}
275
276
+// Loggable implements the ipfs/go-log.Loggable interface.
277
func (n *ProtoNode) Loggable() map[string]interface{} {
278
return map[string]interface{}{
279
"node": n.String(),
280
}
281
}
282
283
+// UnmarshalJSON reads the node fields from a JSON-encoded byte slice.
284
func (n *ProtoNode) UnmarshalJSON(b []byte) error {
285
s := struct {
286
Data []byte `json:"data"`
@@ -287,6 +297,7 @@ func (n *ProtoNode) UnmarshalJSON(b []byte) error {
297
return nil
298
}
299
300
+// MarshalJSON returns a JSON representation of the node.
301
func (n *ProtoNode) MarshalJSON() ([]byte, error) {
302
out := map[string]interface{}{
303
"data": n.data,
@@ -296,6 +307,8 @@ func (n *ProtoNode) MarshalJSON() ([]byte, error) {
307
return json.Marshal(out)
308
}
309
310
+// Cid returns the node's Cid, calculated according to its prefix
311
+// and raw data contents.
312
func (n *ProtoNode) Cid() *cid.Cid {
313
if n.encoded != nil && n.cached != nil {
314
return n.cached
@@ -316,6 +329,7 @@ func (n *ProtoNode) Cid() *cid.Cid {
329
return c
330
}
331
332
+// String prints the node's Cid.
333
func (n *ProtoNode) String() string {
334
return n.Cid().String()
335
}
@@ -332,18 +346,24 @@ func (n *ProtoNode) Multihash() mh.Multihash {
346
return n.cached.Hash()
347
}
348
349
+// Links returns the node links.
350
func (n *ProtoNode) Links() []*ipld.Link {
351
return n.links
352
}
353
354
+// SetLinks replaces the node links with the given ones.
355
func (n *ProtoNode) SetLinks(links []*ipld.Link) {
356
n.links = links
357
}
358
359
+// Resolve is an alias for ResolveLink.
360
func (n *ProtoNode) Resolve(path []string) (interface{}, []string, error) {
361
return n.ResolveLink(path)
362
}
363
364
+// ResolveLink consumes the first element of the path and obtains the link
365
+// corresponding to it from the node. It returns the link
366
+// and the path without the consumed element.
367
func (n *ProtoNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
368
if len(path) == 0 {
369
return nil, nil, fmt.Errorf("end of path, no more links to resolve")
@@ -357,9 +377,10 @@ func (n *ProtoNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
377
return lnk, path[1:], nil
378
}
379
380
+// Tree returns the link names of the ProtoNode.
381
+// ProtoNodes are only ever one path deep, so anything different than an empty
382
+// string for p results in nothing. The depth parameter is ignored.
383
func (n *ProtoNode) Tree(p string, depth int) []string {
361
- // ProtoNodes are only ever one path deep, anything below that results in
362
- // nothing
384
if p != "" {
385
return nil
386
}
merkledag/raw.go
+8
@@ -9,6 +9,7 @@ import (
9
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
10
)
11
12
+// RawNode represents a node which only contains data.
13
type RawNode struct {
14
blocks.Block
15
}
@@ -52,22 +53,27 @@ func NewRawNodeWPrefix(data []byte, prefix cid.Prefix) (*RawNode, error) {
53
return &RawNode{blk}, nil
54
}
55
56
+// Links returns nil.
57
func (rn *RawNode) Links() []*ipld.Link {
58
return nil
59
}
60
61
+// ResolveLink returns an error.
62
func (rn *RawNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
63
return nil, nil, ErrLinkNotFound
64
}
65
66
+// Resolve returns an error.
67
func (rn *RawNode) Resolve(path []string) (interface{}, []string, error) {
68
return nil, nil, ErrLinkNotFound
69
}
70
71
+// Tree returns nil.
72
func (rn *RawNode) Tree(p string, depth int) []string {
73
return nil
74
}
75
76
+// Copy performs a deep copy of this node and returns it as an ipld.Node
77
func (rn *RawNode) Copy() ipld.Node {
78
copybuf := make([]byte, len(rn.RawData()))
79
copy(copybuf, rn.RawData())
@@ -80,10 +86,12 @@ func (rn *RawNode) Copy() ipld.Node {
86
return &RawNode{nblk}
87
}
88
89
+// Size returns the size of this node
90
func (rn *RawNode) Size() (uint64, error) {
91
return uint64(len(rn.RawData())), nil
92
}
93
94
+// Stat returns some Stats about this node.
95
func (rn *RawNode) Stat() (*ipld.NodeStat, error) {
96
return &ipld.NodeStat{
97
CumulativeSize: len(rn.RawData()),
merkledag/rwservice.go
+6
@@ -16,26 +16,32 @@ type ComboService struct {
16
17
var _ ipld.DAGService = (*ComboService)(nil)
18
19
+// Add writes a new node using the Write DAGService.
20
func (cs *ComboService) Add(ctx context.Context, nd ipld.Node) error {
21
return cs.Write.Add(ctx, nd)
22
}
23
24
+// AddMany adds nodes using the Write DAGService.
25
func (cs *ComboService) AddMany(ctx context.Context, nds []ipld.Node) error {
26
return cs.Write.AddMany(ctx, nds)
27
}
28
29
+// Get fetches a node using the Read DAGService.
30
func (cs *ComboService) Get(ctx context.Context, c *cid.Cid) (ipld.Node, error) {
31
return cs.Read.Get(ctx, c)
32
}
33
34
+// GetMany fetches nodes using the Read DAGService.
35
func (cs *ComboService) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *ipld.NodeOption {
36
return cs.Read.GetMany(ctx, cids)
37
}
38
39
+// Remove deletes a node using the Write DAGService.
40
func (cs *ComboService) Remove(ctx context.Context, c *cid.Cid) error {
41
return cs.Write.Remove(ctx, c)
42
}
43
44
+// RemoveMany deletes nodes using the Write DAGService.
45
func (cs *ComboService) RemoveMany(ctx context.Context, cids []*cid.Cid) error {
46
return cs.Write.RemoveMany(ctx, cids)
47
}
merkledag/traverse/traverse.go
+14
-14
@@ -11,10 +11,14 @@ import (
11
// Order is an identifier for traversal algorithm orders
12
type Order int
13
14
+// These constants define different traversing methods
15
const (
15
- DFSPre Order = iota // depth-first pre-order
16
- DFSPost // depth-first post-order
17
- BFS // breadth-first
16
+ // DFSPre defines depth-first pre-order
17
+ DFSPre Order = iota
18
+ // DFSPost defines depth-first post-order
19
+ DFSPost
20
+ // BFS defines breadth-first order
21
+ BFS
22
)
23
24
// Options specifies a series of traversal options
@@ -86,9 +90,9 @@ func (t *traversal) getNode(link *ipld.Link) (ipld.Node, error) {
90
// If an error is returned, processing stops.
91
type Func func(current State) error
92
89
-// If there is a problem walking to the Node, and ErrFunc is provided, Traverse
90
-// will call ErrFunc with the error encountered. ErrFunc can decide how to handle
91
-// that error, and return an error back to Traversal with how to proceed:
93
+// ErrFunc is provided to handle problems when walking to the Node. Traverse
94
+// will call ErrFunc with the error encountered. ErrFunc can decide how to
95
+// handle that error, and return an error back to Traversal with how to proceed:
96
// * nil - skip the Node and its children, but continue processing
97
// * all other errors halt processing immediately.
98
//
@@ -98,6 +102,8 @@ type Func func(current State) error
102
//
103
type ErrFunc func(err error) error
104
105
+// Traverse initiates a DAG traversal with the given options starting at
106
+// the given root.
107
func Traverse(root ipld.Node, o Options) error {
108
t := traversal{
109
opts: o,
@@ -127,20 +133,14 @@ func dfsPreTraverse(state State, t *traversal) error {
133
if err := t.callFunc(state); err != nil {
134
return err
135
}
130
- if err := dfsDescend(dfsPreTraverse, state, t); err != nil {
131
- return err
132
- }
133
- return nil
136
+ return dfsDescend(dfsPreTraverse, state, t)
137
}
138
139
func dfsPostTraverse(state State, t *traversal) error {
140
if err := dfsDescend(dfsPostTraverse, state, t); err != nil {
141
return err
142
}
140
- if err := t.callFunc(state); err != nil {
141
- return err
142
- }
143
- return nil
143
+ return t.callFunc(state)
144
}
145
146
func dfsDescend(df dfsFunc, curr State, t *traversal) error {
merkledag/utils/diff.go
+16
-6
@@ -11,12 +11,15 @@ import (
11
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
12
)
13
14
+// These constants define the changes that can be applied to a DAG.
15
const (
16
Add = iota
17
Remove
18
Mod
19
)
20
21
+// Change represents a change to a DAG and contains a reference to the old and
22
+// new CIDs.
23
type Change struct {
24
Type int
25
Path string
@@ -24,6 +27,7 @@ type Change struct {
27
After *cid.Cid
28
}
29
30
+// String prints a human-friendly line about a change.
31
func (c *Change) String() string {
32
switch c.Type {
33
case Add:
@@ -102,8 +106,8 @@ func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, e
106
}
107
108
var out []*Change
105
- clean_a := a.Copy().(*dag.ProtoNode)
106
- clean_b := b.Copy().(*dag.ProtoNode)
109
+ cleanA := a.Copy().(*dag.ProtoNode)
110
+ cleanB := b.Copy().(*dag.ProtoNode)
111
112
// strip out unchanged stuff
113
for _, lnk := range a.Links() {
@@ -142,19 +146,19 @@ func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, e
146
out = append(out, subc)
147
}
148
}
145
- clean_a.RemoveNodeLink(l.Name)
146
- clean_b.RemoveNodeLink(l.Name)
149
+ cleanA.RemoveNodeLink(l.Name)
150
+ cleanB.RemoveNodeLink(l.Name)
151
}
152
}
153
150
- for _, lnk := range clean_a.Links() {
154
+ for _, lnk := range cleanA.Links() {
155
out = append(out, &Change{
156
Type: Remove,
157
Path: lnk.Name,
158
Before: lnk.Cid,
159
})
160
}
157
- for _, lnk := range clean_b.Links() {
161
+ for _, lnk := range cleanB.Links() {
162
out = append(out, &Change{
163
Type: Add,
164
Path: lnk.Name,
@@ -165,11 +169,17 @@ func Diff(ctx context.Context, ds ipld.DAGService, a, b ipld.Node) ([]*Change, e
169
return out, nil
170
}
171
172
+// Conflict represents two incompatible changes and is returned by MergeDiffs().
173
type Conflict struct {
174
A *Change
175
B *Change
176
}
177
178
+// MergeDiffs takes two slice of changes and adds them to a single slice.
179
+// When a Change from b happens to the same path of an existing change in a,
180
+// a conflict is created and b is not added to the merged slice.
181
+// A slice of Conflicts is returned and contains pointers to the
182
+// Changes involved (which share the same path).
183
func MergeDiffs(a, b []*Change) ([]*Change, []Conflict) {
184
var out []*Change
185
var conflicts []Conflict
merkledag/utils/utils.go
+5
@@ -15,6 +15,8 @@ import (
15
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
16
)
17
18
+// Editor represents a ProtoNode tree editor and provides methods to
19
+// modify it.
20
type Editor struct {
21
root *dag.ProtoNode
22
@@ -83,6 +85,7 @@ func addLink(ctx context.Context, ds ipld.DAGService, root *dag.ProtoNode, child
85
return root, nil
86
}
87
88
+// InsertNodeAtPath inserts a new node in the tree and replaces the current root with the new one.
89
func (e *Editor) InsertNodeAtPath(ctx context.Context, pth string, toinsert ipld.Node, create func() *dag.ProtoNode) error {
90
splpath := path.SplitList(pth)
91
nd, err := e.insertNodeAtPath(ctx, e.root, splpath, toinsert, create)
@@ -137,6 +140,8 @@ func (e *Editor) insertNodeAtPath(ctx context.Context, root *dag.ProtoNode, path
140
return root, nil
141
}
142
143
+// RmLink removes the link with the given name and updates the root node of
144
+// the editor.
145
func (e *Editor) RmLink(ctx context.Context, pth string) error {
146
splpath := path.SplitList(pth)
147
nd, err := e.rmLink(ctx, e.root, splpath)