@cryptotaxi247 / kubo / commits / 1c7e2b245

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

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Nov 21, 2016 at 21:33 UTC 1c7e2b2456d073cf44c1d4cab3c5620c414efb11
7 files changed +17 -21
blocks/blockstore/blockstore.go
+1 -1
@@ -196,7 +196,7 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
196 }
197
198 // need to convert to key.Key using key.KeyFromDsKey.
199 - c, err := dshelp.DsKeyStringToCid(e.Key)
199 + c, err := dshelp.DsKeyToCid(ds.RawKey(e.Key))
200 if err != nil {
201 log.Warningf("error parsing key from DsKey: ", err)
202 return nil, true
namesys/publisher.go
+1 -1
@@ -80,7 +80,7 @@ func (p *ipnsPublisher) PublishWithEOL(ctx context.Context, k ci.PrivKey, value
80 }
81
82 func (p *ipnsPublisher) getPreviousSeqNo(ctx context.Context, ipnskey string) (uint64, error) {
83 - prevrec, err := p.ds.Get(dshelp.NewKeyFromBinary(ipnskey))
83 + prevrec, err := p.ds.Get(dshelp.NewKeyFromBinary([]byte(ipnskey)))
84 if err != nil && err != ds.ErrNotFound {
85 // None found, lets start at zero!
86 return 0, err
namesys/republisher/repub.go
+1 -1
@@ -108,7 +108,7 @@ func (rp *Republisher) republishEntries(p goprocess.Process) error {
108 }
109
110 func (rp *Republisher) getLastVal(k string) (path.Path, uint64, error) {
111 - ival, err := rp.ds.Get(dshelp.NewKeyFromBinary(k))
111 + ival, err := rp.ds.Get(dshelp.NewKeyFromBinary([]byte(k)))
112 if err != nil {
113 // not found means we dont have a previously published entry
114 return "", 0, errNoEntry
routing/mock/centralized_client.go
+2 -2
@@ -40,13 +40,13 @@ func (c *client) PutValue(ctx context.Context, key string, val []byte) error {
40 return err
41 }
42
43 - return c.datastore.Put(dshelp.NewKeyFromBinary(key), data)
43 + return c.datastore.Put(dshelp.NewKeyFromBinary([]byte(key)), data)
44 }
45
46 // FIXME(brian): is this method meant to simulate getting a value from the network?
47 func (c *client) GetValue(ctx context.Context, key string) ([]byte, error) {
48 log.Debugf("GetValue: %s", key)
49 - v, err := c.datastore.Get(dshelp.NewKeyFromBinary(key))
49 + v, err := c.datastore.Get(dshelp.NewKeyFromBinary([]byte(key)))
50 if err != nil {
51 return nil, err
52 }
routing/offline/offline.go
+3 -3
@@ -48,11 +48,11 @@ func (c *offlineRouting) PutValue(ctx context.Context, key string, val []byte) e
48 return err
49 }
50
51 - return c.datastore.Put(dshelp.NewKeyFromBinary(key), data)
51 + return c.datastore.Put(dshelp.NewKeyFromBinary([]byte(key)), data)
52 }
53
54 func (c *offlineRouting) GetValue(ctx context.Context, key string) ([]byte, error) {
55 - v, err := c.datastore.Get(dshelp.NewKeyFromBinary(key))
55 + v, err := c.datastore.Get(dshelp.NewKeyFromBinary([]byte(key)))
56 if err != nil {
57 return nil, err
58 }
@@ -71,7 +71,7 @@ func (c *offlineRouting) GetValue(ctx context.Context, key string) ([]byte, erro
71 }
72
73 func (c *offlineRouting) GetValues(ctx context.Context, key string, _ int) ([]routing.RecvdVal, error) {
74 - v, err := c.datastore.Get(dshelp.NewKeyFromBinary(key))
74 + v, err := c.datastore.Get(dshelp.NewKeyFromBinary([]byte(key)))
75 if err != nil {
76 return nil, err
77 }
routing/supernode/server.go
+2 -2
@@ -117,7 +117,7 @@ var _ proxy.RequestHandler = &Server{}
117 var _ proxy.Proxy = &Server{}
118
119 func getRoutingRecord(ds datastore.Datastore, k string) (*pb.Record, error) {
120 - dskey := dshelp.NewKeyFromBinary(k)
120 + dskey := dshelp.NewKeyFromBinary([]byte(k))
121 val, err := ds.Get(dskey)
122 if err != nil {
123 return nil, err
@@ -138,7 +138,7 @@ func putRoutingRecord(ds datastore.Datastore, k string, value *pb.Record) error
138 if err != nil {
139 return err
140 }
141 - dskey := dshelp.NewKeyFromBinary(k)
141 + dskey := dshelp.NewKeyFromBinary([]byte(k))
142 // TODO namespace
143 if err := ds.Put(dskey, data); err != nil {
144 return err
thirdparty/ds-help/key.go
+7 -11
@@ -7,8 +7,12 @@ import (
7 )
8
9 // TODO: put this code into the go-datastore itself
10 -func NewKeyFromBinary(s string) ds.Key {
11 - return ds.NewKey(base32.RawStdEncoding.EncodeToString([]byte(s)))
10 +
11 +func NewKeyFromBinary(rawKey []byte) ds.Key {
12 + buf := make([]byte, 1+base32.RawStdEncoding.EncodedLen(len(rawKey)))
13 + buf[0] = '/'
14 + base32.RawStdEncoding.Encode(buf[1:], rawKey)
15 + return ds.RawKey(string(buf))
16 }
17
18 func BinaryFromDsKey(k ds.Key) ([]byte, error) {
@@ -16,7 +20,7 @@ func BinaryFromDsKey(k ds.Key) ([]byte, error) {
20 }
21
22 func CidToDsKey(k *cid.Cid) ds.Key {
19 - return NewKeyFromBinary(k.KeyString())
23 + return NewKeyFromBinary(k.Bytes())
24 }
25
26 func DsKeyToCid(dsKey ds.Key) (*cid.Cid, error) {
@@ -26,11 +30,3 @@ func DsKeyToCid(dsKey ds.Key) (*cid.Cid, error) {
30 }
31 return cid.Cast(kb)
32 }
29 -
30 -func DsKeyStringToCid(dsKey string) (*cid.Cid, error) {
31 - kb, err := base32.RawStdEncoding.DecodeString(dsKey[1:])
32 - if err != nil {
33 - return nil, err
34 - }
35 - return cid.Cast(kb)
36 -}