cache added cids
This commit is contained in:
parent
da39b16c83
commit
a6489f2dd7
@ -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,14 +71,17 @@ 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()
|
||||||
|
_, ok := bs.addedCids[blk.Cid()]
|
||||||
|
if !ok {
|
||||||
bs.bufferedBlks = append(bs.bufferedBlks, blk)
|
bs.bufferedBlks = append(bs.bufferedBlks, blk)
|
||||||
|
bs.addedCids[blk.Cid()] = struct{}{}
|
||||||
bs.bufferSize += len(blk.RawData())
|
bs.bufferSize += len(blk.RawData())
|
||||||
if bs.bufferSize >= bs.bufferCapacity {
|
if bs.bufferSize >= bs.bufferCapacity {
|
||||||
// time to flush
|
// time to flush
|
||||||
go bs.Flush(ctx)
|
go bs.Flush(ctx)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
bs.bufferedBlksLk.Unlock()
|
bs.bufferedBlksLk.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user