Skip to content

Commit

Permalink
feat(PeerID): add PeerID to datasetRef
Browse files Browse the repository at this point in the history
Merge pull request #298 from qri-io/peerid
  • Loading branch information
ramfox authored Mar 7, 2018
2 parents 2ef413a + 35427c1 commit e1e46f4
Show file tree
Hide file tree
Showing 16 changed files with 516 additions and 263 deletions.
1 change: 0 additions & 1 deletion api/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,5 @@ func HTTPPathToQriPath(path string) string {
}
path = strings.Replace(path, "/at", "@", 1)
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")
return path
}
6 changes: 3 additions & 3 deletions api/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func TestDatasetRefFromReq(t *testing.T) {
{"http://localhost:2503/peername?limit=10&offset=2", repo.DatasetRef{Peername: "peername"}, ""},
{"http://localhost:2503/peername/datasetname", repo.DatasetRef{Peername: "peername", Name: "datasetname"}, ""},
{"http://localhost:2503/peername/datasetname/at/ipfs/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD", repo.DatasetRef{Peername: "peername", Name: "datasetname", Path: "/ipfs/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD"}, ""},
{"http://localhost:2503/peername/datasetname/at/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD", repo.DatasetRef{Peername: "peername", Name: "datasetname", Path: "/ipfs/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD"}, ""},
{"http://localhost:2503/peername/datasetname/at/ntwk/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD", repo.DatasetRef{Peername: "peername", Name: "datasetname", Path: "/ntwk/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD"}, ""},
{"http://localhost:2503/peername/datasetname/at/map/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD/dataset.json", repo.DatasetRef{Peername: "peername", Name: "datasetname", Path: "/map/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD"}, ""},
{"http://localhost:2503/peername/datasetname/at/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD", repo.DatasetRef{Peername: "peername", Name: "datasetname", Path: "/ipfs/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD"}, ""},
{"http://google.com:8000/peername/datasetname/at/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD", repo.DatasetRef{Peername: "peername", Name: "datasetname", Path: "/ipfs/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD"}, ""},
{"http://localhost:2503/peername/datasetname/at/ipfs/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD", repo.DatasetRef{Peername: "peername", Name: "datasetname", Path: "/ipfs/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD"}, ""},
{"http://google.com:8000/peername/datasetname/at/ipfs/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD", repo.DatasetRef{Peername: "peername", Name: "datasetname", Path: "/ipfs/QmdWJ7RnFj3SdWW85mR4AYP17C8dRPD9eUPyTqUxVyGMgD"}, ""},
{"http://google.com:8000/peername", repo.DatasetRef{Peername: "peername"}, ""},
// {"http://google.com/peername", repo.DatasetRef{Peername: "peername"}, ""},
{"/peername", repo.DatasetRef{Peername: "peername"}, ""},
Expand Down
1 change: 1 addition & 0 deletions api/testdata/getResponseFamilyRelationships.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"data": {
"peername": "peer",
"name": "family_relationships",
"peerID": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"path": "/map/QmfMTGQKjCcy5nEJwMCQNaqNHVGFTyMdhvWk9pQUXSBD3Z",
"dataset": {
"abstract": "/map/Qmb3n8FvgDbLoU9d7e3vo1UAyVkwV1RnqXUqPKC3Rj2Ej7",
Expand Down
4 changes: 4 additions & 0 deletions api/testdata/historyResponse.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"peername": "peer",
"name": "cities",
"peerID": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"path": "/map/QmY6DkbrhHEBSMb1DqqPSUhuVo3BoNadAEGDxSveBZKDrk",
"dataset": {
"abstract": {
Expand Down Expand Up @@ -79,6 +80,7 @@
{
"peername": "peer",
"name": "cities",
"peerID": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"path": "/map/QmVZJTaRsiSc5UkpBSzH4yFQXvKKWGkjvGh5rNzueWDNQa",
"dataset": {
"abstract": {
Expand Down Expand Up @@ -155,6 +157,7 @@
{
"peername": "peer",
"name": "cities",
"peerID": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"path": "/map/QmQyHfc5iro93F5caS9XhkzEf5o6uq1Zz2jxc3Qu2cWNP2",
"dataset": {
"abstract": {
Expand Down Expand Up @@ -227,6 +230,7 @@
{
"peername": "peer",
"name": "cities",
"peerID": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"path": "/map/QmbnworAqqiMBGRoQGfnseRJmsK31UGSAFtWD3Doh4b6xq",
"dataset": {
"abstract": {
Expand Down
79 changes: 4 additions & 75 deletions api/testdata/listResponse.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"peername": "peer",
"name": "cities",
"peerID": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"path": "/map/QmVZJTaRsiSc5UkpBSzH4yFQXvKKWGkjvGh5rNzueWDNQa",
"dataset": {
"abstract": "/map/Qmb3n8FvgDbLoU9d7e3vo1UAyVkwV1RnqXUqPKC3Rj2Ej7",
Expand Down Expand Up @@ -69,6 +70,7 @@
{
"peername": "peer",
"name": "counter",
"peerID": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"path": "/map/Qmd4RtQrDGVaF1aArH8j74VGwU4q5ebiytnGYmBbNRLZjp",
"dataset": {
"abstract": "/map/Qmb3n8FvgDbLoU9d7e3vo1UAyVkwV1RnqXUqPKC3Rj2Ej7",
Expand Down Expand Up @@ -112,6 +114,7 @@
{
"peername": "peer",
"name": "craigslist",
"peerID": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"path": "/map/QmRLpDXcrccTzNCwWXyyHRZCmSzDxPoMX91KtLW3szmmkX",
"dataset": {
"abstract": "/map/QmW4cDbQYgie835Ro5uGfAeRU4PWPGcbuS5yn5uFRP4ddZ",
Expand Down Expand Up @@ -24020,84 +24023,10 @@
}
}
},
{
"peername": "peer",
"name": "flourinated_compounds_in_fast_food_packaging",
"path": "/map/QmU9YD3gLZjBLHhe9MEftyBtMEW6tky2XczTbCVq2q84cY",
"dataset": {
"abstract": "/map/QmPUAqhiqdGDZUourgARtmMqNKWmuTPcy5JSxasvRPdvor",
"commit": {
"qri": "cm:0",
"signature": "5LB6oyPr5VjMbTcwGRTqxmBkULqoJf3uYFM7ovoHkSLdfA94hPGa8uMveXE6YAxZYAB5kaKNWcEXg1oSwCKuXqVzvmjkPJPZs6J3LBYP1GdogZNMUxz9btrYAaS6qEcu4WwnTrhfr77wKc6mTDEzgnTR6tai78AjceXeEFpBppQ1xKXX3vn5ofmeRpoFVAKUN52FZra5x6swzncPaD8TzpFb4u59kHhVy9iKZ9xUKBmAAYPvSwJBJafYgSF5fR5sEYfjCbs6GXSDgpsRhdpGgqFpezUJL4u6ghCH5HbFtjEVGCiHFq5WRATDQMQSv1YNogKo9fhLPLkvKRqipRwsswSwTdEVXS",
"timestamp": "2001-01-01T01:01:01.000000001Z",
"title": "initial commit"
},
"dataPath": "/map/QmcN7FHgCbP9MfUmurGuNh3X1DNU4B9LbjXg3r2b3VBrQZ",
"meta": {
"description": "Paper samples, paper extracts (known), paper extracts (unknown). \n\nThis dataset is associated with the following publication:\nSchaider, L., S. Balan, A. Blum, D. Andrews, M. Strynar, M. Dickinson, D. Lunderberg, J. Lang, and G. Peaslee. Fluorinated Compounds in U.S. Fast Food Packaging. Environmental Science \u0026amp; Technology Letters. American Chemical Society, Washington, DC, USA, 4(3): 105\u0026ndash;111, (2017).",
"qri": "md:0",
"title": "Fluorinated Compounds in U.S. Fast Food Packaging"
},
"qri": "ds:0",
"structure": {
"checksum": "QmcN7FHgCbP9MfUmurGuNh3X1DNU4B9LbjXg3r2b3VBrQZ",
"entries": 25,
"errCount": 1,
"format": "cbor",
"length": 1785,
"qri": "st:0",
"schema": {
"items": {
"items": [
{
"title": "sample",
"type": "string"
},
{
"title": "comments",
"type": "string"
},
{
"description": "length of sample in cm",
"title": "length_cm",
"type": "integer"
},
{
"description": "width of sample in cm",
"title": "width_cm",
"type": "integer"
},
{
"description": "area of sample in cm2",
"title": "area_cm",
"type": "integer"
},
{
"description": "vial tare in grams",
"title": "vial_tare_g",
"type": "number"
},
{
"description": "mass of vial tare with paper in grams",
"title": "vial_with_paper_g",
"type": "number"
},
{
"description": "mass of paper in grams",
"title": "mass_g",
"type": "number"
}
],
"type": "array"
},
"type": "array"
}
}
}
},
{
"peername": "peer",
"name": "movies",
"peerID": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"path": "/map/QmRkFb9FJeq8PUxnPY8UP5esveeCygDnx34suFv6BiG3Wr",
"dataset": {
"abstract": "/map/Qmb3n8FvgDbLoU9d7e3vo1UAyVkwV1RnqXUqPKC3Rj2Ej7",
Expand Down
2 changes: 1 addition & 1 deletion api/testdata/profileResponse.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"data": {
"id": "",
"id": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"created": "0001-01-01T00:00:00Z",
"updated": "0001-01-01T00:00:00Z",
"peername": "peer",
Expand Down
5 changes: 5 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ qri repository.`,
err = r.List(p, &refs)
ExitIfErr(err)

for _, ref := range refs {
// remove peerID so names print pretty
ref.PeerID = ""
}

outformat := cmd.Flag("format").Value.String()
switch outformat {
case "":
Expand Down
36 changes: 19 additions & 17 deletions core/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/qri-io/jsonschema"
"github.com/qri-io/qri/p2p"
"github.com/qri-io/qri/repo"
"github.com/qri-io/qri/repo/profile"
"github.com/qri-io/varName"
)

Expand Down Expand Up @@ -66,28 +65,25 @@ func (r *DatasetRequests) List(p *ListParams, res *[]repo.DatasetRef) error {
return r.cli.Call("DatasetRequests.List", p, res)
}

if err := repo.CanonicalizePeername(r.repo, &p.Peername); err != nil {
return fmt.Errorf("error canonicalizing peername: %s", err.Error())
ds := &repo.DatasetRef{
Peername: p.Peername,
PeerID: p.PeerID,
}

if p.PeerID != "" {
if pid, err := profile.NewB58PeerID(p.PeerID); err == nil {
if peer, err := r.repo.Peers().GetPeer(pid); err == nil {
p.Peername = peer.Peername
}
}
if err := repo.CanonicalizePeer(r.repo, ds); err != nil {
return fmt.Errorf("error canonicalizing peer: %s", err.Error())
}

if p.Peername != "" && r.Node != nil {
replies, err := r.Node.RequestDatasetsList(p.Peername)
if ds.Peername != "" && r.Node != nil {
replies, err := r.Node.RequestDatasetsList(ds.Peername)
*res = replies
return err
} else if p.Peername != "" {
} else if ds.Peername != "" {
pro, err := r.repo.Profile()
if err != nil {
return err
}
if pro.Peername != p.Peername && r.Node == nil {
if pro.Peername != ds.Peername && r.Node == nil {
return fmt.Errorf("cannot list remote datasets without p2p connection")
}
}
Expand All @@ -111,6 +107,10 @@ func (r *DatasetRequests) List(p *ListParams, res *[]repo.DatasetRef) error {
break
}

if err := repo.CanonicalizePeer(r.repo, &replies[i]); err != nil {
return fmt.Errorf("error canonicalizing dataset peername: %s", err.Error())
}

ds, err := dsfs.LoadDataset(store, datastore.NewKey(ref.Path))
if err != nil {
// try one extra time...
Expand Down Expand Up @@ -186,6 +186,7 @@ func (r *DatasetRequests) Get(p *repo.DatasetRef, res *repo.DatasetRef) (err err
}

*res = repo.DatasetRef{
PeerID: p.PeerID,
Peername: p.Peername,
Name: p.Name,
Path: p.Path,
Expand Down Expand Up @@ -224,10 +225,6 @@ func (r *DatasetRequests) Init(p *InitParams, res *repo.DatasetRef) error {
filename = p.DataFilename
)

if err := repo.CanonicalizePeername(r.repo, &p.Peername); err != nil {
return fmt.Errorf("error canonicalizing peername: %s", err.Error())
}

if p.URL != "" {
res, err := http.Get(p.URL)
if err != nil {
Expand Down Expand Up @@ -323,6 +320,11 @@ func (r *DatasetRequests) Init(p *InitParams, res *repo.DatasetRef) error {
}

ref := repo.DatasetRef{Peername: p.Peername, Name: name, Path: dskey.String(), Dataset: ds}

if err := repo.CanonicalizePeer(r.repo, &ref); err != nil {
return fmt.Errorf("error canonicalizing peername: %s", err.Error())
}

if err = r.repo.PutRef(ref); err != nil {
return fmt.Errorf("error adding dataset name to repo: %s", err.Error())
}
Expand Down
12 changes: 5 additions & 7 deletions core/datasets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestDatasetRequestsInit(t *testing.T) {

func TestDatasetRequestsList(t *testing.T) {
var (
movies, counter, cities, craigslist, flourinated repo.DatasetRef
movies, counter, cities, craigslist repo.DatasetRef
)

mr, err := testrepo.NewTestRepo()
Expand All @@ -93,8 +93,6 @@ func TestDatasetRequestsList(t *testing.T) {
cities = ref
case "craigslist":
craigslist = ref
case "flourinated_compounds_in_fast_food_packaging":
flourinated = ref
}
}

Expand All @@ -105,9 +103,9 @@ func TestDatasetRequestsList(t *testing.T) {
}{
{&ListParams{OrderBy: "", Limit: 1, Offset: 0}, nil, ""},
{&ListParams{OrderBy: "chaos", Limit: 1, Offset: -50}, nil, ""},
{&ListParams{OrderBy: "", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, flourinated, movies}, ""},
{&ListParams{OrderBy: "timestamp", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, flourinated, movies}, ""},
{&ListParams{Peername: "me", OrderBy: "timestamp", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, flourinated, movies}, ""},
{&ListParams{OrderBy: "", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, movies}, ""},
{&ListParams{OrderBy: "timestamp", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, movies}, ""},
{&ListParams{Peername: "me", OrderBy: "timestamp", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, movies}, ""},
// TODO: re-enable {&ListParams{OrderBy: "name", Limit: 30, Offset: 0}, []*repo.DatasetRef{cities, counter, movies}, ""},
}

Expand Down Expand Up @@ -406,7 +404,7 @@ Pirates of the Caribbean: At World's End ,169
err string
}{
{ValidateDatasetParams{Ref: repo.DatasetRef{}}, 0, "either data or a dataset reference is required"},
{ValidateDatasetParams{Ref: repo.DatasetRef{Peername: "me"}}, 0, "cannot find dataset: peer"},
{ValidateDatasetParams{Ref: repo.DatasetRef{Peername: "me"}}, 0, "cannot find dataset: peer@QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt"},
{ValidateDatasetParams{Ref: repo.DatasetRef{Peername: "me", Name: "movies"}}, 1, ""},
{ValidateDatasetParams{Ref: repo.DatasetRef{Peername: "me", Name: "movies"}, Data: dataf, DataFilename: "data.csv"}, 1, ""},
{ValidateDatasetParams{Ref: repo.DatasetRef{Peername: "me", Name: "movies"}, Schema: schemaf}, 0, ""},
Expand Down
2 changes: 1 addition & 1 deletion core/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestHistoryRequestsLog(t *testing.T) {
err string
}{
{&LogParams{}, nil, "either path or peername/name is required"},
{&LogParams{Ref: repo.DatasetRef{Path: "/badpath"}}, nil, "error getting reference '@badpath': repo: not found"},
{&LogParams{Ref: repo.DatasetRef{Path: "/badpath"}}, nil, "error getting reference '@/badpath': repo: not found"},
{&LogParams{Ref: ref}, []repo.DatasetRef{ref}, ""},
}

Expand Down
18 changes: 10 additions & 8 deletions repo/fs/refstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,40 @@ type Refstore struct {
func (n Refstore) PutRef(put repo.DatasetRef) (err error) {
var ds *dataset.Dataset

if put.Peername == "" {
return repo.ErrPeernameRequired
if put.PeerID == "" {
return repo.ErrPeerIDRequired
} else if put.Name == "" {
return repo.ErrNameRequired
} else if put.Path == "" {
return repo.ErrPathRequired
}

p := repo.DatasetRef{PeerID: put.PeerID, Name: put.Name, Path: put.Path}

names, err := n.names()
if err != nil {
return err
}

for _, ref := range names {
if ref.Equal(put) {
if ref.Equal(p) {
return nil
} else if ref.Match(put) {
} else if ref.Match(p) {
return repo.ErrNameTaken
}
}

names = append(names, put)
names = append(names, p)
if n.store != nil {
ds, err = dsfs.LoadDataset(n.store, datastore.NewKey(put.Path))
ds, err = dsfs.LoadDataset(n.store, datastore.NewKey(p.Path))
if err != nil {
return err
}
}

if n.index != nil {
batch := n.index.NewBatch()
err = batch.Index(put.Path, ds)
err = batch.Index(p.Path, ds)
if err != nil {
return err
}
Expand Down Expand Up @@ -95,7 +97,7 @@ func (n Refstore) DeleteRef(del repo.DatasetRef) error {
}

for i, ref := range names {
if ref.Equal(del) {
if ref.Match(del) {
if ref.Path != "" && n.index != nil {
if err := n.index.Delete(ref.Path); err != nil {
return err
Expand Down
Loading

0 comments on commit e1e46f4

Please sign in to comment.