2021-01-29 20:01:00 +00:00
|
|
|
package blockstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
ds "github.com/ipfs/go-datastore"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
|
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("blockstore")
|
|
|
|
|
|
|
|
var ErrNotFound = blockstore.ErrNotFound
|
|
|
|
|
|
|
|
// Blockstore is the blockstore interface used by Lotus. It is the union
|
|
|
|
// of the basic go-ipfs blockstore, with other capabilities required by Lotus,
|
|
|
|
// e.g. View or Sync.
|
|
|
|
type Blockstore interface {
|
|
|
|
blockstore.Blockstore
|
|
|
|
blockstore.Viewer
|
|
|
|
}
|
|
|
|
|
|
|
|
// BasicBlockstore is an alias to the original IPFS Blockstore.
|
|
|
|
type BasicBlockstore = blockstore.Blockstore
|
|
|
|
|
|
|
|
type Viewer = blockstore.Viewer
|
|
|
|
|
|
|
|
// WrapIDStore wraps the underlying blockstore in an "identity" blockstore.
|
2021-01-29 23:24:44 +00:00
|
|
|
// The ID store filters out all puts for blocks with CIDs using the "identity"
|
|
|
|
// hash function. It also extracts inlined blocks from CIDs using the identity
|
|
|
|
// hash function and returns them on get/has, ignoring the contents of the
|
|
|
|
// blockstore.
|
2021-01-29 20:01:00 +00:00
|
|
|
func WrapIDStore(bstore blockstore.Blockstore) Blockstore {
|
|
|
|
return blockstore.NewIdStore(bstore).(Blockstore)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromDatastore creates a new blockstore backed by the given datastore.
|
|
|
|
func FromDatastore(dstore ds.Batching) Blockstore {
|
|
|
|
return WrapIDStore(blockstore.NewBlockstore(dstore))
|
|
|
|
}
|
|
|
|
|
|
|
|
type adaptedBlockstore struct {
|
|
|
|
blockstore.Blockstore
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Blockstore = (*adaptedBlockstore)(nil)
|
|
|
|
|
|
|
|
func (a *adaptedBlockstore) View(cid cid.Cid, callback func([]byte) error) error {
|
|
|
|
blk, err := a.Get(cid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return callback(blk.RawData())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adapt adapts a standard blockstore to a Lotus blockstore by
|
|
|
|
// enriching it with the extra methods that Lotus requires (e.g. View, Sync).
|
|
|
|
//
|
|
|
|
// View proxies over to Get and calls the callback with the value supplied by Get.
|
|
|
|
// Sync noops.
|
|
|
|
func Adapt(bs blockstore.Blockstore) Blockstore {
|
2021-02-28 19:44:02 +00:00
|
|
|
if ret, ok := bs.(Blockstore); ok {
|
|
|
|
return ret
|
|
|
|
}
|
2021-01-29 20:01:00 +00:00
|
|
|
return &adaptedBlockstore{bs}
|
|
|
|
}
|