Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ds-help: add helper functions to convert from Cid to DsKey and the reverse #3310

Merged
merged 1 commit into from
Oct 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions blocks/blockstore/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion blocks/blockstore/blockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
13 changes: 13 additions & 0 deletions thirdparty/ds-help/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}