Skip to content

Commit

Permalink
blockservice: don't store blocks we already have
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <[email protected]>
  • Loading branch information
whyrusleeping committed Aug 24, 2016
1 parent 8830aae commit 582e5de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
28 changes: 25 additions & 3 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions exchange/bitswap/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 582e5de

Please sign in to comment.