cache added cids

This commit is contained in:
Aayush Rajasekaran 2022-01-11 17:17:34 -05:00 committed by Jennifer Wang
parent da39b16c83
commit a6489f2dd7
2 changed files with 324 additions and 8 deletions

View File

@ -14,6 +14,7 @@ var autolog = log.Named("auto")
type AutobatchBlockstore struct { type AutobatchBlockstore struct {
bufferedBlks []block.Block bufferedBlks []block.Block
addedCids map[cid.Cid]struct{}
bufferedBlksLk sync.Mutex bufferedBlksLk sync.Mutex
flushLk sync.Mutex flushLk sync.Mutex
backingBs Blockstore backingBs Blockstore
@ -57,6 +58,7 @@ func NewAutobatch(ctx context.Context, backingBs Blockstore, bufferCapacity int)
bs := &AutobatchBlockstore{ bs := &AutobatchBlockstore{
backingBs: backingBs, backingBs: backingBs,
bufferCapacity: bufferCapacity, bufferCapacity: bufferCapacity,
addedCids: make(map[cid.Cid]struct{}),
} }
return bs return bs
@ -69,13 +71,16 @@ func (bs *AutobatchBlockstore) Get(ctx context.Context, c cid.Cid) (block.Block,
} }
func (bs *AutobatchBlockstore) Put(ctx context.Context, blk block.Block) error { func (bs *AutobatchBlockstore) Put(ctx context.Context, blk block.Block) error {
// TODO: Would it be faster to check if bs.backing Has the blk (and skip if so)?
bs.bufferedBlksLk.Lock() bs.bufferedBlksLk.Lock()
bs.bufferedBlks = append(bs.bufferedBlks, blk) _, ok := bs.addedCids[blk.Cid()]
bs.bufferSize += len(blk.RawData()) if !ok {
if bs.bufferSize >= bs.bufferCapacity { bs.bufferedBlks = append(bs.bufferedBlks, blk)
// time to flush bs.addedCids[blk.Cid()] = struct{}{}
go bs.Flush(ctx) bs.bufferSize += len(blk.RawData())
if bs.bufferSize >= bs.bufferCapacity {
// time to flush
go bs.Flush(ctx)
}
} }
bs.bufferedBlksLk.Unlock() bs.bufferedBlksLk.Unlock()
return nil return nil

File diff suppressed because it is too large Load Diff