From 994a3aa9f1f5c7ca25708ee3de92521ab446db4c Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Mon, 21 Nov 2016 21:33:28 -0500 Subject: [PATCH] ds-help: avoid unnecessary allocs when posssible and make use of RawKey License: MIT Signed-off-by: Kevin Atkinson --- blocks/blockstore/blockstore.go | 2 +- namesys/publisher.go | 2 +- namesys/republisher/repub.go | 2 +- routing/mock/centralized_client.go | 4 ++-- routing/offline/offline.go | 6 +++--- thirdparty/ds-help/key.go | 18 +++++++----------- 6 files changed, 15 insertions(+), 19 deletions(-) diff --git a/blocks/blockstore/blockstore.go b/blocks/blockstore/blockstore.go index 40763276b7e..9e5d8ca809b 100644 --- a/blocks/blockstore/blockstore.go +++ b/blocks/blockstore/blockstore.go @@ -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 diff --git a/namesys/publisher.go b/namesys/publisher.go index 345f21e339b..795023c83c6 100644 --- a/namesys/publisher.go +++ b/namesys/publisher.go @@ -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 diff --git a/namesys/republisher/repub.go b/namesys/republisher/repub.go index 9abe8688e12..e4e0bdc928a 100644 --- a/namesys/republisher/repub.go +++ b/namesys/republisher/repub.go @@ -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 diff --git a/routing/mock/centralized_client.go b/routing/mock/centralized_client.go index 1a413ab9c10..135f3be99ee 100644 --- a/routing/mock/centralized_client.go +++ b/routing/mock/centralized_client.go @@ -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 } diff --git a/routing/offline/offline.go b/routing/offline/offline.go index 1e3297bb399..566466139be 100644 --- a/routing/offline/offline.go +++ b/routing/offline/offline.go @@ -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 } @@ -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 } diff --git a/thirdparty/ds-help/key.go b/thirdparty/ds-help/key.go index 256cb15922e..7e962fff045 100644 --- a/thirdparty/ds-help/key.go +++ b/thirdparty/ds-help/key.go @@ -7,8 +7,12 @@ 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) { @@ -16,7 +20,7 @@ func BinaryFromDsKey(k ds.Key) ([]byte, error) { } func CidToDsKey(k *cid.Cid) ds.Key { - return NewKeyFromBinary(k.KeyString()) + return NewKeyFromBinary(k.Bytes()) } func DsKeyToCid(dsKey ds.Key) (*cid.Cid, error) { @@ -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) -}