2021-01-29 20:01:00 +00:00
|
|
|
package blockstore
|
2020-02-04 02:45:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ChainIO interface {
|
|
|
|
ChainReadObj(context.Context, cid.Cid) ([]byte, error)
|
|
|
|
ChainHasObj(context.Context, cid.Cid) (bool, error)
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
type apiBlockstore struct {
|
2020-02-04 02:45:20 +00:00
|
|
|
api ChainIO
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:01:00 +00:00
|
|
|
// This blockstore is adapted in the constructor.
|
2021-01-29 23:17:25 +00:00
|
|
|
var _ BasicBlockstore = (*apiBlockstore)(nil)
|
2021-01-29 20:01:00 +00:00
|
|
|
|
|
|
|
func NewAPIBlockstore(cio ChainIO) Blockstore {
|
2021-01-29 23:17:25 +00:00
|
|
|
bs := &apiBlockstore{api: cio}
|
2021-01-29 20:01:00 +00:00
|
|
|
return Adapt(bs) // return an adapted blockstore.
|
2020-02-04 03:37:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (a *apiBlockstore) DeleteBlock(context.Context, cid.Cid) error {
|
2020-02-04 02:45:20 +00:00
|
|
|
return xerrors.New("not supported")
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (a *apiBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
|
|
|
|
return a.api.ChainHasObj(ctx, c)
|
2020-02-04 02:45:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (a *apiBlockstore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
|
|
|
|
bb, err := a.api.ChainReadObj(ctx, c)
|
2020-02-04 02:45:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return blocks.NewBlockWithCid(bb, c)
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (a *apiBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
|
|
|
|
bb, err := a.api.ChainReadObj(ctx, c)
|
2020-02-04 02:45:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(bb), nil
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (a *apiBlockstore) Put(context.Context, blocks.Block) error {
|
2020-02-04 02:45:20 +00:00
|
|
|
return xerrors.New("not supported")
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (a *apiBlockstore) PutMany(context.Context, []blocks.Block) error {
|
2020-02-04 02:45:20 +00:00
|
|
|
return xerrors.New("not supported")
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
func (a *apiBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
2020-02-04 02:45:20 +00:00
|
|
|
return nil, xerrors.New("not supported")
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (a *apiBlockstore) HashOnRead(enabled bool) {
|
2020-02-04 02:45:20 +00:00
|
|
|
return
|
|
|
|
}
|