-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_getter.go
37 lines (31 loc) · 1009 Bytes
/
node_getter.go
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
package dag
import (
"context"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
)
// NodeGetter wraps the go-ipfs DagAPI to satistfy the IPLD NodeGetter interface
type NodeGetter struct {
Dag ipld.DAGService
}
// NewNodeGetter returns a new NodeGetter from an IPFS core API
func NewNodeGetter(dsvc ipld.DAGService) *NodeGetter {
return &NodeGetter{Dag: dsvc}
}
// 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.
func (ng *NodeGetter) Get(ctx context.Context, id cid.Cid) (ipld.Node, error) {
return ng.Dag.Get(ctx, id)
}
// GetMany returns a channel of NodeOptions given a set of CIDs.
func (ng *NodeGetter) GetMany(ctx context.Context, cids []cid.Cid) <-chan *ipld.NodeOption {
ch := make(chan *ipld.NodeOption)
go func() {
for _, id := range cids {
n, err := ng.Get(ctx, id)
ch <- &ipld.NodeOption{Err: err, Node: n}
}
}()
return ch
}