Skip to content

Commit cfdcd98

Browse files
Merge pull request #4599 from ipfs/feat/doc-interfaces
Feat/doc interfaces
2 parents 34fb6d0 + 83ccc7e commit cfdcd98

File tree

17 files changed

+184
-25
lines changed

17 files changed

+184
-25
lines changed

blocks/blockstore/blockstore.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,19 @@ type Blockstore interface {
4040
DeleteBlock(*cid.Cid) error
4141
Has(*cid.Cid) (bool, error)
4242
Get(*cid.Cid) (blocks.Block, error)
43+
44+
// Put puts a given block to the underlying datastore
4345
Put(blocks.Block) error
46+
47+
// PutMany puts a slice of blocks at the same time using batching
48+
// capabilities of the underlying datastore whenever possible.
4449
PutMany([]blocks.Block) error
50+
4551
// AllKeysChan returns a channel from which
4652
// the CIDs in the Blockstore can be read. It should respect
4753
// the given context, closing the channel if it becomes Done.
4854
AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
55+
4956
// HashOnRead specifies if every read block should be
5057
// rehashed to make sure it matches its CID.
5158
HashOnRead(enabled bool)

blockservice/blockservice.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,26 @@ var ErrNotFound = errors.New("blockservice: key not found")
2525
// datastore and may retrieve data from a remote Exchange.
2626
// It uses an internal `datastore.Datastore` instance to store values.
2727
type BlockService interface {
28+
// Blockstore returns a reference to the underlying blockstore
2829
Blockstore() blockstore.Blockstore
30+
31+
// Exchange returns a reference to the underlying exchange (usually bitswap)
2932
Exchange() exchange.Interface
33+
34+
// AddBlock puts a given block to the underlying datastore
3035
AddBlock(o blocks.Block) (*cid.Cid, error)
36+
37+
// AddBlocks adds a slice of blocks at the same time using batching
38+
// capabilities of the underlying datastore whenever possible.
3139
AddBlocks(bs []blocks.Block) ([]*cid.Cid, error)
40+
3241
GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error)
33-
GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block
3442
DeleteBlock(o blocks.Block) error
43+
44+
// GetBlocks does a batch request for the given cids, returning blocks as
45+
// they are found, in no particular order.
46+
GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block
47+
3548
Close() error
3649
}
3750

core/builder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func defaultRepo(dstore repo.Datastore) (repo.Repo, error) {
115115
}, nil
116116
}
117117

118+
// NewNode constructs and returns an IpfsNode using the given cfg.
118119
func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
119120
if cfg == nil {
120121
cfg = new(BuildCfg)

core/core.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption {
420420
return nil
421421
}
422422

423+
// HandlePeerFound attempts to connect to peer from `PeerInfo`, if it fails
424+
// logs a warning log.
423425
func (n *IpfsNode) HandlePeerFound(p pstore.PeerInfo) {
424426
log.Warning("trying peer info: ", p)
425427
ctx, cancel := context.WithTimeout(n.Context(), discoveryConnTimeout)
@@ -590,6 +592,7 @@ func (n *IpfsNode) teardown() error {
590592
return nil
591593
}
592594

595+
// OnlineMode returns whether or not the IpfsNode is in OnlineMode.
593596
func (n *IpfsNode) OnlineMode() bool {
594597
switch n.mode {
595598
case onlineMode:
@@ -599,13 +602,15 @@ func (n *IpfsNode) OnlineMode() bool {
599602
}
600603
}
601604

605+
// SetLocal will set the IpfsNode to local mode
602606
func (n *IpfsNode) SetLocal(isLocal bool) {
603607
if isLocal {
604608
n.mode = localMode
605609
}
606610
n.localModeSet = true
607611
}
608612

613+
// LocalMode returns whether or not the IpfsNode is in LocalMode
609614
func (n *IpfsNode) LocalMode() bool {
610615
if !n.localModeSet {
611616
// programmer error should not happen
@@ -619,6 +624,7 @@ func (n *IpfsNode) LocalMode() bool {
619624
}
620625
}
621626

627+
// Bootstrap will set and call the IpfsNodes bootstrap function.
622628
func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {
623629

624630
// TODO what should return value be when in offlineMode?
@@ -670,6 +676,7 @@ func (n *IpfsNode) loadID() error {
670676
return nil
671677
}
672678

679+
// GetKey will return a key from the Keystore with name `name`.
673680
func (n *IpfsNode) GetKey(name string) (ic.PrivKey, error) {
674681
if name == "self" {
675682
return n.PrivateKey, nil

core/coreapi/coreapi.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,34 @@ type CoreAPI struct {
1515
node *core.IpfsNode
1616
}
1717

18-
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node
18+
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
1919
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
2020
api := &CoreAPI{n}
2121
return api
2222
}
2323

24+
// Unixfs returns the UnixfsAPI interface backed by the go-ipfs node
2425
func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
2526
return (*UnixfsAPI)(api)
2627
}
2728

29+
// Dag returns the DagAPI interface backed by the go-ipfs node
2830
func (api *CoreAPI) Dag() coreiface.DagAPI {
2931
return &DagAPI{api, nil}
3032
}
3133

34+
// Name returns the NameAPI interface backed by the go-ipfs node
3235
func (api *CoreAPI) Name() coreiface.NameAPI {
3336
return &NameAPI{api, nil}
3437
}
3538

39+
// Key returns the KeyAPI interface backed by the go-ipfs node
3640
func (api *CoreAPI) Key() coreiface.KeyAPI {
3741
return &KeyAPI{api, nil}
3842
}
3943

44+
// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
45+
// resolved Node.
4046
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) {
4147
p, err := api.ResolvePath(ctx, p)
4248
if err != nil {
@@ -50,6 +56,8 @@ func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreifac
5056
return node, nil
5157
}
5258

59+
// ResolvePath resolves the path `p` using Unixfs resolver, returns the
60+
// resolved path.
5361
// TODO: store all of ipfspath.Resolver.ResolvePathComponents() in Path
5462
func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.Path, error) {
5563
if p.Resolved() {
@@ -84,6 +92,7 @@ type path struct {
8492
root *cid.Cid
8593
}
8694

95+
// ParsePath parses path `p` using ipfspath parser, returns the parsed path.
8796
func ParsePath(p string) (coreiface.Path, error) {
8897
pp, err := ipfspath.ParsePath(p)
8998
if err != nil {
@@ -92,10 +101,12 @@ func ParsePath(p string) (coreiface.Path, error) {
92101
return &path{path: pp}, nil
93102
}
94103

104+
// ParseCid parses the path from `c`, retruns the parsed path.
95105
func ParseCid(c *cid.Cid) coreiface.Path {
96106
return &path{path: ipfspath.FromCid(c), cid: c, root: c}
97107
}
98108

109+
// ResolvePath parses path from string `p`, returns parsed path.
99110
func ResolvedPath(p string, c *cid.Cid, r *cid.Cid) coreiface.Path {
100111
return &path{path: ipfspath.FromString(p), cid: c, root: r}
101112
}

core/coreapi/dag.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ type DagAPI struct {
1919
*caopts.DagOptions
2020
}
2121

22+
// Put inserts data using specified format and input encoding. Unless used with
23+
// `WithCodes` or `WithHash`, the defaults "dag-cbor" and "sha256" are used.
24+
// Returns the path of the inserted data.
2225
func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.Path, error) {
2326
settings, err := caopts.DagPutOptions(opts...)
2427
if err != nil {
@@ -46,10 +49,12 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut
4649
return ParseCid(nds[0].Cid()), nil
4750
}
4851

52+
// Get resolves `path` using Unixfs resolver, returns the resolved Node.
4953
func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
5054
return api.core().ResolveNode(ctx, path)
5155
}
5256

57+
// Tree returns list of paths within a node specified by the path `p`.
5358
func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.DagTreeOption) ([]coreiface.Path, error) {
5459
settings, err := caopts.DagTreeOptions(opts...)
5560
if err != nil {

core/coreapi/interface/interface.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ import (
1717
// Path is a generic wrapper for paths used in the API. A path can be resolved
1818
// to a CID using one of Resolve functions in the API.
1919
type Path interface {
20+
// String returns the path as a string.
2021
String() string
22+
// Cid returns cid referred to by path
2123
Cid() *cid.Cid
24+
// Root returns cid of root path
2225
Root() *cid.Cid
26+
// Resolved returns whether path has been fully resolved
2327
Resolved() bool
2428
}
2529

@@ -33,22 +37,31 @@ type Reader interface {
3337
io.Closer
3438
}
3539

40+
// IpnsEntry specifies the interface to IpnsEntries
3641
type IpnsEntry interface {
42+
// Name returns IpnsEntry name
3743
Name() string
44+
// Value returns IpnsEntry value
3845
Value() Path
3946
}
4047

48+
// Key specifies the interface to Keys in KeyAPI Keystore
4149
type Key interface {
50+
// Key returns key name
4251
Name() string
52+
// Path returns key path
4353
Path() Path
4454
}
4555

4656
// CoreAPI defines an unified interface to IPFS for Go programs.
4757
type CoreAPI interface {
48-
// Unixfs returns an implementation of Unixfs API
58+
// Unixfs returns an implementation of Unixfs API.
4959
Unixfs() UnixfsAPI
60+
// Dag returns an implementation of Dag API.
5061
Dag() DagAPI
62+
// Name returns an implementation of Name API.
5163
Name() NameAPI
64+
// Key returns an implementation of Key API.
5265
Key() KeyAPI
5366

5467
// ResolvePath resolves the path using Unixfs resolver

core/coreapi/key.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ type key struct {
2424
peerId string
2525
}
2626

27+
// Name returns the key name
2728
func (k *key) Name() string {
2829
return k.name
2930
}
3031

32+
// Path returns the path of the key.
3133
func (k *key) Path() coreiface.Path {
3234
return &path{path: ipfspath.FromString(ipfspath.Join([]string{"/ipns", k.peerId}))}
3335
}
3436

37+
// Generate generates new key, stores it in the keystore under the specified
38+
// name and returns a base58 encoded multihash of its public key.
3539
func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (coreiface.Key, error) {
3640
options, err := caopts.KeyGenerateOptions(opts...)
3741
if err != nil {
@@ -88,6 +92,7 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
8892
return &key{name, pid.Pretty()}, nil
8993
}
9094

95+
// List returns a list keys stored in keystore.
9196
func (api *KeyAPI) List(ctx context.Context) ([]coreiface.Key, error) {
9297
keys, err := api.node.Repo.Keystore().List()
9398
if err != nil {
@@ -117,6 +122,8 @@ func (api *KeyAPI) List(ctx context.Context) ([]coreiface.Key, error) {
117122
return out, nil
118123
}
119124

125+
// Rename renames `oldName` to `newName`. Returns the key and whether another
126+
// key was overwritten, or an error.
120127
func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (coreiface.Key, bool, error) {
121128
options, err := caopts.KeyRenameOptions(opts...)
122129
if err != nil {
@@ -169,6 +176,7 @@ func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, o
169176
return &key{newName, pid.Pretty()}, overwrite, ks.Delete(oldName)
170177
}
171178

179+
// Remove removes keys from keystore. Returns ipns path of the removed key.
172180
func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Path, error) {
173181
ks := api.node.Repo.Keystore()
174182

core/coreapi/name.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ type ipnsEntry struct {
2929
value coreiface.Path
3030
}
3131

32+
// Name returns the ipnsEntry name.
3233
func (e *ipnsEntry) Name() string {
3334
return e.name
3435
}
3536

37+
// Value returns the ipnsEntry value.
3638
func (e *ipnsEntry) Value() coreiface.Path {
3739
return e.value
3840
}
3941

42+
// Publish announces new IPNS name and returns the new IPNS entry.
4043
func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopts.NamePublishOption) (coreiface.IpnsEntry, error) {
4144
options, err := caopts.NamePublishOptions(opts...)
4245
if err != nil {
@@ -82,6 +85,8 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
8285
}, nil
8386
}
8487

88+
// Resolve attempts to resolve the newest version of the specified name and
89+
// returns its path.
8590
func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.NameResolveOption) (coreiface.Path, error) {
8691
options, err := caopts.NameResolveOptions(opts...)
8792
if err != nil {

core/coreapi/unixfs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
type UnixfsAPI CoreAPI
1616

17+
// Add builds a merkledag node from a reader, adds it to the blockstore,
18+
// and returns the key representing that node.
1719
func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.Path, error) {
1820
k, err := coreunix.AddWithContext(ctx, api.node, r)
1921
if err != nil {
@@ -26,6 +28,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.Path, err
2628
return ParseCid(c), nil
2729
}
2830

31+
// Cat returns the data contained by an IPFS or IPNS object(s) at path `p`.
2932
func (api *UnixfsAPI) Cat(ctx context.Context, p coreiface.Path) (coreiface.Reader, error) {
3033
dagnode, err := api.core().ResolveNode(ctx, p)
3134
if err != nil {
@@ -41,6 +44,8 @@ func (api *UnixfsAPI) Cat(ctx context.Context, p coreiface.Path) (coreiface.Read
4144
return r, nil
4245
}
4346

47+
// Ls returns the contents of an IPFS or IPNS object(s) at path p, with the format:
48+
// `<link base58 hash> <link size in bytes> <link name>`
4449
func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) ([]*coreiface.Link, error) {
4550
dagnode, err := api.core().ResolveNode(ctx, p)
4651
if err != nil {

core/coreunix/add.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type AddedObject struct {
7373
Size string `json:",omitempty"`
7474
}
7575

76+
// NewAdder Returns a new Adder used for a file add operation.
7677
func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCBlockstore, ds dag.DAGService) (*Adder, error) {
7778
return &Adder{
7879
ctx: ctx,
@@ -126,6 +127,7 @@ func (adder *Adder) mfsRoot() (*mfs.Root, error) {
126127
return adder.mroot, nil
127128
}
128129

130+
// SetMfsRoot sets `r` as the root for Adder.
129131
func (adder *Adder) SetMfsRoot(r *mfs.Root) {
130132
adder.mroot = r
131133
}
@@ -152,6 +154,7 @@ func (adder *Adder) add(reader io.Reader) (node.Node, error) {
152154
return balanced.BalancedLayout(params.New(chnk))
153155
}
154156

157+
// RootNode returns the root node of the Added.
155158
func (adder *Adder) RootNode() (node.Node, error) {
156159
// for memoizing
157160
if adder.root != nil {
@@ -181,6 +184,8 @@ func (adder *Adder) RootNode() (node.Node, error) {
181184
return root, err
182185
}
183186

187+
// Recursively pins the root node of Adder and
188+
// writes the pin state to the backing datastore.
184189
func (adder *Adder) PinRoot() error {
185190
root, err := adder.RootNode()
186191
if err != nil {
@@ -207,6 +212,7 @@ func (adder *Adder) PinRoot() error {
207212
return adder.pinning.Flush()
208213
}
209214

215+
// Finalize flushes the mfs root directory and returns the mfs root node.
210216
func (adder *Adder) Finalize() (node.Node, error) {
211217
mr, err := adder.mfsRoot()
212218
if err != nil {
@@ -566,6 +572,7 @@ func outputDagnode(out chan interface{}, name string, dn node.Node) error {
566572
return nil
567573
}
568574

575+
// NewMemoryDagService builds and returns a new mem-datastore.
569576
func NewMemoryDagService() dag.DAGService {
570577
// build mem-datastore for editor's intermediary nodes
571578
bs := bstore.NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))

0 commit comments

Comments
 (0)