Skip to content

Commit 2ec3699

Browse files
committed
Support using a custom Blockstore
Usually (in Kubo), the Blockstore is just built on top of the datastore. There are, however, blockstore implementations (i.e StoreTheHash) that are optimized blockstores and not general purpose datastores. This commit allows providing a custom blockstore, but initializes everything as before when set to nil. The signature of New() changes accordingly. A new option UncachedBlockstore, allows disabling the CachedBlockstore-wrapping that also happens by default, since some datastores/blockstores include their own caching and bloom-filters, so this can potentially be making things slower.
1 parent f27f1d2 commit 2ec3699

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

examples/litepeer/litepeer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func main() {
3939
panic(err)
4040
}
4141

42-
lite, err := ipfslite.New(ctx, ds, h, dht, nil)
42+
lite, err := ipfslite.New(ctx, ds, nil, h, dht, nil)
4343
if err != nil {
4444
panic(err)
4545
}

examples/walker/walker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func main() {
4444
panic(err)
4545
}
4646

47-
lite, err := ipfslite.New(ctx, ds, h, dht, nil)
47+
lite, err := ipfslite.New(ctx, ds, nil, h, dht, nil)
4848
if err != nil {
4949
panic(err)
5050
}

ipfs.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ type Config struct {
5555
Offline bool
5656
// ReprovideInterval sets how often to reprovide records to the DHT
5757
ReprovideInterval time.Duration
58+
// Disables wrapping the blockstore in an ARC cache + Bloomfilter. Use
59+
// when the given blockstore or datastore already has caching, or when
60+
// caching is not needed.
61+
UncachedBlockstore bool
5862
}
5963

6064
func (cfg *Config) setDefaults() {
@@ -81,13 +85,15 @@ type Peer struct {
8185
reprovider provider.System
8286
}
8387

84-
// New creates an IPFS-Lite Peer. It uses the given datastore, libp2p Host and
85-
// Routing (usuall the DHT). The Host and the Routing may be nil if
86-
// config.Offline is set to true, as they are not used in that case. Peer
87-
// implements the ipld.DAGService interface.
88+
// New creates an IPFS-Lite Peer. It uses the given datastore, blockstore,
89+
// libp2p Host and Routing (usuall the DHT). If the blockstore is nil, the
90+
// given datastore will be wrapped to create one. The Host and the Routing may
91+
// be nil if config.Offline is set to true, as they are not used in that
92+
// case. Peer implements the ipld.DAGService interface.
8893
func New(
8994
ctx context.Context,
90-
store datastore.Batching,
95+
datastore datastore.Batching,
96+
blockstore blockstore.Blockstore,
9197
host host.Host,
9298
dht routing.Routing,
9399
cfg *Config,
@@ -104,10 +110,10 @@ func New(
104110
cfg: cfg,
105111
host: host,
106112
dht: dht,
107-
store: store,
113+
store: datastore,
108114
}
109115

110-
err := p.setupBlockstore()
116+
err := p.setupBlockstore(blockstore)
111117
if err != nil {
112118
return nil, err
113119
}
@@ -131,14 +137,22 @@ func New(
131137
return p, nil
132138
}
133139

134-
func (p *Peer) setupBlockstore() error {
135-
bs := blockstore.NewBlockstore(p.store)
140+
func (p *Peer) setupBlockstore(bs blockstore.Blockstore) error {
141+
var err error
142+
if bs == nil {
143+
bs = blockstore.NewBlockstore(p.store)
144+
}
145+
146+
// Support Identity multihashes.
136147
bs = blockstore.NewIdStore(bs)
137-
cachedbs, err := blockstore.CachedBlockstore(p.ctx, bs, blockstore.DefaultCacheOpts())
138-
if err != nil {
139-
return err
148+
149+
if !p.cfg.UncachedBlockstore {
150+
bs, err = blockstore.CachedBlockstore(p.ctx, bs, blockstore.DefaultCacheOpts())
151+
if err != nil {
152+
return err
153+
}
140154
}
141-
p.bstore = cachedbs
155+
p.bstore = bs
142156
return nil
143157
}
144158

ipfs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ func setupPeers(t *testing.T) (p1, p2 *Peer, closer func(t *testing.T)) {
7979
}
8080
}
8181
}
82-
p1, err = New(ctx, ds1, h1, dht1, nil)
82+
p1, err = New(ctx, ds1, nil, h1, dht1, nil)
8383
if err != nil {
8484
closer(t)
8585
t.Fatal(err)
8686
}
87-
p2, err = New(ctx, ds2, h2, dht2, nil)
87+
p2, err = New(ctx, ds2, nil, h2, dht2, nil)
8888
if err != nil {
8989
closer(t)
9090
t.Fatal(err)

0 commit comments

Comments
 (0)