blockstore: Put checks Has first.
Juan Batiz-Benet committed
Dec 6, 2014 at 13:39 UTC
1192be196b3d0acca2e2dce5ffd5d12a924fdc5a
1 file changed
+7
-1
blocks/blockstore/blockstore.go
+7
-1
@@ -45,7 +45,13 @@ func (bs *blockstore) Get(k u.Key) (*blocks.Block, error) {
45
}
46
47
func (bs *blockstore) Put(block *blocks.Block) error {
48
- return bs.datastore.Put(block.Key().DsKey(), block.Data)
48
+ // Has is cheaper than
49
+ k := block.Key().DsKey()
50
+ exists, err := bs.datastore.Has(k)
51
+ if err != nil && exists {
52
+ return nil // already stored.
53
+ }
54
+ return bs.datastore.Put(k, block.Data)
55
}
56
57
func (bs *blockstore) Has(k u.Key) (bool, error) {