2019-07-08 20:29:01 +00:00
|
|
|
package bufbstore
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-01 20:37:34 +00:00
|
|
|
"os"
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
block "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
ds "github.com/ipfs/go-datastore"
|
|
|
|
bstore "github.com/ipfs/go-ipfs-blockstore"
|
2020-04-01 20:37:34 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
2020-04-01 20:37:34 +00:00
|
|
|
var log = logging.Logger("bufbs")
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
type BufferedBS struct {
|
|
|
|
read bstore.Blockstore
|
|
|
|
write bstore.Blockstore
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBufferedBstore(base bstore.Blockstore) *BufferedBS {
|
|
|
|
buf := bstore.NewBlockstore(ds.NewMapDatastore())
|
2020-04-01 20:37:34 +00:00
|
|
|
if os.Getenv("LOTUS_DISABLE_VM_BUF") == "iknowitsabadidea" {
|
|
|
|
log.Warn("VM BLOCKSTORE BUFFERING IS DISABLED")
|
|
|
|
buf = base
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
return &BufferedBS{
|
|
|
|
read: base,
|
|
|
|
write: buf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:51:55 +00:00
|
|
|
func NewTieredBstore(r bstore.Blockstore, w bstore.Blockstore) *BufferedBS {
|
|
|
|
return &BufferedBS{
|
|
|
|
read: r,
|
|
|
|
write: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
var _ (bstore.Blockstore) = &BufferedBS{}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
|
|
|
a, err := bs.read.AllKeysChan(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := bs.write.AllKeysChan(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make(chan cid.Cid)
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
for a != nil || b != nil {
|
|
|
|
select {
|
|
|
|
case val, ok := <-a:
|
|
|
|
if !ok {
|
|
|
|
a = nil
|
|
|
|
} else {
|
|
|
|
select {
|
|
|
|
case out <- val:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case val, ok := <-b:
|
|
|
|
if !ok {
|
|
|
|
b = nil
|
|
|
|
} else {
|
|
|
|
select {
|
|
|
|
case out <- val:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) DeleteBlock(c cid.Cid) error {
|
|
|
|
if err := bs.read.DeleteBlock(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs.write.DeleteBlock(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) Get(c cid.Cid) (block.Block, error) {
|
|
|
|
if out, err := bs.read.Get(c); err != nil {
|
|
|
|
if err != bstore.ErrNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs.write.Get(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) GetSize(c cid.Cid) (int, error) {
|
|
|
|
panic("nyi")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) Put(blk block.Block) error {
|
|
|
|
return bs.write.Put(blk)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) Has(c cid.Cid) (bool, error) {
|
|
|
|
has, err := bs.read.Has(c)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return bs.write.Has(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) HashOnRead(hor bool) {
|
|
|
|
bs.read.HashOnRead(hor)
|
|
|
|
bs.write.HashOnRead(hor)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BufferedBS) PutMany(blks []block.Block) error {
|
|
|
|
return bs.write.PutMany(blks)
|
2019-07-05 14:36:08 +00:00
|
|
|
}
|
2019-07-08 20:29:01 +00:00
|
|
|
|
|
|
|
func (bs *BufferedBS) Read() bstore.Blockstore {
|
|
|
|
return bs.read
|
|
|
|
}
|