Commit 716aa867 authored by Jeromy Johnson's avatar Jeromy Johnson Committed by GitHub

Merge pull request #3310 from ipfs/kevina/cid2dskey

ds-help: add helper functions to convert from Cid to DsKey and the reverse
parents 62fcf7e9 c4fbe348
...@@ -87,7 +87,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) { ...@@ -87,7 +87,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) {
return nil, ErrNotFound return nil, ErrNotFound
} }
maybeData, err := bs.datastore.Get(dshelp.NewKeyFromBinary(k.KeyString())) maybeData, err := bs.datastore.Get(dshelp.CidToDsKey(k))
if err == ds.ErrNotFound { if err == ds.ErrNotFound {
return nil, ErrNotFound return nil, ErrNotFound
} }
...@@ -112,7 +112,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) { ...@@ -112,7 +112,7 @@ func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) {
} }
func (bs *blockstore) Put(block 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 // Has is cheaper than Put, so see if we already have it
exists, err := bs.datastore.Has(k) exists, err := bs.datastore.Has(k)
...@@ -128,7 +128,7 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error { ...@@ -128,7 +128,7 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error {
return err return err
} }
for _, b := range blocks { for _, b := range blocks {
k := dshelp.NewKeyFromBinary(b.Cid().KeyString()) k := dshelp.CidToDsKey(b.Cid())
exists, err := bs.datastore.Has(k) exists, err := bs.datastore.Has(k)
if err == nil && exists { if err == nil && exists {
continue continue
...@@ -143,11 +143,11 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error { ...@@ -143,11 +143,11 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error {
} }
func (bs *blockstore) Has(k *cid.Cid) (bool, 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 { 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. // 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,17 +180,12 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
} }
// need to convert to key.Key using key.KeyFromDsKey. // 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 { if err != nil {
log.Warningf("error parsing key from DsKey: ", err) log.Warningf("error parsing key from DsKey: ", err)
return nil, true 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) log.Debug("blockstore: query got key", c)
return c, true return c, true
......
...@@ -190,7 +190,7 @@ func TestValueTypeMismatch(t *testing.T) { ...@@ -190,7 +190,7 @@ func TestValueTypeMismatch(t *testing.T) {
block := blocks.NewBlock([]byte("some data")) block := blocks.NewBlock([]byte("some data"))
datastore := ds.NewMapDatastore() 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!") datastore.Put(k, "data that isn't a block!")
blockstore := NewBlockstore(ds_sync.MutexWrap(datastore)) blockstore := NewBlockstore(ds_sync.MutexWrap(datastore))
......
...@@ -3,6 +3,7 @@ package dshelp ...@@ -3,6 +3,7 @@ package dshelp
import ( import (
base32 "gx/ipfs/Qmb1DA2A9LS2wR4FFweB4uEDomFsdmnw1VLawLE1yQzudj/base32" base32 "gx/ipfs/Qmb1DA2A9LS2wR4FFweB4uEDomFsdmnw1VLawLE1yQzudj/base32"
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore" ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
) )
// TODO: put this code into the go-datastore itself // TODO: put this code into the go-datastore itself
...@@ -13,3 +14,15 @@ func NewKeyFromBinary(s string) ds.Key { ...@@ -13,3 +14,15 @@ func NewKeyFromBinary(s string) ds.Key {
func BinaryFromDsKey(k ds.Key) ([]byte, error) { func BinaryFromDsKey(k ds.Key) ([]byte, error) {
return base32.RawStdEncoding.DecodeString(k.String()[1:]) 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)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment