Skip to content

Commit

Permalink
feedback adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
i-norden committed Oct 18, 2023
1 parent bf9dcda commit 860cb76
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
37 changes: 19 additions & 18 deletions blockstore/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ type Blockstore interface {
HashOnRead(enabled bool)
}

// GetManyBlockstore is a blockstore interface that supports a GetMany method
type GetManyBlockstore interface {
// TxnBlockstore is a blockstore interface that supports GetMany and PutMany methods using ds.TxnDatastore
type TxnBlockstore interface {
Blockstore
PutMany(ctx context.Context, blocks []blocks.Block) error
GetMany(context.Context, []cid.Cid) ([]blocks.Block, []cid.Cid, error)
}

Expand Down Expand Up @@ -317,15 +318,15 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return output, nil
}

// GetManyOption is a getManyBlockStore option implementation
type GetManyOption struct {
f func(bs *getManyBlockStore)
// TxnBlockstoreOption is a txnBlockstore option implementation
type TxnBlockstoreOption struct {
f func(bs *txnBlockstore)
}

// NewGetManyBlockstore returns a default GetManyBlockstore implementation
// NewTxnBlockstore returns a default TxnBlockstore implementation
// using the provided datastore.TxnDatastore backend.
func NewGetManyBlockstore(d ds.TxnDatastore, opts ...GetManyOption) GetManyBlockstore {
bs := &getManyBlockStore{
func NewTxnBlockstore(d ds.TxnDatastore, opts ...TxnBlockstoreOption) TxnBlockstore {
bs := &txnBlockstore{
datastore: d,
}

Expand All @@ -339,19 +340,19 @@ func NewGetManyBlockstore(d ds.TxnDatastore, opts ...GetManyOption) GetManyBlock
return bs
}

type getManyBlockStore struct {
type txnBlockstore struct {
datastore ds.TxnDatastore

rehash atomic.Bool
writeThrough bool
noPrefix bool
}

func (bs *getManyBlockStore) HashOnRead(enabled bool) {
func (bs *txnBlockstore) HashOnRead(enabled bool) {
bs.rehash.Store(enabled)

Check warning on line 352 in blockstore/blockstore.go

View check run for this annotation

Codecov / codecov/patch

blockstore/blockstore.go#L351-L352

Added lines #L351 - L352 were not covered by tests
}

func (bs *getManyBlockStore) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
func (bs *txnBlockstore) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
if !k.Defined() {
logger.Error("undefined cid in blockstore")
return nil, ipld.ErrNotFound{Cid: k}
Expand All @@ -378,7 +379,7 @@ func (bs *getManyBlockStore) Get(ctx context.Context, k cid.Cid) (blocks.Block,
return blocks.NewBlockWithCid(bdata, k)

Check warning on line 379 in blockstore/blockstore.go

View check run for this annotation

Codecov / codecov/patch

blockstore/blockstore.go#L379

Added line #L379 was not covered by tests
}

func (bs *getManyBlockStore) GetMany(ctx context.Context, cs []cid.Cid) ([]blocks.Block, []cid.Cid, error) {
func (bs *txnBlockstore) GetMany(ctx context.Context, cs []cid.Cid) ([]blocks.Block, []cid.Cid, error) {
if len(cs) == 1 {
// performance fast-path
block, err := bs.Get(ctx, cs[0])
Expand Down Expand Up @@ -433,7 +434,7 @@ func (bs *getManyBlockStore) GetMany(ctx context.Context, cs []cid.Cid) ([]block
return blks, missingCIDs, t.Commit(ctx)
}

func (bs *getManyBlockStore) Put(ctx context.Context, block blocks.Block) error {
func (bs *txnBlockstore) 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
Expand All @@ -446,7 +447,7 @@ func (bs *getManyBlockStore) Put(ctx context.Context, block blocks.Block) error
return bs.datastore.Put(ctx, k, block.RawData())

Check warning on line 447 in blockstore/blockstore.go

View check run for this annotation

Codecov / codecov/patch

blockstore/blockstore.go#L447

Added line #L447 was not covered by tests
}

func (bs *getManyBlockStore) PutMany(ctx context.Context, blocks []blocks.Block) error {
func (bs *txnBlockstore) PutMany(ctx context.Context, blocks []blocks.Block) error {
if len(blocks) == 1 {
// performance fast-path
return bs.Put(ctx, blocks[0])
Expand Down Expand Up @@ -474,27 +475,27 @@ func (bs *getManyBlockStore) PutMany(ctx context.Context, blocks []blocks.Block)
return t.Commit(ctx)
}

func (bs *getManyBlockStore) Has(ctx context.Context, k cid.Cid) (bool, error) {
func (bs *txnBlockstore) Has(ctx context.Context, k cid.Cid) (bool, error) {
return bs.datastore.Has(ctx, dshelp.MultihashToDsKey(k.Hash()))

Check warning on line 479 in blockstore/blockstore.go

View check run for this annotation

Codecov / codecov/patch

blockstore/blockstore.go#L478-L479

Added lines #L478 - L479 were not covered by tests
}

func (bs *getManyBlockStore) GetSize(ctx context.Context, k cid.Cid) (int, error) {
func (bs *txnBlockstore) GetSize(ctx context.Context, k cid.Cid) (int, error) {
size, err := bs.datastore.GetSize(ctx, dshelp.MultihashToDsKey(k.Hash()))
if err == ds.ErrNotFound {
return -1, ipld.ErrNotFound{Cid: k}
}
return size, err

Check warning on line 487 in blockstore/blockstore.go

View check run for this annotation

Codecov / codecov/patch

blockstore/blockstore.go#L482-L487

Added lines #L482 - L487 were not covered by tests
}

func (bs *getManyBlockStore) DeleteBlock(ctx context.Context, k cid.Cid) error {
func (bs *txnBlockstore) DeleteBlock(ctx context.Context, k cid.Cid) error {
return bs.datastore.Delete(ctx, dshelp.MultihashToDsKey(k.Hash()))

Check warning on line 491 in blockstore/blockstore.go

View check run for this annotation

Codecov / codecov/patch

blockstore/blockstore.go#L490-L491

Added lines #L490 - L491 were not covered by tests
}

// AllKeysChan runs a query for keys from the blockstore.
// this is very simplistic, in the future, take dsq.Query as a param?
//
// AllKeysChan respects context.
func (bs *getManyBlockStore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
func (bs *txnBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {

// KeysOnly, because that would be _a lot_ of data.
q := dsq.Query{KeysOnly: true}
Expand Down
8 changes: 4 additions & 4 deletions blockstore/blockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestCidv0v1(t *testing.T) {
}

func TestGetManyWhenKeyNotPresent(t *testing.T) {
bs := NewGetManyBlockstore(dstest.NewTestTxnDatastore(ds.NewMapDatastore(), false))
bs := NewTxnBlockstore(dstest.NewTestTxnDatastore(ds.NewMapDatastore(), false))
c1 := cid.NewCidV0(u.Hash([]byte("stuff")))
c2 := cid.NewCidV0(u.Hash([]byte("stuff2")))

Expand All @@ -93,15 +93,15 @@ func TestGetManyWhenKeyNotPresent(t *testing.T) {
}

func TestGetManyWhenKeyIsNil(t *testing.T) {
bs := NewGetManyBlockstore(dstest.NewTestTxnDatastore(ds.NewMapDatastore(), false))
bs := NewTxnBlockstore(dstest.NewTestTxnDatastore(ds.NewMapDatastore(), false))
_, _, err := bs.GetMany(bg, []cid.Cid{{}, {}})
if !ipld.IsNotFound(err) {
t.Fail()
}
}

func TestPutsThenGetManyBlock(t *testing.T) {
bs := NewGetManyBlockstore(dstest.NewTestTxnDatastore(ds.NewMapDatastore(), false))
bs := NewTxnBlockstore(dstest.NewTestTxnDatastore(ds.NewMapDatastore(), false))
block1 := blocks.NewBlock([]byte("some data1"))
block2 := blocks.NewBlock([]byte("some data2"))
block3 := blocks.NewBlock([]byte("some data3"))
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestPutsThenGetManyBlock(t *testing.T) {
}

func TestCidv0v1Many(t *testing.T) {
bs := NewGetManyBlockstore(dstest.NewTestTxnDatastore(ds.NewMapDatastore(), false))
bs := NewTxnBlockstore(dstest.NewTestTxnDatastore(ds.NewMapDatastore(), false))
block1 := blocks.NewBlock([]byte("some data1"))
block2 := blocks.NewBlock([]byte("some data2"))
block3 := blocks.NewBlock([]byte("some data3"))
Expand Down

0 comments on commit 860cb76

Please sign in to comment.