merkledag.go 1.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package format

import (
	"context"
	"fmt"

	cid "github.com/ipfs/go-cid"
)

var ErrNotFound = fmt.Errorf("merkledag: not found")

// Either a node or an error.
type NodeOption struct {
	Node Node
	Err  error
}

Steven Allen's avatar
Steven Allen committed
18
// The basic Node resolution service.
19
type NodeGetter interface {
20 21 22
	// Get retrieves nodes by CID. Depending on the NodeGetter
	// implementation, this may involve fetching the Node from a remote
	// machine; consider setting a deadline in the context.
23
	Get(context.Context, *cid.Cid) (Node, error)
24

25 26
	// GetMany returns a channel of NodeOptions given a set of CIDs.
	GetMany(context.Context, []*cid.Cid) <-chan *NodeOption
27 28 29 30 31 32
}

// NodeGetters can optionally implement this interface to make finding linked
// objects faster.
type LinkGetter interface {
	NodeGetter
33

34
	// TODO(ipfs/go-ipld-format#9): This should return []*cid.Cid
35 36 37

	// GetLinks returns the children of the node refered to by the given
	// CID.
38 39 40
	GetLinks(ctx context.Context, nd *cid.Cid) ([]*Link, error)
}

41 42 43 44
// DAGService is an IPFS Merkle DAG service.
type DAGService interface {
	NodeGetter

45
	// Add adds a node to this DAG.
46
	Add(context.Context, Node) error
47

48 49
	// Remove removes a node from this DAG.
	//
Steven Allen's avatar
Steven Allen committed
50
	// Remove returns no error if the requested node is not present in this DAG.
51
	Remove(context.Context, *cid.Cid) error
52

53 54 55 56
	// AddMany adds many nodes to this DAG.
	//
	// Consider using NewBatch instead of calling this directly if you need
	// to add an unbounded number of nodes to avoid buffering too much.
57
	AddMany(context.Context, []Node) error
Steven Allen's avatar
Steven Allen committed
58 59 60 61

	// RemoveMany removes many nodes from this DAG.
	//
	// It returns success even if the nodes were not present in the DAG.
62
	RemoveMany(context.Context, []*cid.Cid) error
63
}