Skip to content

Commit

Permalink
feat(core, log): add getRemote to Log, add Node *p2p.QriNode to Histo…
Browse files Browse the repository at this point in the history
…ryRequests struct
  • Loading branch information
ramfox committed Feb 7, 2018
1 parent a5b5f9b commit 4356a9d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
38 changes: 28 additions & 10 deletions core/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/rpc"

"github.com/ipfs/go-datastore"
"github.com/qri-io/dataset/dsfs"
"github.com/qri-io/qri/p2p"
"github.com/qri-io/qri/repo"
)

Expand All @@ -14,6 +14,7 @@ import (
type HistoryRequests struct {
repo repo.Repo
cli *rpc.Client
Node *p2p.QriNode
}

// CoreRequestsName implements the Requets interface
Expand All @@ -27,6 +28,7 @@ func NewHistoryRequests(r repo.Repo, cli *rpc.Client) *HistoryRequests {
}
return &HistoryRequests{
repo: r,
cli: cli,
}
}

Expand All @@ -48,32 +50,48 @@ func (d *HistoryRequests) Log(params *LogParams, res *[]*repo.DatasetRef) (err e
return fmt.Errorf("either path or name is required")
}

if params.Path.String() == "" {
ref := &repo.DatasetRef{Peername: params.ListParams.Peername, Name: params.Name, Path: params.Path.String()}

getRemote := func(err error) error {
if d.Node != nil {
log, err := d.Node.RequestDatasetLog(ref)
if err != nil {
return err
}

*res = *log
return nil
}
return err
}
if ref.Path == "" {
path, err := d.repo.GetPath(params.Name)
if err != nil {
return fmt.Errorf("error loading path for name: %s", err.Error())
err = fmt.Errorf("error loading path from name: %s", err.Error())
return getRemote(err)
}
params.Path = path
ref.Path = path.String()
}

log := []*repo.DatasetRef{}
limit := params.Limit
ref := &repo.DatasetRef{Path: params.Path.String()}

for {
ref.Dataset, err = dsfs.LoadDataset(d.repo.Store(), datastore.NewKey(ref.Path))
ref.Dataset, err = d.repo.GetDataset(datastore.NewKey(ref.Path))
if err != nil {
return err
return fmt.Errorf("error adding datasets to log: %s", err.Error())
}
log = append(log, ref)

limit--
if limit == 0 || ref.Dataset.PreviousPath == "" {
break
}
// TODO - clean this up
_, cleaned := dsfs.RefType(ref.Dataset.PreviousPath)
ref = &repo.DatasetRef{Path: cleaned}

ref, err = repo.ParseDatasetRef(ref.Dataset.PreviousPath)
if err != nil {
return fmt.Errorf("error adding datasets to log: %s", err.Error())
}
}

*res = log
Expand Down
4 changes: 2 additions & 2 deletions core/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func TestHistoryRequestsLog(t *testing.T) {
err string
}{
{&LogParams{}, nil, "either path or name is required"},
{&LogParams{Path: datastore.NewKey("/badpath")}, nil, "error loading dataset: error getting file bytes: datastore: key not found"},
{&LogParams{Path: path}, []*repo.DatasetRef{&repo.DatasetRef{Path: path.String()}}, ""},
{&LogParams{Path: datastore.NewKey("/badpath")}, nil, "error adding datasets to log: datastore: key not found"},
{&LogParams{Path: path}, []*repo.DatasetRef{{Path: path.String()}}, ""},
}

req := NewHistoryRequests(mr, nil)
Expand Down
2 changes: 0 additions & 2 deletions p2p/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ func (n *QriNode) RequestDatasetInfo(ref *repo.DatasetRef) (*repo.DatasetRef, er
return resref, err
}

// DatasetLogParams encapsulates options for requesting datasets

// RequestDatasetLog gets the log information of Peer's dataset
func (n *QriNode) RequestDatasetLog(ref *repo.DatasetRef) (*[]*repo.DatasetRef, error) {
id, err := n.Repo.Peers().IPFSPeerID(ref.Peername)
Expand Down

0 comments on commit 4356a9d

Please sign in to comment.