Skip to content

Commit

Permalink
feat(profile): Detect peername renames when listing datasets.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Jul 25, 2018
1 parent 1b13e0e commit f1a19ba
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
8 changes: 5 additions & 3 deletions lib/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (r *DatasetRequests) List(p *ListParams, res *[]repo.DatasetRef) error {
ds.ProfileID = pro.ID
}

if err := repo.CanonicalizeProfile(r.repo, ds); err != nil {
if err := repo.CanonicalizeProfile(r.repo, ds, nil); err != nil {
return fmt.Errorf("error canonicalizing peer: %s", err.Error())
}

Expand Down Expand Up @@ -156,9 +156,10 @@ func (r *DatasetRequests) List(p *ListParams, res *[]repo.DatasetRef) error {
return fmt.Errorf("error getting dataset list: %s", err.Error())
}

renames := repo.NewNeedPeernameRenames()
for i, ref := range replies {
if err := repo.CanonicalizeProfile(r.repo, &replies[i]); err != nil {
log.Debug(err.Error())
// May need to change peername.
if err := repo.CanonicalizeProfile(r.repo, &replies[i], &renames); err != nil {
return fmt.Errorf("error canonicalizing dataset peername: %s", err.Error())
}

Expand All @@ -171,6 +172,7 @@ func (r *DatasetRequests) List(p *ListParams, res *[]repo.DatasetRef) error {
replies[i].Dataset.Structure.Schema = nil
}
}
// TODO: If renames.Renames is non-empty, apply it to r.repo

*res = replies
return nil
Expand Down
2 changes: 1 addition & 1 deletion lib/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *ProfileRequests) getProfile(idStr, peername string) (pro *profile.Profi
ref := &repo.DatasetRef{
Peername: peername,
}
if err = repo.CanonicalizeProfile(r.repo, ref); err != nil {
if err = repo.CanonicalizeProfile(r.repo, ref, nil); err != nil {
log.Error("error canonicalizing profile", err.Error())
return nil, err
}
Expand Down
28 changes: 21 additions & 7 deletions repo/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ func isBase58Multihash(hash string) bool {
return true
}

// NeedPeernameRenames represents which peernames need to be renamed, and to what.
type NeedPeernameRenames struct {
Renames map[string]string
}

// NewNeedPeernameRenames returns a new NeedPeerNameRenames struct.
func NewNeedPeernameRenames() NeedPeernameRenames {
return NeedPeernameRenames{
Renames: make(map[string]string),
}
}

// CanonicalizeDatasetRef uses a repo to turn any local aliases into known
// canonical peername for a dataset and populates a missing path
// if the repo has path information for a peername/name combo
Expand All @@ -312,7 +324,7 @@ func CanonicalizeDatasetRef(r Repo, ref *DatasetRef) error {
return nil
}

if err := CanonicalizeProfile(r, ref); err != nil {
if err := CanonicalizeProfile(r, ref, nil); err != nil {
return err
}

Expand Down Expand Up @@ -344,7 +356,7 @@ func CanonicalizeDatasetRef(r Repo, ref *DatasetRef) error {

// CanonicalizeProfile populates dataset DatasetRef ProfileID and Peername properties,
// changing aliases to known names, and adding ProfileID from a peerstore
func CanonicalizeProfile(r Repo, ref *DatasetRef) error {
func CanonicalizeProfile(r Repo, ref *DatasetRef, need *NeedPeernameRenames) error {
if ref.Peername == "" && ref.ProfileID == "" {
return nil
}
Expand All @@ -354,6 +366,7 @@ func CanonicalizeProfile(r Repo, ref *DatasetRef) error {
return err
}

// If this is a dataset ref that a peer of the user owns.
if ref.Peername == "me" || ref.Peername == p.Peername || ref.ProfileID == p.ID {
if ref.Peername == "me" {
ref.ProfileID = p.ID
Expand All @@ -365,6 +378,12 @@ func CanonicalizeProfile(r Repo, ref *DatasetRef) error {
return fmt.Errorf("Peername and ProfileID combination not valid: Peername = %s, ProfileID = %s, but was given ProfileID = %s", p.Peername, p.ID, ref.ProfileID)
}
if ref.ProfileID == p.ID && ref.Peername != p.Peername {
// Rename may have happended, record it if requested by caller.
if need != nil {
need.Renames[ref.Peername] = p.Peername
ref.Peername = p.Peername
return nil
}
return fmt.Errorf("Peername and ProfileID combination not valid: ProfileID = %s, Peername = %s, but was given Peername = %s", p.ID, p.Peername, ref.Peername)
}
if ref.Peername == p.Peername && ref.ProfileID == p.ID {
Expand All @@ -389,11 +408,6 @@ func CanonicalizeProfile(r Repo, ref *DatasetRef) error {
return nil
}
if ref.ProfileID != "" {
// pid, err := profile.NewB58ID(ref.ProfileID)
// if err != nil {
// return fmt.Errorf("error converting ProfileID to base58 hash: %s", err)
// }

profile, err := r.Profiles().GetProfile(ref.ProfileID)
if err != nil {
return fmt.Errorf("error fetching peers from store: %s", err)
Expand Down
42 changes: 41 additions & 1 deletion repo/ref_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repo

import (
"reflect"
"testing"

"github.com/qri-io/cafs"
Expand Down Expand Up @@ -439,7 +440,7 @@ func TestCanonicalizeProfile(t *testing.T) {
}
got := &ref

err = CanonicalizeProfile(repo, got)
err = CanonicalizeProfile(repo, got, nil)
if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) {
t.Errorf("case %d error mismatch. expected: '%s', got: '%s'", i, c.err, err)
continue
Expand All @@ -461,3 +462,42 @@ func TestCanonicalizeProfile(t *testing.T) {
}
}
}

func TestCanonicalizeProfileWithRename(t *testing.T) {
repo, err := NewMemRepo(&profile.Profile{
Peername: "lucille",
ID: profile.IDB58MustDecode("QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y"),
}, cafs.NewMapstore(), profile.NewMemStore(), nil)
if err != nil {
t.Errorf("error allocating mem repo: %s", err.Error())
return
}

lucy := DatasetRef{
ProfileID: profile.IDB58MustDecode("QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y"),
Peername: "lucy",
Name: "ball",
Path: "/ipfs/QmRdexT18WuAKVX3vPusqmJTWLeNSeJgjmMbaF5QLGHna1",
}

renames := NewNeedPeernameRenames()
err = CanonicalizeProfile(repo, &lucy, &renames)
if err != nil {
t.Errorf("error canonicalizing: %s", err.Error())
return
}

keys := make([]string, 0)
for k := range renames.Renames {
keys = append(keys, k)
}
expect := []string{"lucy"}
if !reflect.DeepEqual(keys, expect) {
t.Errorf("error, expected keys %s, got %s", expect, keys)
}
expectVal := "lucille"
actualVal := renames.Renames["lucy"]
if actualVal != expectVal {
t.Errorf("error, expected value %s, got %s", expectVal, actualVal)
}
}

0 comments on commit f1a19ba

Please sign in to comment.