diff --git a/blocks/blockstore/blockstore.go b/blocks/blockstore/blockstore.go index dfa35ec413a..861863d9d79 100644 --- a/blocks/blockstore/blockstore.go +++ b/blocks/blockstore/blockstore.go @@ -87,7 +87,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) { return nil, ErrNotFound } - maybeData, err := bs.datastore.Get(dshelp.NewKeyFromBinary(k.KeyString())) + maybeData, err := bs.datastore.Get(dshelp.CidToDsKey(k)) if err == ds.ErrNotFound { return nil, ErrNotFound } @@ -112,7 +112,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) { } func (bs *blockstore) Put(block blocks.Block) error { - k := dshelp.NewKeyFromBinary(block.Cid().KeyString()) + k := dshelp.CidToDsKey(block.Cid()) // Has is cheaper than Put, so see if we already have it exists, err := bs.datastore.Has(k) @@ -128,7 +128,7 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error { return err } for _, b := range blocks { - k := dshelp.NewKeyFromBinary(b.Cid().KeyString()) + k := dshelp.CidToDsKey(b.Cid()) exists, err := bs.datastore.Has(k) if err == nil && exists { continue @@ -143,11 +143,11 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error { } func (bs *blockstore) Has(k *cid.Cid) (bool, error) { - return bs.datastore.Has(dshelp.NewKeyFromBinary(k.KeyString())) + return bs.datastore.Has(dshelp.CidToDsKey(k)) } func (s *blockstore) DeleteBlock(k *cid.Cid) error { - return s.datastore.Delete(dshelp.NewKeyFromBinary(k.KeyString())) + return s.datastore.Delete(dshelp.CidToDsKey(k)) } // AllKeysChan runs a query for keys from the blockstore. @@ -180,17 +180,12 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) } // need to convert to key.Key using key.KeyFromDsKey. - kb, err := dshelp.BinaryFromDsKey(ds.NewKey(e.Key)) // TODO: calling NewKey isnt free + c, err := dshelp.DsKeyToCid(ds.NewKey(e.Key)) // TODO: calling NewKey isnt free if err != nil { log.Warningf("error parsing key from DsKey: ", err) return nil, true } - c, err := cid.Cast(kb) - if err != nil { - log.Warning("error parsing cid from decoded DsKey: ", err) - return nil, true - } log.Debug("blockstore: query got key", c) return c, true diff --git a/blocks/blockstore/blockstore_test.go b/blocks/blockstore/blockstore_test.go index a5ecefd4417..4c1a4db8815 100644 --- a/blocks/blockstore/blockstore_test.go +++ b/blocks/blockstore/blockstore_test.go @@ -190,7 +190,7 @@ func TestValueTypeMismatch(t *testing.T) { block := blocks.NewBlock([]byte("some data")) datastore := ds.NewMapDatastore() - k := BlockPrefix.Child(dshelp.NewKeyFromBinary(block.Cid().KeyString())) + k := BlockPrefix.Child(dshelp.CidToDsKey(block.Cid())) datastore.Put(k, "data that isn't a block!") blockstore := NewBlockstore(ds_sync.MutexWrap(datastore)) diff --git a/thirdparty/ds-help/key.go b/thirdparty/ds-help/key.go index 417f4960571..7db86aedb85 100644 --- a/thirdparty/ds-help/key.go +++ b/thirdparty/ds-help/key.go @@ -3,6 +3,7 @@ package dshelp import ( base32 "gx/ipfs/Qmb1DA2A9LS2wR4FFweB4uEDomFsdmnw1VLawLE1yQzudj/base32" ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore" + cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid" ) // TODO: put this code into the go-datastore itself @@ -13,3 +14,15 @@ func NewKeyFromBinary(s string) ds.Key { 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()) +} + +func DsKeyToCid(dsKey ds.Key) (*cid.Cid, error) { + kb, err := BinaryFromDsKey(dsKey) + if err != nil { + return nil, err + } + return cid.Cast(kb) +}