merkledag.go 1.47 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
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)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

	// TODO(ipfs/go-ipfs#4009): Remove this method after fixing.
	OfflineNodeGetter() NodeGetter
}

// NodeGetters can optionally implement this interface to make finding linked
// objects faster.
type LinkGetter interface {
	NodeGetter
	// TODO(ipfs/go-ipld-format#9): This should return []*cid.Cid
	GetLinks(ctx context.Context, nd *cid.Cid) ([]*Link, error)
}

func GetLinks(ctx context.Context, ng NodeGetter, c *cid.Cid) ([]*Link, error) {
	if c.Type() == cid.Raw {
		return nil, nil
	}
	if gl, ok := ng.(LinkGetter); ok {
		return gl.GetLinks(ctx, c)
	}
	node, err := ng.Get(ctx, c)
	if err != nil {
		return nil, err
	}
	return node.Links(), nil
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
}

// 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

64
	AddMany([]Node) ([]*cid.Cid, error)
65
}