2021-03-02 16:06:19 +00:00
|
|
|
package blockstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
mh "github.com/multiformats/go-multihash"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Blockstore = (*idstore)(nil)
|
|
|
|
|
|
|
|
type idstore struct {
|
|
|
|
bs Blockstore
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDStore(bs Blockstore) Blockstore {
|
|
|
|
return &idstore{bs: bs}
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeCid(cid cid.Cid) (inline bool, data []byte, err error) {
|
2021-03-02 16:15:05 +00:00
|
|
|
if cid.Prefix().MhType != mh.IDENTITY {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
2021-03-02 16:06:19 +00:00
|
|
|
dmh, err := mh.Decode(cid.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-02 16:15:05 +00:00
|
|
|
if dmh.Code == mh.IDENTITY {
|
2021-03-02 16:06:19 +00:00
|
|
|
return true, dmh.Digest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (b *idstore) Has(ctx context.Context, cid cid.Cid) (bool, error) {
|
2021-03-02 16:06:19 +00:00
|
|
|
inline, _, err := decodeCid(cid)
|
|
|
|
if err != nil {
|
|
|
|
return false, xerrors.Errorf("error decoding Cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inline {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
return b.bs.Has(ctx, cid)
|
2021-03-02 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (b *idstore) Get(ctx context.Context, cid cid.Cid) (blocks.Block, error) {
|
2021-03-02 16:06:19 +00:00
|
|
|
inline, data, err := decodeCid(cid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("error decoding Cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inline {
|
|
|
|
return blocks.NewBlockWithCid(data, cid)
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
return b.bs.Get(ctx, cid)
|
2021-03-02 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (b *idstore) GetSize(ctx context.Context, cid cid.Cid) (int, error) {
|
2021-03-02 16:06:19 +00:00
|
|
|
inline, data, err := decodeCid(cid)
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("error decoding Cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inline {
|
|
|
|
return len(data), err
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
return b.bs.GetSize(ctx, cid)
|
2021-03-02 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (b *idstore) View(ctx context.Context, cid cid.Cid, cb func([]byte) error) error {
|
2021-03-02 16:06:19 +00:00
|
|
|
inline, data, err := decodeCid(cid)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error decoding Cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inline {
|
|
|
|
return cb(data)
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
return b.bs.View(ctx, cid, cb)
|
2021-03-02 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (b *idstore) Put(ctx context.Context, blk blocks.Block) error {
|
2021-03-02 16:06:19 +00:00
|
|
|
inline, _, err := decodeCid(blk.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error decoding Cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inline {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
return b.bs.Put(ctx, blk)
|
2021-03-02 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (b *idstore) PutMany(ctx context.Context, blks []blocks.Block) error {
|
2021-03-02 16:06:19 +00:00
|
|
|
toPut := make([]blocks.Block, 0, len(blks))
|
|
|
|
for _, blk := range blks {
|
|
|
|
inline, _, err := decodeCid(blk.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error decoding Cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inline {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
toPut = append(toPut, blk)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(toPut) > 0 {
|
2021-11-19 01:50:25 +00:00
|
|
|
return b.bs.PutMany(ctx, toPut)
|
2021-03-02 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (b *idstore) DeleteBlock(ctx context.Context, cid cid.Cid) error {
|
2021-03-02 16:06:19 +00:00
|
|
|
inline, _, err := decodeCid(cid)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error decoding Cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inline {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
return b.bs.DeleteBlock(ctx, cid)
|
2021-03-02 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (b *idstore) DeleteMany(ctx context.Context, cids []cid.Cid) error {
|
2021-03-02 16:06:19 +00:00
|
|
|
toDelete := make([]cid.Cid, 0, len(cids))
|
|
|
|
for _, cid := range cids {
|
|
|
|
inline, _, err := decodeCid(cid)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error decoding Cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inline {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
toDelete = append(toDelete, cid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(toDelete) > 0 {
|
2021-11-19 01:50:25 +00:00
|
|
|
return b.bs.DeleteMany(ctx, toDelete)
|
2021-03-02 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *idstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
|
|
|
return b.bs.AllKeysChan(ctx)
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (b *idstore) HashOnRead(ctx context.Context, enabled bool) {
|
|
|
|
b.bs.HashOnRead(ctx, enabled)
|
2021-03-02 16:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *idstore) Close() error {
|
|
|
|
if c, ok := b.bs.(io.Closer); ok {
|
|
|
|
return c.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|