@cryptotaxi247 / kubo / commits / 699b2c4bb

make code-climate happier

License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Jan 25, 2018 at 15:10 UTC 699b2c4bb876b6f12a8dc87de3ff268d60bc05f7
15 files changed +62 -18
blockservice/blockservice.go
+10 -4
@@ -22,6 +22,8 @@ var log = logging.Logger("blockservice")
22
23 var ErrNotFound = errors.New("blockservice: key not found")
24
25 +// BlockGetter is the common interface shared between blockservice sessions and
26 +// the blockservice.
27 type BlockGetter interface {
28 // GetBlock gets the requested block.
29 GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error)
@@ -95,12 +97,14 @@ func NewWriteThrough(bs blockstore.Blockstore, rem exchange.Interface) BlockServ
97 }
98 }
99
98 -func (bs *blockService) Blockstore() blockstore.Blockstore {
99 - return bs.blockstore
100 +// Blockstore returns the blockstore behind this blockservice.
101 +func (s *blockService) Blockstore() blockstore.Blockstore {
102 + return s.blockstore
103 }
104
102 -func (bs *blockService) Exchange() exchange.Interface {
103 - return bs.exchange
105 +// Exchange returns the exchange behind this blockservice.
106 +func (s *blockService) Exchange() exchange.Interface {
107 + return s.exchange
108 }
109
110 // NewSession creates a bitswap session that allows for controlled exchange of
@@ -286,3 +290,5 @@ func (s *Session) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error
290 func (s *Session) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block {
291 return getBlocks(ctx, ks, s.bs, s.ses)
292 }
293 +
294 +var _ BlockGetter = (*Session)(nil)
importer/helpers/helpers.go
+7 -4
@@ -65,10 +65,12 @@ func (n *UnixfsNode) SetPrefix(prefix *cid.Prefix) {
65 n.node.SetPrefix(prefix)
66 }
67
68 +// NumChildren returns the number of children referenced by this UnixfsNode.
69 func (n *UnixfsNode) NumChildren() int {
70 return n.ufmt.NumChildren()
71 }
72
73 +// Set replaces this UnixfsNode with another UnixfsNode
74 func (n *UnixfsNode) Set(other *UnixfsNode) {
75 n.node = other.node
76 n.raw = other.raw
@@ -78,6 +80,7 @@ func (n *UnixfsNode) Set(other *UnixfsNode) {
80 }
81 }
82
83 +// GetChild gets the ith child of this node from the given DAGService.
84 func (n *UnixfsNode) GetChild(ctx context.Context, i int, ds node.DAGService) (*UnixfsNode, error) {
85 nd, err := n.node.Links()[i].GetNode(ctx, ds)
86 if err != nil {
@@ -92,8 +95,8 @@ func (n *UnixfsNode) GetChild(ctx context.Context, i int, ds node.DAGService) (*
95 return NewUnixfsNodeFromDag(pbn)
96 }
97
95 -// addChild will add the given UnixfsNode as a child of the receiver.
96 -// the passed in DagBuilderHelper is used to store the child node an
98 +// AddChild adds the given UnixfsNode as a child of the receiver.
99 +// The passed in DagBuilderHelper is used to store the child node an
100 // pin it locally so it doesnt get lost
101 func (n *UnixfsNode) AddChild(child *UnixfsNode, db *DagBuilderHelper) error {
102 n.ufmt.AddBlockSize(child.FileSize())
@@ -115,7 +118,7 @@ func (n *UnixfsNode) AddChild(child *UnixfsNode, db *DagBuilderHelper) error {
118 return err
119 }
120
118 -// Removes the child node at the given index
121 +// RemoveChild removes the child node at the given index
122 func (n *UnixfsNode) RemoveChild(index int, dbh *DagBuilderHelper) {
123 n.ufmt.RemoveBlockSize(index)
124 n.node.SetLinks(append(n.node.Links()[:index], n.node.Links()[index+1:]...))
@@ -140,7 +143,7 @@ func (n *UnixfsNode) SetPosInfo(offset uint64, fullPath string, stat os.FileInfo
143 }
144 }
145
143 -// getDagNode fills out the proper formatting for the unixfs node
146 +// GetDagNode fills out the proper formatting for the unixfs node
147 // inside of a DAG node and returns the dag node
148 func (n *UnixfsNode) GetDagNode() (node.Node, error) {
149 nd, err := n.getBaseDagNode()
importer/importer.go
+7 -4
@@ -1,5 +1,5 @@
1 -// package importer implements utilities used to create IPFS DAGs from files
2 -// and readers
1 +// Package importer implements utilities used to create IPFS DAGs from files
2 +// and readers.
3 package importer
4
5 import (
@@ -15,8 +15,8 @@ import (
15 node "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
16 )
17
18 -// Builds a DAG from the given file, writing created blocks to disk as they are
19 -// created
18 +// BuildDagFromFile builds a DAG from the given file, writing created blocks to
19 +// disk as they are created
20 func BuildDagFromFile(fpath string, ds node.DAGService) (node.Node, error) {
21 stat, err := os.Lstat(fpath)
22 if err != nil {
@@ -36,6 +36,8 @@ func BuildDagFromFile(fpath string, ds node.DAGService) (node.Node, error) {
36 return BuildDagFromReader(ds, chunk.DefaultSplitter(f))
37 }
38
39 +// BuildDagFromReader builds a DAG from the chunks returned by the given chunk
40 +// splitter.
41 func BuildDagFromReader(ds node.DAGService, spl chunk.Splitter) (node.Node, error) {
42 dbp := h.DagBuilderParams{
43 Dagserv: ds,
@@ -45,6 +47,7 @@ func BuildDagFromReader(ds node.DAGService, spl chunk.Splitter) (node.Node, erro
47 return bal.BalancedLayout(dbp.New(spl))
48 }
49
50 +// BuildTrickleDagFromReader is similar to BuildDagFromReader but uses the trickle layout.
51 func BuildTrickleDagFromReader(ds node.DAGService, spl chunk.Splitter) (node.Node, error) {
52 dbp := h.DagBuilderParams{
53 Dagserv: ds,
merkledag/node.go
+4 -2
@@ -124,7 +124,7 @@ func (n *ProtoNode) AddRawLink(name string, l *node.Link) error {
124 return nil
125 }
126
127 -// Remove a link on this node by the given name
127 +// RemoveNodeLink removes a link on this node by the given name.
128 func (n *ProtoNode) RemoveNodeLink(name string) error {
129 n.encoded = nil
130 good := make([]*node.Link, 0, len(n.links))
@@ -146,7 +146,7 @@ func (n *ProtoNode) RemoveNodeLink(name string) error {
146 return nil
147 }
148
149 -// Return a copy of the link with given name
149 +// GetNodeLink returns a copy of the link with the given name.
150 func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
151 for _, l := range n.links {
152 if l.Name == name {
@@ -160,6 +160,7 @@ func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
160 return nil, ErrLinkNotFound
161 }
162
163 +// GetLinkedProtoNode returns a copy of the ProtoNode with the given name.
164 func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds node.DAGService, name string) (*ProtoNode, error) {
165 nd, err := n.GetLinkedNode(ctx, ds, name)
166 if err != nil {
@@ -174,6 +175,7 @@ func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds node.DAGService,
175 return pbnd, nil
176 }
177
178 +// GetLinkedNode returns a copy of the IPLD Node with the given name.
179 func (n *ProtoNode) GetLinkedNode(ctx context.Context, ds node.DAGService, name string) (node.Node, error) {
180 lnk, err := n.GetNodeLink(name)
181 if err != nil {
merkledag/test/utils.go
+2
@@ -11,10 +11,12 @@ import (
11 node "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
12 )
13
14 +// Mock returns a new thread-safe, mock DAGService.
15 func Mock() node.DAGService {
16 return dag.NewDAGService(Bserv())
17 }
18
19 +// Bserv returns a new, thread-safe, mock BlockService.
20 func Bserv() bsrv.BlockService {
21 bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
22 return bsrv.New(bstore, offline.Exchange(bstore))
merkledag/utils/diff.go
+1
@@ -37,6 +37,7 @@ func (c *Change) String() string {
37 }
38 }
39
40 +// ApplyChange applies the requested changes to the given node in the given dag.
41 func ApplyChange(ctx context.Context, ds node.DAGService, nd *dag.ProtoNode, cs []*Change) (*dag.ProtoNode, error) {
42 e := NewDagEditor(nd, ds)
43 for _, c := range cs {
merkledag/utils/utils.go
+10 -2
@@ -27,6 +27,7 @@ type Editor struct {
27 src node.DAGService
28 }
29
30 +// NewMemoryDagService returns a new, thread-safe in-memory DAGService.
31 func NewMemoryDagService() node.DAGService {
32 // build mem-datastore for editor's intermediary nodes
33 bs := bstore.NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))
@@ -34,7 +35,10 @@ func NewMemoryDagService() node.DAGService {
35 return dag.NewDAGService(bsrv)
36 }
37
37 -// root is the node to be modified, source is the dagstore to pull nodes from (optional)
38 +// NewDagEditor returns an ProtoNode editor.
39 +//
40 +// * root is the node to be modified
41 +// * source is the dagstore to pull nodes from (optional)
42 func NewDagEditor(root *dag.ProtoNode, source node.DAGService) *Editor {
43 return &Editor{
44 root: root,
@@ -43,17 +47,19 @@ func NewDagEditor(root *dag.ProtoNode, source node.DAGService) *Editor {
47 }
48 }
49
50 +// GetNode returns the a copy of the root node being edited.
51 func (e *Editor) GetNode() *dag.ProtoNode {
52 return e.root.Copy().(*dag.ProtoNode)
53 }
54
55 +// GetDagService returns the DAGService used by this editor.
56 func (e *Editor) GetDagService() node.DAGService {
57 return e.tmp
58 }
59
60 func addLink(ctx context.Context, ds node.DAGService, root *dag.ProtoNode, childname string, childnd node.Node) (*dag.ProtoNode, error) {
61 if childname == "" {
56 - return nil, errors.New("cannot create link with no name!")
62 + return nil, errors.New("cannot create link with no name")
63 }
64
65 // ensure that the node we are adding is in the dagservice
@@ -188,6 +194,8 @@ func (e *Editor) rmLink(ctx context.Context, root *dag.ProtoNode, path []string)
194 return root, nil
195 }
196
197 +// Finalize writes the new DAG to the given DAGService and returns the modified
198 +// root node.
199 func (e *Editor) Finalize(ctx context.Context, ds node.DAGService) (*dag.ProtoNode, error) {
200 nd := e.GetNode()
201 err := copyDag(ctx, nd, e.tmp, ds)
merkledag/utils/utils_test.go
+2 -2
@@ -70,8 +70,8 @@ func TestInsertNode(t *testing.T) {
70 testInsert(t, e, "a/b/c/d/f", "baz", true, "")
71 testInsert(t, e, "a/b/c/d/f", "bar", true, "")
72
73 - testInsert(t, e, "", "bar", true, "cannot create link with no name!")
74 - testInsert(t, e, "////", "slashes", true, "cannot create link with no name!")
73 + testInsert(t, e, "", "bar", true, "cannot create link with no name")
74 + testInsert(t, e, "////", "slashes", true, "cannot create link with no name")
75
76 c := e.GetNode().Cid()
77
mfs/dir.go
+4
@@ -40,6 +40,10 @@ type Directory struct {
40 name string
41 }
42
43 +// NewDirectory constructs a new MFS directory.
44 +//
45 +// You probably don't want to call this directly. Instead, construct a new root
46 +// using NewRoot.
47 func NewDirectory(ctx context.Context, name string, node node.Node, parent childCloser, dserv node.DAGService) (*Directory, error) {
48 db, err := uio.NewDirectoryFromNode(dserv, node)
49 if err != nil {
path/resolver.go
+1
@@ -40,6 +40,7 @@ type Resolver struct {
40 ResolveOnce func(ctx context.Context, ds node.DAGService, nd node.Node, names []string) (*node.Link, []string, error)
41 }
42
43 +// NewBasicResolver constructs a new basic resolver.
44 func NewBasicResolver(ds node.DAGService) *Resolver {
45 return &Resolver{
46 DAG: ds,
tar/format.go
+4
@@ -34,6 +34,8 @@ func marshalHeader(h *tar.Header) ([]byte, error) {
34 return buf.Bytes(), nil
35 }
36
37 +// ImportTar imports a tar file into the given DAGService and returns the root
38 +// node.
39 func ImportTar(ctx context.Context, r io.Reader, ds node.DAGService) (*dag.ProtoNode, error) {
40 tr := tar.NewReader(r)
41
@@ -194,6 +196,8 @@ func (tr *tarReader) Read(b []byte) (int, error) {
196 return tr.Read(b)
197 }
198
199 +// ExportTar exports the passed DAG as a tar file. This function is the inverse
200 +// of ImportTar.
201 func ExportTar(ctx context.Context, root *dag.ProtoNode, ds node.DAGService) (io.Reader, error) {
202 if string(root.Data()) != "ipfs/tar" {
203 return nil, errors.New("not an IPFS tarchive")
unixfs/hamt/hamt.go
+2
@@ -66,6 +66,7 @@ type child interface {
66 Label() string
67 }
68
69 +// NewHamtShard creates a new, empty HAMT shard with the given size.
70 func NewHamtShard(dserv node.DAGService, size int) (*HamtShard, error) {
71 ds, err := makeHamtShard(dserv, size)
72 if err != nil {
@@ -93,6 +94,7 @@ func makeHamtShard(ds node.DAGService, size int) (*HamtShard, error) {
94 }, nil
95 }
96
97 +// NewHamtFromDag creates new a HAMT shard from the given DAG.
98 func NewHamtFromDag(dserv node.DAGService, nd node.Node) (*HamtShard, error) {
99 pbnd, ok := nd.(*dag.ProtoNode)
100 if !ok {
unixfs/io/dirbuilder.go
+2
@@ -51,6 +51,8 @@ func NewDirectory(dserv node.DAGService) *Directory {
51 // ErrNotADir implies that the given node was not a unixfs directory
52 var ErrNotADir = fmt.Errorf("merkledag node was not a directory or shard")
53
54 +// NewDirectoryFromNode loads a unixfs directory from the given IPLD node and
55 +// DAGService.
56 func NewDirectoryFromNode(dserv node.DAGService, nd node.Node) (*Directory, error) {
57 pbnd, ok := nd.(*mdag.ProtoNode)
58 if !ok {
unixfs/io/pbdagreader.go
+1
@@ -50,6 +50,7 @@ type pbDagReader struct {
50
51 var _ DagReader = (*pbDagReader)(nil)
52
53 +// NewPBFileReader constructs a new PBFileReader.
54 func NewPBFileReader(ctx context.Context, n *mdag.ProtoNode, pb *ftpb.Data, serv node.DAGService) *pbDagReader {
55 fctx, cancel := context.WithCancel(ctx)
56 curLinks := getLinkCids(n)
unixfs/test/utils.go
+5
@@ -27,6 +27,7 @@ func SizeSplitterGen(size int64) chunk.SplitterGen {
27 }
28 }
29
30 +// GetDAGServ returns a mock DAGService.
31 func GetDAGServ() node.DAGService {
32 return mdagmock.Mock()
33 }
@@ -51,6 +52,7 @@ func init() {
52 UseBlake2b256.Prefix.MhLength = -1
53 }
54
55 +// GetNode returns a unixfs file node with the specified data.
56 func GetNode(t testing.TB, dserv node.DAGService, data []byte, opts NodeOpts) node.Node {
57 in := bytes.NewReader(data)
58
@@ -69,10 +71,12 @@ func GetNode(t testing.TB, dserv node.DAGService, data []byte, opts NodeOpts) no
71 return node
72 }
73
74 +// GetEmptyNode returns an empty unixfs file node.
75 func GetEmptyNode(t testing.TB, dserv node.DAGService, opts NodeOpts) node.Node {
76 return GetNode(t, dserv, []byte{}, opts)
77 }
78
79 +// GetRandomNode returns a random unixfs file node.
80 func GetRandomNode(t testing.TB, dserv node.DAGService, size int64, opts NodeOpts) ([]byte, node.Node) {
81 in := io.LimitReader(u.NewTimeSeededRand(), size)
82 buf, err := ioutil.ReadAll(in)
@@ -96,6 +100,7 @@ func ArrComp(a, b []byte) error {
100 return nil
101 }
102
103 +// PrintDag pretty-prints the given dag to stdout.
104 func PrintDag(nd *mdag.ProtoNode, ds node.DAGService, indent int) {
105 pbd, err := ft.FromBytes(nd.Data())
106 if err != nil {