From f9bac131a4b6e0cf3a6819c79cf21cee7ebf221b Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Wed, 11 Dec 2024 10:24:10 +0100 Subject: [PATCH] blockstore/blockservice: change option to `WriteThrough(enabled bool)` This allows passing in a boolean to indicate if we want to use the option or not, which provides better ergonomics for initialization. --- CHANGELOG.md | 2 ++ blockservice/blockservice.go | 6 +++--- blockservice/blockservice_test.go | 8 ++++---- blockstore/blockstore.go | 6 +++--- exchange/providing/providing_test.go | 2 +- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ceabbf05..41ec2392d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/blockservice/blockservice.go b/blockservice/blockservice.go index 353be00f8..810f26d3f 100644 --- a/blockservice/blockservice.go +++ b/blockservice/blockservice.go @@ -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 } } diff --git a/blockservice/blockservice_test.go b/blockservice/blockservice_test.go index 29350ff37..39c4b6e4b 100644 --- a/blockservice/blockservice_test.go +++ b/blockservice/blockservice_test.go @@ -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] @@ -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, @@ -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] @@ -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) { diff --git a/blockstore/blockstore.go b/blockstore/blockstore.go index 27260e5d8..368670d3d 100644 --- a/blockstore/blockstore.go +++ b/blockstore/blockstore.go @@ -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 }, } } diff --git a/exchange/providing/providing_test.go b/exchange/providing/providing_test.go index 42a2a0cb9..4ee1dcf6b 100644 --- a/exchange/providing/providing_test.go +++ b/exchange/providing/providing_test.go @@ -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)