mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-10 18:57:57 +08:00
blockservice: don't store blocks we already have
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
This commit is contained in:
parent
8830aae9bc
commit
582e5dee3a
@ -43,7 +43,15 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
|
||||
// TODO pass a context into this if the remote.HasBlock is going to remain here.
|
||||
func (s *BlockService) AddBlock(b blocks.Block) (key.Key, error) {
|
||||
k := b.Key()
|
||||
err := s.Blockstore.Put(b)
|
||||
has, err := s.Blockstore.Has(k)
|
||||
if err != nil {
|
||||
return k, err
|
||||
}
|
||||
if has {
|
||||
return k, nil
|
||||
}
|
||||
|
||||
err = s.Blockstore.Put(b)
|
||||
if err != nil {
|
||||
return k, err
|
||||
}
|
||||
@ -54,13 +62,27 @@ func (s *BlockService) AddBlock(b blocks.Block) (key.Key, error) {
|
||||
}
|
||||
|
||||
func (s *BlockService) AddBlocks(bs []blocks.Block) ([]key.Key, error) {
|
||||
err := s.Blockstore.PutMany(bs)
|
||||
var toput []blocks.Block
|
||||
for _, b := range bs {
|
||||
has, err := s.Blockstore.Has(b.Key())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if has {
|
||||
continue
|
||||
}
|
||||
|
||||
toput = append(toput, b)
|
||||
}
|
||||
|
||||
err := s.Blockstore.PutMany(toput)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ks []key.Key
|
||||
for _, b := range bs {
|
||||
for _, b := range toput {
|
||||
if err := s.Exchange.HasBlock(b); err != nil {
|
||||
return nil, errors.New("blockservice is closed")
|
||||
}
|
||||
|
||||
@ -133,6 +133,7 @@ func (bs *Bitswap) provideCollector(ctx context.Context) {
|
||||
log.Debug("newBlocks channel closed")
|
||||
return
|
||||
}
|
||||
|
||||
if keysOut == nil {
|
||||
nextKey = blk.Key()
|
||||
keysOut = bs.provideKeys
|
||||
|
||||
Loading…
Reference in New Issue
Block a user