Skip to content

Commit

Permalink
make code-climate happier
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Steven Allen <[email protected]>
  • Loading branch information
Stebalien committed Jan 25, 2018
1 parent 0d12a97 commit e638bc6
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 16 deletions.
14 changes: 10 additions & 4 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var log = logging.Logger("blockservice")

var ErrNotFound = errors.New("blockservice: key not found")

// BlockGetter is the common interface shared between blockservice sessions and
// the blockservice.
type BlockGetter interface {
// GetBlock gets the requested block.
GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error)
Expand Down Expand Up @@ -95,12 +97,14 @@ func NewWriteThrough(bs blockstore.Blockstore, rem exchange.Interface) BlockServ
}
}

func (bs *blockService) Blockstore() blockstore.Blockstore {
return bs.blockstore
// Blockstore returns the blockstore behind this blockservice.
func (s *blockService) Blockstore() blockstore.Blockstore {
return s.blockstore
}

func (bs *blockService) Exchange() exchange.Interface {
return bs.exchange
// Exchange returns the exchange behind this blockservice.
func (s *blockService) Exchange() exchange.Interface {
return s.exchange
}

// NewSession creates a bitswap session that allows for controlled exchange of
Expand Down Expand Up @@ -286,3 +290,5 @@ func (s *Session) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error
func (s *Session) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block {
return getBlocks(ctx, ks, s.bs, s.ses)
}

var _ BlockGetter = (*Session)(nil)
11 changes: 7 additions & 4 deletions importer/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ func (n *UnixfsNode) SetPrefix(prefix *cid.Prefix) {
n.node.SetPrefix(prefix)
}

// NumChildren returns the number of children referenced by this UnixfsNode.
func (n *UnixfsNode) NumChildren() int {
return n.ufmt.NumChildren()
}

// Set replaces this UnixfsNode with another UnixfsNode
func (n *UnixfsNode) Set(other *UnixfsNode) {
n.node = other.node
n.raw = other.raw
Expand All @@ -78,6 +80,7 @@ func (n *UnixfsNode) Set(other *UnixfsNode) {
}
}

// GetChild gets the ith child of this node from the given DAGService.
func (n *UnixfsNode) GetChild(ctx context.Context, i int, ds node.DAGService) (*UnixfsNode, error) {
nd, err := n.node.Links()[i].GetNode(ctx, ds)
if err != nil {
Expand All @@ -92,8 +95,8 @@ func (n *UnixfsNode) GetChild(ctx context.Context, i int, ds node.DAGService) (*
return NewUnixfsNodeFromDag(pbn)
}

// addChild will add the given UnixfsNode as a child of the receiver.
// the passed in DagBuilderHelper is used to store the child node an
// AddChild adds the given UnixfsNode as a child of the receiver.
// The passed in DagBuilderHelper is used to store the child node an
// pin it locally so it doesnt get lost
func (n *UnixfsNode) AddChild(child *UnixfsNode, db *DagBuilderHelper) error {
n.ufmt.AddBlockSize(child.FileSize())
Expand All @@ -115,7 +118,7 @@ func (n *UnixfsNode) AddChild(child *UnixfsNode, db *DagBuilderHelper) error {
return err
}

// Removes the child node at the given index
// RemoveChild removes the child node at the given index
func (n *UnixfsNode) RemoveChild(index int, dbh *DagBuilderHelper) {
n.ufmt.RemoveBlockSize(index)
n.node.SetLinks(append(n.node.Links()[:index], n.node.Links()[index+1:]...))
Expand All @@ -140,7 +143,7 @@ func (n *UnixfsNode) SetPosInfo(offset uint64, fullPath string, stat os.FileInfo
}
}

// getDagNode fills out the proper formatting for the unixfs node
// GetDagNode fills out the proper formatting for the unixfs node
// inside of a DAG node and returns the dag node
func (n *UnixfsNode) GetDagNode() (node.Node, error) {
nd, err := n.getBaseDagNode()
Expand Down
11 changes: 7 additions & 4 deletions importer/importer.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// package importer implements utilities used to create IPFS DAGs from files
// and readers
// Package importer implements utilities used to create IPFS DAGs from files
// and readers.
package importer

import (
Expand All @@ -15,8 +15,8 @@ import (
node "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)

// Builds a DAG from the given file, writing created blocks to disk as they are
// created
// BuildDagFromFile builds a DAG from the given file, writing created blocks to
// disk as they are created
func BuildDagFromFile(fpath string, ds node.DAGService) (node.Node, error) {
stat, err := os.Lstat(fpath)
if err != nil {
Expand All @@ -36,6 +36,8 @@ func BuildDagFromFile(fpath string, ds node.DAGService) (node.Node, error) {
return BuildDagFromReader(ds, chunk.DefaultSplitter(f))
}

// BuildDagFromReader builds a DAG from the chunks returned by the given chunk
// splitter.
func BuildDagFromReader(ds node.DAGService, spl chunk.Splitter) (node.Node, error) {
dbp := h.DagBuilderParams{
Dagserv: ds,
Expand All @@ -45,6 +47,7 @@ func BuildDagFromReader(ds node.DAGService, spl chunk.Splitter) (node.Node, erro
return bal.BalancedLayout(dbp.New(spl))
}

// BuildTrickleDagFromReader is similar to BuildDagFromReader but uses the trickle layout.
func BuildTrickleDagFromReader(ds node.DAGService, spl chunk.Splitter) (node.Node, error) {
dbp := h.DagBuilderParams{
Dagserv: ds,
Expand Down
6 changes: 4 additions & 2 deletions merkledag/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (n *ProtoNode) AddRawLink(name string, l *node.Link) error {
return nil
}

// Remove a link on this node by the given name
// RemoveNodeLink removes a link on this node by the given name.
func (n *ProtoNode) RemoveNodeLink(name string) error {
n.encoded = nil
good := make([]*node.Link, 0, len(n.links))
Expand All @@ -146,7 +146,7 @@ func (n *ProtoNode) RemoveNodeLink(name string) error {
return nil
}

// Return a copy of the link with given name
// GetNodeLink returns a copy of the link with the given name.
func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
for _, l := range n.links {
if l.Name == name {
Expand All @@ -160,6 +160,7 @@ func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
return nil, ErrLinkNotFound
}

// GetLinkedProtoNode returns a copy of the ProtoNode with the given name.
func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds node.DAGService, name string) (*ProtoNode, error) {
nd, err := n.GetLinkedNode(ctx, ds, name)
if err != nil {
Expand All @@ -174,6 +175,7 @@ func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds node.DAGService,
return pbnd, nil
}

// GetLinkedNode returns a copy of the IPLD Node with the given name.
func (n *ProtoNode) GetLinkedNode(ctx context.Context, ds node.DAGService, name string) (node.Node, error) {
lnk, err := n.GetNodeLink(name)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions merkledag/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
node "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)

// Mock returns a new thread-safe, mock DAGService.
func Mock() node.DAGService {
return dag.NewDAGService(Bserv())
}

// Bserv returns a new, thread-safe, mock BlockService.
func Bserv() bsrv.BlockService {
bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
return bsrv.New(bstore, offline.Exchange(bstore))
Expand Down
1 change: 1 addition & 0 deletions merkledag/utils/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Change) String() string {
}
}

// ApplyChange applies the requested changes to the given node in the given dag.
func ApplyChange(ctx context.Context, ds node.DAGService, nd *dag.ProtoNode, cs []*Change) (*dag.ProtoNode, error) {
e := NewDagEditor(nd, ds)
for _, c := range cs {
Expand Down
12 changes: 10 additions & 2 deletions merkledag/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ type Editor struct {
src node.DAGService
}

// NewMemoryDagService returns a new, thread-safe in-memory DAGService.
func NewMemoryDagService() node.DAGService {
// build mem-datastore for editor's intermediary nodes
bs := bstore.NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))
bsrv := bserv.New(bs, offline.Exchange(bs))
return dag.NewDAGService(bsrv)
}

// root is the node to be modified, source is the dagstore to pull nodes from (optional)
// NewDagEditor returns an ProtoNode editor.
//
// * root is the node to be modified
// * source is the dagstore to pull nodes from (optional)
func NewDagEditor(root *dag.ProtoNode, source node.DAGService) *Editor {
return &Editor{
root: root,
Expand All @@ -43,17 +47,19 @@ func NewDagEditor(root *dag.ProtoNode, source node.DAGService) *Editor {
}
}

// GetNode returns the a copy of the root node being edited.
func (e *Editor) GetNode() *dag.ProtoNode {
return e.root.Copy().(*dag.ProtoNode)
}

// GetDagService returns the DAGService used by this editor.
func (e *Editor) GetDagService() node.DAGService {
return e.tmp
}

func addLink(ctx context.Context, ds node.DAGService, root *dag.ProtoNode, childname string, childnd node.Node) (*dag.ProtoNode, error) {
if childname == "" {
return nil, errors.New("cannot create link with no name!")
return nil, errors.New("cannot create link with no name")
}

// ensure that the node we are adding is in the dagservice
Expand Down Expand Up @@ -188,6 +194,8 @@ func (e *Editor) rmLink(ctx context.Context, root *dag.ProtoNode, path []string)
return root, nil
}

// Finalize writes the new DAG to the given DAGService and returns the modified
// root node.
func (e *Editor) Finalize(ctx context.Context, ds node.DAGService) (*dag.ProtoNode, error) {
nd := e.GetNode()
err := copyDag(ctx, nd, e.tmp, ds)
Expand Down
4 changes: 4 additions & 0 deletions mfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type Directory struct {
name string
}

// NewDirectory constructs a new MFS directory.
//
// You probably don't want to call this directly. Instead, construct a new root
// using NewRoot.
func NewDirectory(ctx context.Context, name string, node node.Node, parent childCloser, dserv node.DAGService) (*Directory, error) {
db, err := uio.NewDirectoryFromNode(dserv, node)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions path/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Resolver struct {
ResolveOnce func(ctx context.Context, ds node.DAGService, nd node.Node, names []string) (*node.Link, []string, error)
}

// NewBasicResolver constructs a new basic resolver.
func NewBasicResolver(ds node.DAGService) *Resolver {
return &Resolver{
DAG: ds,
Expand Down
4 changes: 4 additions & 0 deletions tar/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func marshalHeader(h *tar.Header) ([]byte, error) {
return buf.Bytes(), nil
}

// ImportTar imports a tar file into the given DAGService and returns the root
// node.
func ImportTar(ctx context.Context, r io.Reader, ds node.DAGService) (*dag.ProtoNode, error) {
tr := tar.NewReader(r)

Expand Down Expand Up @@ -194,6 +196,8 @@ func (tr *tarReader) Read(b []byte) (int, error) {
return tr.Read(b)
}

// ExportTar exports the passed DAG as a tar file. This function is the inverse
// of ImportTar.
func ExportTar(ctx context.Context, root *dag.ProtoNode, ds node.DAGService) (io.Reader, error) {
if string(root.Data()) != "ipfs/tar" {
return nil, errors.New("not an IPFS tarchive")
Expand Down
2 changes: 2 additions & 0 deletions unixfs/hamt/hamt.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type child interface {
Label() string
}

// NewHamtShard creates a new, empty HAMT shard with the given size.
func NewHamtShard(dserv node.DAGService, size int) (*HamtShard, error) {
ds, err := makeHamtShard(dserv, size)
if err != nil {
Expand Down Expand Up @@ -93,6 +94,7 @@ func makeHamtShard(ds node.DAGService, size int) (*HamtShard, error) {
}, nil
}

// NewHamtFromDag creates new a HAMT shard from the given DAG.
func NewHamtFromDag(dserv node.DAGService, nd node.Node) (*HamtShard, error) {
pbnd, ok := nd.(*dag.ProtoNode)
if !ok {
Expand Down
2 changes: 2 additions & 0 deletions unixfs/io/dirbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func NewDirectory(dserv node.DAGService) *Directory {
// ErrNotADir implies that the given node was not a unixfs directory
var ErrNotADir = fmt.Errorf("merkledag node was not a directory or shard")

// NewDirectoryFromNode loads a unixfs directory from the given IPLD node and
// DAGService.
func NewDirectoryFromNode(dserv node.DAGService, nd node.Node) (*Directory, error) {
pbnd, ok := nd.(*mdag.ProtoNode)
if !ok {
Expand Down
1 change: 1 addition & 0 deletions unixfs/io/pbdagreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type pbDagReader struct {

var _ DagReader = (*pbDagReader)(nil)

// NewPBFileReader constructs a new PBFileReader.
func NewPBFileReader(ctx context.Context, n *mdag.ProtoNode, pb *ftpb.Data, serv node.DAGService) *pbDagReader {
fctx, cancel := context.WithCancel(ctx)
curLinks := getLinkCids(n)
Expand Down
5 changes: 5 additions & 0 deletions unixfs/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func SizeSplitterGen(size int64) chunk.SplitterGen {
}
}

// GetDAGServ returns a mock DAGService.
func GetDAGServ() node.DAGService {
return mdagmock.Mock()
}
Expand All @@ -51,6 +52,7 @@ func init() {
UseBlake2b256.Prefix.MhLength = -1
}

// GetNode returns a unixfs file node with the specified data.
func GetNode(t testing.TB, dserv node.DAGService, data []byte, opts NodeOpts) node.Node {
in := bytes.NewReader(data)

Expand All @@ -69,10 +71,12 @@ func GetNode(t testing.TB, dserv node.DAGService, data []byte, opts NodeOpts) no
return node
}

// GetEmptyNode returns an empty unixfs file node.
func GetEmptyNode(t testing.TB, dserv node.DAGService, opts NodeOpts) node.Node {
return GetNode(t, dserv, []byte{}, opts)
}

// GetRandomNode returns a random unixfs file node.
func GetRandomNode(t testing.TB, dserv node.DAGService, size int64, opts NodeOpts) ([]byte, node.Node) {
in := io.LimitReader(u.NewTimeSeededRand(), size)
buf, err := ioutil.ReadAll(in)
Expand All @@ -96,6 +100,7 @@ func ArrComp(a, b []byte) error {
return nil
}

// PrintDag pretty-prints the given dag to stdout.
func PrintDag(nd *mdag.ProtoNode, ds node.DAGService, indent int) {
pbd, err := ft.FromBytes(nd.Data())
if err != nil {
Expand Down

0 comments on commit e638bc6

Please sign in to comment.