Skip to content

Commit

Permalink
ds-help: avoid unnecessary allocs when posssible and make use of RawKey
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Nov 29, 2016
1 parent f81cccc commit 994a3aa
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 19 deletions.
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
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)
}

0 comments on commit 994a3aa

Please sign in to comment.