Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
Allow using a NewWriteThrough() blockstore.
Browse files Browse the repository at this point in the history
Similar to blockservice.NewWriteThrough().

Currently the default blockstore trades read amplification for every
write. While this is fine in cases where writes are very expensive and the datastore offers a quick Has() method, it is not always the case.

Some datastore backends may be better off just getting all the writes no
matter what. At least I would like to try it out.
  • Loading branch information
hsanjuan authored and Jorropo committed Mar 8, 2023
1 parent 606ff4b commit 498084a
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,22 @@ func NewBlockstore(d ds.Batching) Blockstore {
dd := dsns.Wrap(d, BlockPrefix)
dsb = dd
return &blockstore{
datastore: dsb,
rehash: uatomic.NewBool(false),
datastore: dsb,
rehash: uatomic.NewBool(false),
checkFirst: true,
}
}

// NewWriteThrough returns a default Blockstore implementation
// which does not tries to check if blocks exist prior to writing.
func NewWriteThrough(d ds.Batching) Blockstore {
var dsb ds.Batching
dd := dsns.Wrap(d, BlockPrefix)
dsb = dd
return &blockstore{
datastore: dsb,
rehash: uatomic.NewBool(false),
checkFirst: false,
}
}

Expand All @@ -132,7 +146,8 @@ func NewBlockstoreNoPrefix(d ds.Batching) Blockstore {
type blockstore struct {
datastore ds.Batching

rehash *uatomic.Bool
rehash *uatomic.Bool
checkFirst bool
}

func (bs *blockstore) HashOnRead(enabled bool) {
Expand Down Expand Up @@ -170,9 +185,11 @@ func (bs *blockstore) Put(ctx context.Context, block blocks.Block) error {
k := dshelp.MultihashToDsKey(block.Cid().Hash())

// Has is cheaper than Put, so see if we already have it
exists, err := bs.datastore.Has(ctx, k)
if err == nil && exists {
return nil // already stored.
if bs.checkFirst {
exists, err := bs.datastore.Has(ctx, k)
if err == nil && exists {
return nil // already stored.
}
}
return bs.datastore.Put(ctx, k, block.RawData())
}
Expand All @@ -189,9 +206,12 @@ func (bs *blockstore) PutMany(ctx context.Context, blocks []blocks.Block) error
}
for _, b := range blocks {
k := dshelp.MultihashToDsKey(b.Cid().Hash())
exists, err := bs.datastore.Has(ctx, k)
if err == nil && exists {
continue

if bs.checkFirst {
exists, err := bs.datastore.Has(ctx, k)
if err == nil && exists {
continue
}
}

err = t.Put(ctx, k, b.RawData())
Expand Down

0 comments on commit 498084a

Please sign in to comment.