From 04eba28a16dbbf9de4ac77967bb17eda9664fcc1 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Thu, 30 Oct 2014 01:54:50 -0700 Subject: [PATCH] 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 --- blockservice/blockservice.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/blockservice/blockservice.go b/blockservice/blockservice.go index 54a992cdb..c3c3868f2 100644 --- a/blockservice/blockservice.go +++ b/blockservice/blockservice.go @@ -40,10 +40,21 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) { log.Debug("blockservice: storing [%s] in datastore", k) // TODO(brian): define a block datastore with a Put method which accepts a // block parameter - err := s.Datastore.Put(k.DsKey(), b.Data) + + // check if we have it before adding. this is an extra read, but large writes + // are more expensive. + // TODO(jbenet) cheaper has. https://github.com/jbenet/go-datastore/issues/6 + has, err := s.Datastore.Has(k.DsKey()) if err != nil { return k, err } + if !has { + err := s.Datastore.Put(k.DsKey(), b.Data) + if err != nil { + return k, err + } + } + if s.Remote != nil { ctx := context.TODO() err = s.Remote.HasBlock(ctx, *b)