diff --git a/blockservice/blockservice.go b/blockservice/blockservice.go index d4f6fffadecd..3969dad69b13 100644 --- a/blockservice/blockservice.go +++ b/blockservice/blockservice.go @@ -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) @@ -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 @@ -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) diff --git a/importer/helpers/helpers.go b/importer/helpers/helpers.go index dbd994c2c685..64b91d38ba4c 100644 --- a/importer/helpers/helpers.go +++ b/importer/helpers/helpers.go @@ -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 @@ -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 { @@ -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()) @@ -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:]...)) @@ -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() diff --git a/importer/importer.go b/importer/importer.go index 8a07df3df12d..f209f3cc5732 100644 --- a/importer/importer.go +++ b/importer/importer.go @@ -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 ( @@ -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 { @@ -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, @@ -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, diff --git a/merkledag/node.go b/merkledag/node.go index 143e7e07525d..4cb79ca9c33f 100644 --- a/merkledag/node.go +++ b/merkledag/node.go @@ -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)) @@ -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 { @@ -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 { @@ -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 { diff --git a/merkledag/utils/diff.go b/merkledag/utils/diff.go index 2ead4bdf761a..5dfd37fb75b8 100644 --- a/merkledag/utils/diff.go +++ b/merkledag/utils/diff.go @@ -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 { diff --git a/merkledag/utils/utils.go b/merkledag/utils/utils.go index dd119834da12..eb4413f155bb 100644 --- a/merkledag/utils/utils.go +++ b/merkledag/utils/utils.go @@ -27,6 +27,7 @@ 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())) @@ -34,7 +35,10 @@ func NewMemoryDagService() node.DAGService { 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, @@ -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 @@ -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) diff --git a/mfs/dir.go b/mfs/dir.go index 5ad39d20503a..901f69952f15 100644 --- a/mfs/dir.go +++ b/mfs/dir.go @@ -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 { diff --git a/path/resolver.go b/path/resolver.go index 71ac52ba0f3d..68865283addb 100644 --- a/path/resolver.go +++ b/path/resolver.go @@ -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, diff --git a/tar/format.go b/tar/format.go index 327d1fd2cba1..e1426fdad1f7 100644 --- a/tar/format.go +++ b/tar/format.go @@ -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) diff --git a/unixfs/hamt/hamt.go b/unixfs/hamt/hamt.go index 2974d4928059..e0b063e4f7b6 100644 --- a/unixfs/hamt/hamt.go +++ b/unixfs/hamt/hamt.go @@ -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 { @@ -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 { diff --git a/unixfs/io/dirbuilder.go b/unixfs/io/dirbuilder.go index bc9eb6c8e467..cc3a3905cebb 100644 --- a/unixfs/io/dirbuilder.go +++ b/unixfs/io/dirbuilder.go @@ -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 { diff --git a/unixfs/io/pbdagreader.go b/unixfs/io/pbdagreader.go index 8f0f22d3b098..85cbc19688cb 100644 --- a/unixfs/io/pbdagreader.go +++ b/unixfs/io/pbdagreader.go @@ -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) diff --git a/unixfs/test/utils.go b/unixfs/test/utils.go index 8b234c9ad4f2..1e44b1e9c789 100644 --- a/unixfs/test/utils.go +++ b/unixfs/test/utils.go @@ -27,6 +27,7 @@ func SizeSplitterGen(size int64) chunk.SplitterGen { } } +// GetDAGServ returns a mock DAGService. func GetDAGServ() node.DAGService { return mdagmock.Mock() } @@ -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) @@ -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) @@ -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 {