Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RawKey function and make use of it. #56

Merged
merged 1 commit into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ func NewKey(s string) Key {
return k
}

// RawKey creates a new Key without safety checking the input. Use with care.
func RawKey(s string) Key {
// accept an empty string and fix it to avoid special cases
// elsewhere
if len(s) == 0 {
return Key{"/"}
}

// perform a quick sanity check that the key is in the correct
// format, if it is not then it is a programmer error and it is
// okay to panic
if len(s) == 0 || s[0] != '/' || (len(s) > 1 && s[len(s)-1] == '/') {
panic("invalid datastore key: " + s)
}

return Key{s}
}

// KeyWithNamespaces constructs a key out of a namespace slice.
func KeyWithNamespaces(ns []string) Key {
return NewKey(strings.Join(ns, "/"))
Expand Down Expand Up @@ -156,7 +174,7 @@ func (k Key) Path() Key {
func (k Key) Parent() Key {
n := k.List()
if len(n) == 1 {
return NewKey("/")
return RawKey("/")
}
return NewKey(strings.Join(n[:len(n)-1], "/"))
}
Expand All @@ -165,7 +183,14 @@ func (k Key) Parent() Key {
// NewKey("/Comedy/MontyPython").Child(NewKey("Actor:JohnCleese"))
// NewKey("/Comedy/MontyPython/Actor:JohnCleese")
func (k Key) Child(k2 Key) Key {
return NewKey(k.string + "/" + k2.string)
switch {
case k.string == "/":
return k2
case k2.string == "/":
return k
default:
return RawKey(k.string + k2.string)
}
}

// ChildString returns the `child` Key of this Key -- string helper.
Expand Down
2 changes: 1 addition & 1 deletion keytransform/keytransform.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (d *ktds) Query(q dsq.Query) (dsq.Results, error) {

for r := range qr.Next() {
if r.Error == nil {
r.Entry.Key = d.InvertKey(ds.NewKey(r.Entry.Key)).String()
r.Entry.Key = d.InvertKey(ds.RawKey(r.Entry.Key)).String()
}
ch <- r
}
Expand Down
7 changes: 3 additions & 4 deletions namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package namespace

import (
"fmt"
"strings"

ds "github.com/ipfs/go-datastore"
ktds "github.com/ipfs/go-datastore/keytransform"
Expand All @@ -29,8 +28,8 @@ func PrefixTransform(prefix ds.Key) ktds.KeyTransform {
panic("expected prefix not found")
}

s := strings.TrimPrefix(k.String(), prefix.String())
return ds.NewKey(s)
s := k.String()[len(prefix.String()):]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should make sure to do a bounds check here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could but then what, panic. Also it is redundant because if prefix.IsAncestorOf passes then there is no way for this to be out of bounds.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, i see. I didnt realize we already had that expectation here.

return ds.RawKey(s)
},
}
}
Expand Down Expand Up @@ -70,7 +69,7 @@ func (d *datastore) Query(q dsq.Query) (dsq.Results, error) {
continue
}

k := ds.NewKey(r.Entry.Key)
k := ds.RawKey(r.Entry.Key)
if !d.prefix.IsAncestorOf(k) {
continue
}
Expand Down