mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-10 18:57:57 +08:00
ds-help: add helper func to convert from Cid to DsKey and the reverse
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
This commit is contained in:
parent
d5c716a3f0
commit
c4fbe348f8
@ -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
|
||||
|
||||
@ -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))
|
||||
|
||||
13
thirdparty/ds-help/key.go
vendored
13
thirdparty/ds-help/key.go
vendored
@ -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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user