2018-02-09 22:20:44 +00:00
|
|
|
package importer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
ipld "github.com/ipfs/go-ipld-format"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errUninit = errors.New("DAGService output channel uninitialized")
|
|
|
|
|
2018-02-12 20:57:04 +00:00
|
|
|
// outDAGService will "add" a node by sending through the outChan
|
2018-02-09 22:20:44 +00:00
|
|
|
type outDAGService struct {
|
2018-02-15 00:02:55 +00:00
|
|
|
membership map[string]struct{}
|
2018-02-09 22:20:44 +00:00
|
|
|
outChan chan<- *ipld.Node
|
|
|
|
}
|
|
|
|
|
2018-02-12 20:57:04 +00:00
|
|
|
// Get always returns errNotFound
|
2018-02-09 22:20:44 +00:00
|
|
|
func (ods *outDAGService) Get(ctx context.Context, key *cid.Cid) (ipld.Node, error) {
|
|
|
|
return nil, errNotFound
|
|
|
|
}
|
|
|
|
|
2018-02-12 20:57:04 +00:00
|
|
|
// GetMany returns an output channel that always emits an error
|
2018-02-09 22:20:44 +00:00
|
|
|
func (ods *outDAGService) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *ipld.NodeOption {
|
2018-02-15 00:02:55 +00:00
|
|
|
out := make(chan *ipld.NodeOption, 1)
|
|
|
|
out <- &ipld.NodeOption{Err: fmt.Errorf("failed to fetch all nodes")}
|
|
|
|
close(out)
|
2018-02-09 22:20:44 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-02-12 20:57:04 +00:00
|
|
|
// Add passes the provided node through the output channel
|
2018-02-09 22:20:44 +00:00
|
|
|
func (ods *outDAGService) Add(ctx context.Context, node ipld.Node) error {
|
|
|
|
id := node.Cid().String()
|
|
|
|
_, ok := ods.membership[id]
|
|
|
|
if ok { // already added don't add again
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-15 00:02:55 +00:00
|
|
|
ods.membership[id] = struct{}{}
|
2018-02-09 22:20:44 +00:00
|
|
|
|
|
|
|
// Send node on output channel
|
|
|
|
if ods.outChan == nil {
|
|
|
|
return errUninit
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case ods.outChan <- &node:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
close(ods.outChan)
|
|
|
|
return errors.New("canceled context preempted dagservice add")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 20:57:04 +00:00
|
|
|
// AddMany passes the provided nodes through the output channel
|
2018-02-09 22:20:44 +00:00
|
|
|
func (ods *outDAGService) AddMany(ctx context.Context, nodes []ipld.Node) error {
|
|
|
|
for _, node := range nodes {
|
|
|
|
err := ods.Add(ctx, node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove is a nop
|
|
|
|
func (ods *outDAGService) Remove(ctx context.Context, key *cid.Cid) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveMany is a nop
|
|
|
|
func (ods *outDAGService) RemoveMany(ctx context.Context, keys []*cid.Cid) error {
|
|
|
|
return nil
|
|
|
|
}
|