lotus/blockstore/autobatch.go

138 lines
3.4 KiB
Go
Raw Normal View History

2022-01-11 16:31:59 +00:00
package blockstore
import (
"context"
"sync"
2022-01-11 22:44:45 +00:00
"golang.org/x/xerrors"
2022-01-11 16:31:59 +00:00
block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
)
// autolog is a logger for the autobatching blockstore. It is subscoped from the
// blockstore logger.
var autolog = log.Named("auto")
type AutobatchBlockstore struct {
bufferedBlks []block.Block
2022-01-11 22:17:34 +00:00
addedCids map[cid.Cid]struct{}
2022-01-11 16:31:59 +00:00
bufferedBlksLk sync.Mutex
flushLk sync.Mutex
backingBs Blockstore
bufferCapacity int
bufferSize int
}
func NewAutobatch(ctx context.Context, backingBs Blockstore, bufferCapacity int) *AutobatchBlockstore {
bs := &AutobatchBlockstore{
backingBs: backingBs,
bufferCapacity: bufferCapacity,
2022-01-11 22:17:34 +00:00
addedCids: make(map[cid.Cid]struct{}),
2022-01-11 16:31:59 +00:00
}
return bs
}
func (bs *AutobatchBlockstore) Put(ctx context.Context, blk block.Block) error {
bs.bufferedBlksLk.Lock()
2022-01-11 22:17:34 +00:00
_, ok := bs.addedCids[blk.Cid()]
if !ok {
bs.bufferedBlks = append(bs.bufferedBlks, blk)
bs.addedCids[blk.Cid()] = struct{}{}
bs.bufferSize += len(blk.RawData())
if bs.bufferSize >= bs.bufferCapacity {
// time to flush
go bs.Flush(ctx)
}
2022-01-11 16:31:59 +00:00
}
bs.bufferedBlksLk.Unlock()
return nil
}
func (bs *AutobatchBlockstore) Flush(ctx context.Context) {
bs.flushLk.Lock()
defer bs.flushLk.Unlock()
bs.bufferedBlksLk.Lock()
2022-01-11 22:44:45 +00:00
// We do NOT clear addedCids here, because its purpose is to expedite Puts
2022-01-11 16:31:59 +00:00
toFlush := bs.bufferedBlks
bs.bufferedBlks = []block.Block{}
bs.bufferedBlksLk.Unlock()
2022-01-11 22:44:45 +00:00
err := bs.backingBs.PutMany(ctx, toFlush)
autolog.Errorf("FLUSH ERRORED, maybe async: %w", err)
}
// May be very slow if the cid queried wasn't in the backingBs at the time of creation of this AutobatchBlockstore
func (bs *AutobatchBlockstore) Get(ctx context.Context, c cid.Cid) (block.Block, error) {
blk, err := bs.backingBs.Get(ctx, c)
if err == nil {
return blk, nil
}
if err != ErrNotFound {
return blk, err
}
bs.Flush(ctx)
return bs.backingBs.Get(ctx, c)
}
func (bs *AutobatchBlockstore) DeleteBlock(context.Context, cid.Cid) error {
// if we wanted to support this, we would have to:
// - flush
// - delete from the backingBs (if present)
// - remove from addedCids (if present)
// - if present in addedCids, also walk bufferedBlks and remove if present
return xerrors.New("deletion is unsupported")
}
func (bs *AutobatchBlockstore) DeleteMany(ctx context.Context, cids []cid.Cid) error {
// see note in DeleteBlock()
return xerrors.New("deletion is unsupported")
}
func (bs *AutobatchBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
_, err := bs.Get(ctx, c)
if err == nil {
return true, nil
}
if err == ErrNotFound {
return false, nil
}
return false, err
}
func (bs *AutobatchBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
blk, err := bs.Get(ctx, c)
if err != nil {
return 0, err
}
return len(blk.RawData()), nil
}
func (bs *AutobatchBlockstore) PutMany(ctx context.Context, blks []block.Block) error {
for _, blk := range blks {
if err := bs.Put(ctx, blk); err != nil {
return err
}
}
return nil
}
func (bs *AutobatchBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
bs.Flush(ctx)
return bs.backingBs.AllKeysChan(ctx)
}
func (bs *AutobatchBlockstore) HashOnRead(enabled bool) {
bs.backingBs.HashOnRead(enabled)
}
func (bs *AutobatchBlockstore) View(ctx context.Context, cid cid.Cid, callback func([]byte) error) error {
bs.Flush(ctx)
return bs.backingBs.View(ctx, cid, callback)
2022-01-11 16:31:59 +00:00
}