Merge pull request #5352 from ipfs/fix/v0v1-get-size

add GetSize method to the v0v1 blockstore
This commit is contained in:
Steven Allen 2018-08-08 17:52:13 +00:00 committed by GitHub
commit 04f7f2845b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,9 @@ package cidv0v1
import (
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
bs "gx/ipfs/QmTCHqj6s51pDu1GaPGyBW2VdmCUvtzLCF6nWykfX9ZYRt/go-ipfs-blockstore"
blocks "gx/ipfs/QmVzK524a2VWLqyvtBeiHKsUAWYgeAk4DBeZoY7vpNPNRx/go-block-format"
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
bs "gx/ipfs/QmadMhXJLHMFjpRmh85XjpmVDkEtQpNYEZNRpWRvYVLrvb/go-ipfs-blockstore"
)
type blockstore struct {
@ -57,6 +57,21 @@ func (b *blockstore) Get(c *cid.Cid) (blocks.Block, error) {
return block, nil
}
func (b *blockstore) GetSize(c *cid.Cid) (int, error) {
size, err := b.Blockstore.GetSize(c)
if err == nil {
return size, nil
}
if err != bs.ErrNotFound {
return -1, err
}
c1 := tryOtherCidVersion(c)
if c1 == nil {
return -1, bs.ErrNotFound
}
return b.Blockstore.GetSize(c1)
}
func tryOtherCidVersion(c *cid.Cid) *cid.Cid {
prefix := c.Prefix()
if prefix.Codec != cid.DagProtobuf || prefix.MhType != mh.SHA2_256 || prefix.MhLength != 32 {