-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
510 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,41 @@ | ||
package actions | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/qri-io/qri/base" | ||
"github.com/qri-io/qri/manifest" | ||
"github.com/qri-io/qri/p2p" | ||
|
||
"gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" | ||
ipld "gx/ipfs/QmR7TcHkR9nxkUorfi8XMTAMLUK7GiP64TWWBzY3aacc1o/go-ipld-format" | ||
"gx/ipfs/QmUJYo4etAQqFfSS2rarFAE97eNGB8ej64YkRT2SmsYD4r/go-ipfs/core/coreapi" | ||
coreiface "gx/ipfs/QmUJYo4etAQqFfSS2rarFAE97eNGB8ej64YkRT2SmsYD4r/go-ipfs/core/coreapi/interface" | ||
) | ||
|
||
// NewManifest generates a manifest for a given node | ||
func NewManifest(node *p2p.QriNode, path string) (*manifest.Manifest, error) { | ||
ipfsn, err := node.IPFSNode() | ||
ng, err := newNodeGetter(node) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
id, err := cid.Parse(path) | ||
return base.NewManifest(node.Context(), ng, path) | ||
} | ||
|
||
// Missing returns a manifest describing blocks that are not in this node for a given manifest | ||
func Missing(node *p2p.QriNode, m *manifest.Manifest) (missing *manifest.Manifest, err error) { | ||
ng, err := newNodeGetter(node) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ng := &nodeGetter{dag: coreapi.NewCoreAPI(ipfsn).Dag()} | ||
return manifest.NewManifest(node.Context(), ng, id) | ||
return base.Missing(node.Context(), ng, m) | ||
} | ||
|
||
type nodeGetter struct { | ||
dag coreiface.DagAPI | ||
} | ||
|
||
// 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) { | ||
path, err := coreiface.ParsePath(id.String()) | ||
// newNodeGetter generates an ipld.NodeGetter from a QriNode | ||
func newNodeGetter(node *p2p.QriNode) (ng ipld.NodeGetter, err error) { | ||
ipfsn, err := node.IPFSNode() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ng.dag.Get(ctx, path) | ||
} | ||
|
||
// 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 | ||
ng = &manifest.NodeGetter{Dag: coreapi.NewCoreAPI(ipfsn).Dag()} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package base | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/qri-io/qri/manifest" | ||
|
||
"gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" | ||
ipld "gx/ipfs/QmR7TcHkR9nxkUorfi8XMTAMLUK7GiP64TWWBzY3aacc1o/go-ipld-format" | ||
) | ||
|
||
// NewManifest generates a manifest for a given node | ||
func NewManifest(ctx context.Context, ng ipld.NodeGetter, path string) (*manifest.Manifest, error) { | ||
id, err := cid.Parse(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return manifest.NewManifest(ctx, ng, id) | ||
} | ||
|
||
// Missing returns a manifest describing blocks that are not in this node for a given manifest | ||
func Missing(ctx context.Context, ng ipld.NodeGetter, m *manifest.Manifest) (missing *manifest.Manifest, err error) { | ||
var nodes []string | ||
|
||
for _, idstr := range m.Nodes { | ||
id, err := cid.Parse(idstr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if _, err := ng.Get(ctx, id); err == ipld.ErrNotFound { | ||
nodes = append(nodes, id.String()) | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return &manifest.Manifest{Nodes: nodes}, nil | ||
} |
Oops, something went wrong.