From 4356a9dd59f079ebd47bfd4fe0b2b583f8d84af4 Mon Sep 17 00:00:00 2001 From: ramfox Date: Tue, 6 Feb 2018 20:18:57 -0500 Subject: [PATCH] feat(core, log): add getRemote to Log, add Node *p2p.QriNode to HistoryRequests struct --- core/history.go | 38 ++++++++++++++++++++++++++++---------- core/history_test.go | 4 ++-- p2p/datasets.go | 2 -- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/core/history.go b/core/history.go index 8e9ca4a15..e30a589c3 100644 --- a/core/history.go +++ b/core/history.go @@ -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" ) @@ -14,6 +14,7 @@ import ( type HistoryRequests struct { repo repo.Repo cli *rpc.Client + Node *p2p.QriNode } // CoreRequestsName implements the Requets interface @@ -27,6 +28,7 @@ func NewHistoryRequests(r repo.Repo, cli *rpc.Client) *HistoryRequests { } return &HistoryRequests{ repo: r, + cli: cli, } } @@ -48,22 +50,36 @@ 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) @@ -71,9 +87,11 @@ func (d *HistoryRequests) Log(params *LogParams, res *[]*repo.DatasetRef) (err e 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 diff --git a/core/history_test.go b/core/history_test.go index 1bc793be4..5381fd795 100644 --- a/core/history_test.go +++ b/core/history_test.go @@ -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) diff --git a/p2p/datasets.go b/p2p/datasets.go index 41c74e04b..44bee37a8 100644 --- a/p2p/datasets.go +++ b/p2p/datasets.go @@ -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)