2021-07-15 10:12:10 +00:00
|
|
|
package splitstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2022-06-28 11:09:59 +00:00
|
|
|
ipld "github.com/ipfs/go-ipld-format"
|
2021-07-15 10:12:10 +00:00
|
|
|
|
|
|
|
bstore "github.com/filecoin-project/lotus/blockstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type exposedSplitStore struct {
|
|
|
|
s *SplitStore
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ bstore.Blockstore = (*exposedSplitStore)(nil)
|
|
|
|
|
|
|
|
func (s *SplitStore) Expose() bstore.Blockstore {
|
|
|
|
return &exposedSplitStore{s: s}
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (es *exposedSplitStore) DeleteBlock(_ context.Context, _ cid.Cid) error {
|
2021-07-15 10:12:10 +00:00
|
|
|
return errors.New("DeleteBlock: operation not supported")
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (es *exposedSplitStore) DeleteMany(_ context.Context, _ []cid.Cid) error {
|
2021-07-15 10:12:10 +00:00
|
|
|
return errors.New("DeleteMany: operation not supported")
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (es *exposedSplitStore) Has(ctx context.Context, c cid.Cid) (bool, error) {
|
2021-07-15 10:12:10 +00:00
|
|
|
if isIdentiyCid(c) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
has, err := es.s.hot.Has(ctx, c)
|
2021-07-15 10:12:10 +00:00
|
|
|
if has || err != nil {
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
return es.s.cold.Has(ctx, c)
|
2021-07-15 10:12:10 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (es *exposedSplitStore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
|
2021-07-15 10:12:10 +00:00
|
|
|
if isIdentiyCid(c) {
|
|
|
|
data, err := decodeIdentityCid(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocks.NewBlockWithCid(data, c)
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
blk, err := es.s.hot.Get(ctx, c)
|
2022-06-28 11:09:59 +00:00
|
|
|
if ipld.IsNotFound(err) {
|
2021-12-11 21:03:00 +00:00
|
|
|
return es.s.cold.Get(ctx, c)
|
2021-07-15 10:12:10 +00:00
|
|
|
}
|
2022-06-28 11:09:59 +00:00
|
|
|
return blk, err
|
2021-07-15 10:12:10 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (es *exposedSplitStore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
|
2021-07-15 10:12:10 +00:00
|
|
|
if isIdentiyCid(c) {
|
|
|
|
data, err := decodeIdentityCid(c)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(data), nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
size, err := es.s.hot.GetSize(ctx, c)
|
2022-06-28 11:09:59 +00:00
|
|
|
if ipld.IsNotFound(err) {
|
2021-12-11 21:03:00 +00:00
|
|
|
return es.s.cold.GetSize(ctx, c)
|
2021-07-15 10:12:10 +00:00
|
|
|
}
|
2022-06-28 11:09:59 +00:00
|
|
|
return size, err
|
2021-07-15 10:12:10 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (es *exposedSplitStore) Put(ctx context.Context, blk blocks.Block) error {
|
|
|
|
return es.s.Put(ctx, blk)
|
2021-07-15 10:12:10 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (es *exposedSplitStore) PutMany(ctx context.Context, blks []blocks.Block) error {
|
|
|
|
return es.s.PutMany(ctx, blks)
|
2021-07-15 10:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es *exposedSplitStore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
|
|
|
return es.s.AllKeysChan(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *exposedSplitStore) HashOnRead(enabled bool) {}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (es *exposedSplitStore) View(ctx context.Context, c cid.Cid, f func([]byte) error) error {
|
2021-07-15 10:12:10 +00:00
|
|
|
if isIdentiyCid(c) {
|
|
|
|
data, err := decodeIdentityCid(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f(data)
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
err := es.s.hot.View(ctx, c, f)
|
2022-06-28 11:09:59 +00:00
|
|
|
if ipld.IsNotFound(err) {
|
2021-12-11 21:03:00 +00:00
|
|
|
return es.s.cold.View(ctx, c, f)
|
2021-07-15 10:12:10 +00:00
|
|
|
}
|
2022-06-28 11:09:59 +00:00
|
|
|
|
|
|
|
return err
|
2021-07-15 10:12:10 +00:00
|
|
|
}
|