Skip to content

Commit

Permalink
blockstore: add helper functions to convert from Cid to DsKey and back
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Oct 16, 2016
1 parent d5c716a commit 2b09019
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
18 changes: 6 additions & 12 deletions blocks/blockstore/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync/atomic"

blocks "github.com/ipfs/go-ipfs/blocks"
dshelp "github.com/ipfs/go-ipfs/thirdparty/ds-help"

logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
Expand Down Expand Up @@ -87,7 +86,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(CidToDsKey(k))
if err == ds.ErrNotFound {
return nil, ErrNotFound
}
Expand All @@ -112,7 +111,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 := 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 +127,7 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error {
return err
}
for _, b := range blocks {
k := dshelp.NewKeyFromBinary(b.Cid().KeyString())
k := CidToDsKey(b.Cid())
exists, err := bs.datastore.Has(k)
if err == nil && exists {
continue
Expand All @@ -143,11 +142,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(CidToDsKey(k))
}

func (s *blockstore) DeleteBlock(k *cid.Cid) error {
return s.datastore.Delete(dshelp.NewKeyFromBinary(k.KeyString()))
return s.datastore.Delete(CidToDsKey(k))
}

// AllKeysChan runs a query for keys from the blockstore.
Expand Down Expand Up @@ -180,17 +179,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 := 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
3 changes: 1 addition & 2 deletions blocks/blockstore/blockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

blocks "github.com/ipfs/go-ipfs/blocks"
dshelp "github.com/ipfs/go-ipfs/thirdparty/ds-help"

cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
Expand Down Expand Up @@ -190,7 +189,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(CidToDsKey(block.Cid()))
datastore.Put(k, "data that isn't a block!")

blockstore := NewBlockstore(ds_sync.MutexWrap(datastore))
Expand Down
20 changes: 20 additions & 0 deletions blocks/blockstore/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package blockstore

import (
dshelp "github.com/ipfs/go-ipfs/thirdparty/ds-help"

cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
)

func CidToDsKey(k *cid.Cid) ds.Key {
return dshelp.NewKeyFromBinary(k.KeyString())
}

func DsKeyToCid(dsKey ds.Key) (*cid.Cid, error) {
kb, err := dshelp.BinaryFromDsKey(dsKey)
if err != nil {
return nil, err
}
return cid.Cast(kb)
}

0 comments on commit 2b09019

Please sign in to comment.