blockservice: dont write blocks twice
If the datastore has a value for the key, we already have the block. We should not write it again. This will make redundant writes much faster. At the moment, a datastore.Has on leveldb is a GetBackedHas. Track https://github.com/jbenet/go-datastore/issues/6
Juan Batiz-Benet committed
Oct 30, 2014 at 01:54 UTC
04eba28a16dbbf9de4ac77967bb17eda9664fcc1
1 file changed
+12
-1
blockservice/blockservice.go
+12
-1
@@ -40,10 +40,21 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
40
log.Debug("blockservice: storing [%s] in datastore", k)
41
// TODO(brian): define a block datastore with a Put method which accepts a
42
// block parameter
43
- err := s.Datastore.Put(k.DsKey(), b.Data)
43
+
44
+ // check if we have it before adding. this is an extra read, but large writes
45
+ // are more expensive.
46
+ // TODO(jbenet) cheaper has. https://github.com/jbenet/go-datastore/issues/6
47
+ has, err := s.Datastore.Has(k.DsKey())
48
if err != nil {
49
return k, err
50
}
51
+ if !has {
52
+ err := s.Datastore.Put(k.DsKey(), b.Data)
53
+ if err != nil {
54
+ return k, err
55
+ }
56
+ }
57
+
58
if s.Remote != nil {
59
ctx := context.TODO()
60
err = s.Remote.HasBlock(ctx, *b)