1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-10 09:52:20 +08:00

blockstore: Put checks Has first.

This commit is contained in:
Juan Batiz-Benet
2014-12-06 13:39:03 -08:00
parent 3db0da504f
commit 1192be196b

View File

@ -45,7 +45,13 @@ func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
}
func (bs *blockstore) Put(block *blocks.Block) error {
return bs.datastore.Put(block.Key().DsKey(), block.Data)
// Has is cheaper than
k := block.Key().DsKey()
exists, err := bs.datastore.Has(k)
if err != nil && exists {
return nil // already stored.
}
return bs.datastore.Put(k, block.Data)
}
func (bs *blockstore) Has(k u.Key) (bool, error) {