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

ds-help: avoid unnecessary allocs when posssible and make use of RawKey #3407

Merged
merged 1 commit into from
Nov 30, 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
2 changes: 1 addition & 1 deletion blocks/blockstore/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
}

// need to convert to key.Key using key.KeyFromDsKey.
c, err := dshelp.DsKeyStringToCid(e.Key)
c, err := dshelp.DsKeyToCid(ds.RawKey(e.Key))
if err != nil {
log.Warningf("error parsing key from DsKey: ", err)
return nil, true
Expand Down
2 changes: 1 addition & 1 deletion namesys/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (p *ipnsPublisher) PublishWithEOL(ctx context.Context, k ci.PrivKey, value
}

func (p *ipnsPublisher) getPreviousSeqNo(ctx context.Context, ipnskey string) (uint64, error) {
prevrec, err := p.ds.Get(dshelp.NewKeyFromBinary(ipnskey))
prevrec, err := p.ds.Get(dshelp.NewKeyFromBinary([]byte(ipnskey)))
if err != nil && err != ds.ErrNotFound {
// None found, lets start at zero!
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion namesys/republisher/repub.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (rp *Republisher) republishEntries(p goprocess.Process) error {
}

func (rp *Republisher) getLastVal(k string) (path.Path, uint64, error) {
ival, err := rp.ds.Get(dshelp.NewKeyFromBinary(k))
ival, err := rp.ds.Get(dshelp.NewKeyFromBinary([]byte(k)))
if err != nil {
// not found means we dont have a previously published entry
return "", 0, errNoEntry
Expand Down
4 changes: 2 additions & 2 deletions routing/mock/centralized_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ func (c *client) PutValue(ctx context.Context, key string, val []byte) error {
return err
}

return c.datastore.Put(dshelp.NewKeyFromBinary(key), data)
return c.datastore.Put(dshelp.NewKeyFromBinary([]byte(key)), data)
}

// FIXME(brian): is this method meant to simulate getting a value from the network?
func (c *client) GetValue(ctx context.Context, key string) ([]byte, error) {
log.Debugf("GetValue: %s", key)
v, err := c.datastore.Get(dshelp.NewKeyFromBinary(key))
v, err := c.datastore.Get(dshelp.NewKeyFromBinary([]byte(key)))
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions routing/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func (c *offlineRouting) PutValue(ctx context.Context, key string, val []byte) e
return err
}

return c.datastore.Put(dshelp.NewKeyFromBinary(key), data)
return c.datastore.Put(dshelp.NewKeyFromBinary([]byte(key)), data)
}

func (c *offlineRouting) GetValue(ctx context.Context, key string) ([]byte, error) {
v, err := c.datastore.Get(dshelp.NewKeyFromBinary(key))
v, err := c.datastore.Get(dshelp.NewKeyFromBinary([]byte(key)))
if err != nil {
return nil, err
}
Expand All @@ -71,7 +71,7 @@ func (c *offlineRouting) GetValue(ctx context.Context, key string) ([]byte, erro
}

func (c *offlineRouting) GetValues(ctx context.Context, key string, _ int) ([]routing.RecvdVal, error) {
v, err := c.datastore.Get(dshelp.NewKeyFromBinary(key))
v, err := c.datastore.Get(dshelp.NewKeyFromBinary([]byte(key)))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions routing/supernode/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var _ proxy.RequestHandler = &Server{}
var _ proxy.Proxy = &Server{}

func getRoutingRecord(ds datastore.Datastore, k string) (*pb.Record, error) {
dskey := dshelp.NewKeyFromBinary(k)
dskey := dshelp.NewKeyFromBinary([]byte(k))
val, err := ds.Get(dskey)
if err != nil {
return nil, err
Expand All @@ -138,7 +138,7 @@ func putRoutingRecord(ds datastore.Datastore, k string, value *pb.Record) error
if err != nil {
return err
}
dskey := dshelp.NewKeyFromBinary(k)
dskey := dshelp.NewKeyFromBinary([]byte(k))
// TODO namespace
if err := ds.Put(dskey, data); err != nil {
return err
Expand Down
18 changes: 7 additions & 11 deletions thirdparty/ds-help/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import (
)

// TODO: put this code into the go-datastore itself
func NewKeyFromBinary(s string) ds.Key {
return ds.NewKey(base32.RawStdEncoding.EncodeToString([]byte(s)))

func NewKeyFromBinary(rawKey []byte) ds.Key {
buf := make([]byte, 1+base32.RawStdEncoding.EncodedLen(len(rawKey)))
buf[0] = '/'
base32.RawStdEncoding.Encode(buf[1:], rawKey)
return ds.RawKey(string(buf))
}

func BinaryFromDsKey(k ds.Key) ([]byte, error) {
return base32.RawStdEncoding.DecodeString(k.String()[1:])
}

func CidToDsKey(k *cid.Cid) ds.Key {
return NewKeyFromBinary(k.KeyString())
return NewKeyFromBinary(k.Bytes())
}

func DsKeyToCid(dsKey ds.Key) (*cid.Cid, error) {
Expand All @@ -26,11 +30,3 @@ func DsKeyToCid(dsKey ds.Key) (*cid.Cid, error) {
}
return cid.Cast(kb)
}

func DsKeyStringToCid(dsKey string) (*cid.Cid, error) {
kb, err := base32.RawStdEncoding.DecodeString(dsKey[1:])
if err != nil {
return nil, err
}
return cid.Cast(kb)
}