ds-help: add helper func to convert from Cid to DsKey and the reverse
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Kevin Atkinson committed
Oct 17, 2016 at 18:13 UTC
c4fbe348f85369378383e5ec9f5ddadff2972ace
3 files changed
+20
-12
blocks/blockstore/blockstore.go
+6
-11
@@ -87,7 +87,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) {
87
return nil, ErrNotFound
88
}
89
90
- maybeData, err := bs.datastore.Get(dshelp.NewKeyFromBinary(k.KeyString()))
90
+ maybeData, err := bs.datastore.Get(dshelp.CidToDsKey(k))
91
if err == ds.ErrNotFound {
92
return nil, ErrNotFound
93
}
@@ -112,7 +112,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) {
112
}
113
114
func (bs *blockstore) Put(block blocks.Block) error {
115
- k := dshelp.NewKeyFromBinary(block.Cid().KeyString())
115
+ k := dshelp.CidToDsKey(block.Cid())
116
117
// Has is cheaper than Put, so see if we already have it
118
exists, err := bs.datastore.Has(k)
@@ -128,7 +128,7 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error {
128
return err
129
}
130
for _, b := range blocks {
131
- k := dshelp.NewKeyFromBinary(b.Cid().KeyString())
131
+ k := dshelp.CidToDsKey(b.Cid())
132
exists, err := bs.datastore.Has(k)
133
if err == nil && exists {
134
continue
@@ -143,11 +143,11 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error {
143
}
144
145
func (bs *blockstore) Has(k *cid.Cid) (bool, error) {
146
- return bs.datastore.Has(dshelp.NewKeyFromBinary(k.KeyString()))
146
+ return bs.datastore.Has(dshelp.CidToDsKey(k))
147
}
148
149
func (s *blockstore) DeleteBlock(k *cid.Cid) error {
150
- return s.datastore.Delete(dshelp.NewKeyFromBinary(k.KeyString()))
150
+ return s.datastore.Delete(dshelp.CidToDsKey(k))
151
}
152
153
// AllKeysChan runs a query for keys from the blockstore.
@@ -180,17 +180,12 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
180
}
181
182
// need to convert to key.Key using key.KeyFromDsKey.
183
- kb, err := dshelp.BinaryFromDsKey(ds.NewKey(e.Key)) // TODO: calling NewKey isnt free
183
+ c, err := dshelp.DsKeyToCid(ds.NewKey(e.Key)) // TODO: calling NewKey isnt free
184
if err != nil {
185
log.Warningf("error parsing key from DsKey: ", err)
186
return nil, true
187
}
188
189
- c, err := cid.Cast(kb)
190
- if err != nil {
191
- log.Warning("error parsing cid from decoded DsKey: ", err)
192
- return nil, true
193
- }
189
log.Debug("blockstore: query got key", c)
190
191
return c, true
blocks/blockstore/blockstore_test.go
+1
-1
@@ -190,7 +190,7 @@ func TestValueTypeMismatch(t *testing.T) {
190
block := blocks.NewBlock([]byte("some data"))
191
192
datastore := ds.NewMapDatastore()
193
- k := BlockPrefix.Child(dshelp.NewKeyFromBinary(block.Cid().KeyString()))
193
+ k := BlockPrefix.Child(dshelp.CidToDsKey(block.Cid()))
194
datastore.Put(k, "data that isn't a block!")
195
196
blockstore := NewBlockstore(ds_sync.MutexWrap(datastore))
thirdparty/ds-help/key.go
+13
@@ -3,6 +3,7 @@ package dshelp
3
import (
4
base32 "gx/ipfs/Qmb1DA2A9LS2wR4FFweB4uEDomFsdmnw1VLawLE1yQzudj/base32"
5
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
6
+ cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
7
)
8
9
// TODO: put this code into the go-datastore itself
@@ -13,3 +14,15 @@ func NewKeyFromBinary(s string) ds.Key {
14
func BinaryFromDsKey(k ds.Key) ([]byte, error) {
15
return base32.RawStdEncoding.DecodeString(k.String()[1:])
16
}
17
+
18
+func CidToDsKey(k *cid.Cid) ds.Key {
19
+ return NewKeyFromBinary(k.KeyString())
20
+}
21
+
22
+func DsKeyToCid(dsKey ds.Key) (*cid.Cid, error) {
23
+ kb, err := BinaryFromDsKey(dsKey)
24
+ if err != nil {
25
+ return nil, err
26
+ }
27
+ return cid.Cast(kb)
28
+}