merkledag.go 1.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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
}

// TODO: This name kind of sucks.
// NodeResolver?
// NodeService?
// Just Resolver?
type NodeGetter interface {
	Get(context.Context, *cid.Cid) (Node, error)
}

// DAGService is an IPFS Merkle DAG service.
type DAGService interface {
	NodeGetter

	Add(Node) (*cid.Cid, error)
	Remove(Node) error

	// TODO: This is returning them in-order?? Why not just use []NodePromise?
	// Maybe add a couple of helpers for getting them in-order and as-available?
	// GetDAG returns, in order, all the single leve child
	// nodes of the passed in node.
	GetMany(context.Context, []*cid.Cid) <-chan *NodeOption

39
	AddMany([]Node) ([]*cid.Cid, error)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

	LinkService
}

// TODO: Replace this? I'm really not convinced this interface pulls its weight.
//
// Instead, we could add an `Offline()` function to `NodeGetter` that returns an
// offline `NodeGetter` and then define the following function:
//
// ```
// func GetLinks(ctx context.Context, ng NodeGetter, c *cid.Cid) ([]*Link, error) {
// 	if c.Type() == cid.Raw {
// 		return nil, nil
// 	}
// 	node, err := ng.Get(ctx, c)
// 	if err != nil {
// 		return nil, err
// 	}
// 	return node.Links(), nil
// }
// ```
//
// Why *not* do this? We might decide to store a light-weight DAG of links
// without actually storing the data. I don't really find that to be a
// convincing argument.
type LinkService interface {
	// GetLinks return all links for a node.  The complete node does not
	// necessarily have to exist locally, or at all.  For example, raw
	// leaves cannot possibly have links so there is no need to look
	// at the node.
	// TODO: These *really* should be Cids, not Links
	GetLinks(context.Context, *cid.Cid) ([]*Link, error)

	GetOfflineLinkService() LinkService
}