Skip to content

Commit

Permalink
Merge pull request #749 from ipfs/feat/writethrough-bool
Browse files Browse the repository at this point in the history
blockstore/blockservice: change option to `WriteThrough(enabled bool)`
  • Loading branch information
hsanjuan authored Dec 12, 2024
2 parents 7c459af + f9bac13 commit 0c321cc
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The following emojis are used to highlight certain changes:

### Changed

* 🛠 `blockstore` and `blockservice`'s `WriteThrough()` option now takes an "enabled" parameter: `WriteThrough(enabled bool)`.

### Removed

### Fixed
Expand Down
6 changes: 3 additions & 3 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ type blockService struct {
type Option func(*blockService)

// WriteThrough disable cache checks for writes and make them go straight to
// the blockstore.
func WriteThrough() Option {
// the blockstore, when enabled.
func WriteThrough(enabled bool) Option {
return func(bs *blockService) {
bs.checkFirst = false
bs.checkFirst = !enabled
}
}

Expand Down
8 changes: 4 additions & 4 deletions blockservice/blockservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestWriteThroughWorks(t *testing.T) {
}
exchbstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
exch := offline.Exchange(exchbstore)
bserv := New(bstore, exch, WriteThrough())
bserv := New(bstore, exch, WriteThrough(true))

block := random.BlocksOfSize(1, blockSize)[0]

Expand Down Expand Up @@ -63,7 +63,7 @@ func TestExchangeWrite(t *testing.T) {
offline.Exchange(exchbstore),
0,
}
bserv := New(bstore, exch, WriteThrough())
bserv := New(bstore, exch, WriteThrough(true))

for name, fetcher := range map[string]BlockGetter{
"blockservice": bserv,
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestLazySessionInitialization(t *testing.T) {
session := offline.Exchange(bstore2)
exch := offline.Exchange(bstore3)
sessionExch := &fakeSessionExchange{Interface: exch, session: session}
bservSessEx := New(bstore, sessionExch, WriteThrough())
bservSessEx := New(bstore, sessionExch, WriteThrough(true))
blks := random.BlocksOfSize(2, blockSize)

block := blks[0]
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestNilExchange(t *testing.T) {
block := random.BlocksOfSize(1, blockSize)[0]

bs := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
bserv := New(bs, nil, WriteThrough())
bserv := New(bs, nil, WriteThrough(true))
sess := NewSession(ctx, bserv)
_, err := sess.GetBlock(ctx, block.Cid())
if !ipld.IsNotFound(err) {
Expand Down
6 changes: 3 additions & 3 deletions blockstore/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ type Option struct {
}

// WriteThrough skips checking if the blockstore already has a block before
// writing it.
func WriteThrough() Option {
// writing it, when enabled.
func WriteThrough(enabled bool) Option {
return Option{
func(bs *blockstore) {
bs.writeThrough = true
bs.writeThrough = enabled
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion exchange/providing/providing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestExchange(t *testing.T) {
provExchange := New(i.Exchange, prov)
// write-through so that we notify when re-adding block
bs := blockservice.New(i.Blockstore, provExchange,
blockservice.WriteThrough())
blockservice.WriteThrough(true))
block := random.BlocksOfSize(1, 10)[0]
// put it on the blockstore of the first instance
err = i.Blockstore.Put(ctx, block)
Expand Down

0 comments on commit 0c321cc

Please sign in to comment.