Skip to content

Commit 6084860

Browse files
authored
Filestore: add Provider option to provide filestore blocks. (#1042)
* Filestore: add Provider option to provide filestore blocks. A Filestore is a Blockstore wrapper. When I moved providing resposibilities to Blockstore/Pinner/MFS... I forgot the Filestore. In Particular, the Filestore writes all blocks to the Blockstore except those of FilestoreNode type, which are serialized and written to the datastore directly via the Filemanager. The data serialized is not the block itself, but a pointer to a filesystem object and an offset. So FilestoreNodes escaped providing even when the blockstore is configured to provide... Not anymore.
1 parent 031df82 commit 6084860

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The following emojis are used to highlight certain changes:
4040
- `bitswap`: Updated to use `verifcid.DefaultMaxDigestSize` for `MaximumHashLength` constant
4141
- The default `MaximumAllowedCid` limit for incoming CIDs can be adjusted using `bitswap.MaxCidSize` or `server.MaxCidSize` options
4242
- 🛠 `bitswap/client`: The `RebroadcastDelay` option now takes a `time.Duration` value. This is a potentially BREAKING CHANGE. The time-varying functionality of `delay.Delay` was never used, so it was replaced with a fixed duration value. This also removes the `github.com/ipfs/go-ipfs-delay` dependency.
43+
- `filestore`: Support providing filestore-blocks. A new `provider.MultihashProvider` parameter has been added to `filestore.New()`. When used, the blocks handled by the Filestore's `FileManager` will be provided on write (Put and PutMany).
4344

4445
### Removed
4546

blockstore/blockstore.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,12 @@ func (bs *blockstore) PutMany(ctx context.Context, blocks []blocks.Block) error
250250
return err
251251
}
252252

253-
var hashes []multihash.Multihash
254-
for _, block := range blocks {
255-
hashes = append(hashes, block.Cid().Hash())
256-
}
257253
if bs.provider != nil {
254+
var hashes []multihash.Multihash
255+
for _, block := range blocks {
256+
hashes = append(hashes, block.Cid().Hash())
257+
}
258+
logger.Debugf("blockstore: provide %d hashes", len(hashes))
258259
if err := bs.provider.StartProviding(false, hashes...); err != nil {
259260
logger.Warnf("blockstore: error while providing blocks: %s", err)
260261
}

filestore/filestore.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import (
1313

1414
blockstore "github.com/ipfs/boxo/blockstore"
1515
posinfo "github.com/ipfs/boxo/filestore/posinfo"
16+
"github.com/ipfs/boxo/provider"
1617
blocks "github.com/ipfs/go-block-format"
1718
cid "github.com/ipfs/go-cid"
1819
dsq "github.com/ipfs/go-datastore/query"
1920
ipld "github.com/ipfs/go-ipld-format"
2021
logging "github.com/ipfs/go-log/v2"
22+
"github.com/multiformats/go-multihash"
2123
)
2224

2325
var logger = logging.Logger("filestore")
@@ -31,8 +33,9 @@ var (
3133
// to store regular blocks and a special Blockstore called
3234
// FileManager to store blocks which data exists in an external file.
3335
type Filestore struct {
34-
fm *FileManager
35-
bs blockstore.Blockstore
36+
fm *FileManager
37+
bs blockstore.Blockstore
38+
provider provider.MultihashProvider
3639
}
3740

3841
// FileManager returns the FileManager in Filestore.
@@ -45,9 +48,12 @@ func (f *Filestore) MainBlockstore() blockstore.Blockstore {
4548
return f.bs
4649
}
4750

48-
// NewFilestore creates one using the given Blockstore and FileManager.
49-
func NewFilestore(bs blockstore.Blockstore, fm *FileManager) *Filestore {
50-
return &Filestore{fm, bs}
51+
// NewFilestore creates one using the given Blockstore and FileManager. An
52+
// optional MultihashProvider can be used to provide blocks written to the
53+
// FileManager (blocks written to the normal blockstore will be provided by it
54+
// if it has been initialized with the blockstore.Provider option).
55+
func NewFilestore(bs blockstore.Blockstore, fm *FileManager, prov provider.MultihashProvider) *Filestore {
56+
return &Filestore{fm, bs, prov}
5157
}
5258

5359
// AllKeysChan returns a channel from which to read the keys stored in
@@ -193,7 +199,17 @@ func (f *Filestore) Put(ctx context.Context, b blocks.Block) error {
193199

194200
switch b := b.(type) {
195201
case *posinfo.FilestoreNode:
196-
return f.fm.Put(ctx, b)
202+
err = f.fm.Put(ctx, b)
203+
if err != nil {
204+
return err
205+
}
206+
if f.provider != nil {
207+
logger.Debugf("filestore: provide %s", b.Cid())
208+
if err := f.provider.StartProviding(false, b.Cid().Hash()); err != nil {
209+
logger.Warnf("filestore: error while providing %s: %s", b.Cid(), err)
210+
}
211+
}
212+
return nil
197213
default:
198214
return f.bs.Put(ctx, b)
199215
}
@@ -235,6 +251,17 @@ func (f *Filestore) PutMany(ctx context.Context, bs []blocks.Block) error {
235251
if err != nil {
236252
return err
237253
}
254+
255+
if f.provider != nil {
256+
var hashes []multihash.Multihash
257+
for _, n := range fstores {
258+
hashes = append(hashes, n.Node.Cid().Hash())
259+
}
260+
logger.Debugf("filestore: provide %d hashes", len(hashes))
261+
if err := f.provider.StartProviding(false, hashes...); err != nil {
262+
logger.Warnf("filestore: error while providing hashes: %s", err)
263+
}
264+
}
238265
}
239266
return nil
240267
}

filestore/filestore_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func newTestFilestore(t *testing.T, option ...Option) (string, *Filestore) {
2525
fm.AllowFiles = true
2626

2727
bs := blockstore.NewBlockstore(mds)
28-
fstore := NewFilestore(bs, fm)
28+
fstore := NewFilestore(bs, fm, nil)
2929
return testdir, fstore
3030
}
3131

0 commit comments

Comments
 (0)