Skip to content

Commit

Permalink
feat(bsync): initial work on bsync
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Dec 5, 2018
1 parent e3e52ac commit f3c37c6
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 43 deletions.
43 changes: 14 additions & 29 deletions actions/manifest.go
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
}
38 changes: 38 additions & 0 deletions base/manifest.go
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
}
Loading

0 comments on commit f3c37c6

Please sign in to comment.