generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: blockstore: GetMany blockstore method #492
Open
i-norden
wants to merge
12
commits into
ipfs:main
Choose a base branch
from
vulcanize:dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
34ddb5e
GetMany blockstore implementation
i-norden 4ac2f26
use dsns.WrapTxnDatastore
i-norden d606bde
update to use v0.6.1-internal go-datastore
i-norden 021f60a
update changelog to make CI happy
i-norden e87fea2
GetMany blockstore test
i-norden a9aed04
update to use v0.6.1-internal-0.0.1
i-norden 8c22079
add check on returned missing cids
i-norden cfa2d14
update opentelemetry-go dependencies to work with version update on u…
i-norden 6a6fa0d
gofmt
i-norden bf9dcda
update CHANGELOG
i-norden 860cb76
feedback adjustments
i-norden b3ed048
use batchingTxnDatastoreStub to avoid reimplementing methods
i-norden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync" | ||
"sync/atomic" | ||
|
||
|
@@ -64,6 +65,13 @@ | |
HashOnRead(enabled bool) | ||
} | ||
|
||
// GetManyBlockstore is a blockstore interface that supports GetMany and PutMany methods using ds.TxnDatastore | ||
type GetManyBlockstore interface { | ||
Blockstore | ||
PutMany(ctx context.Context, blocks []blocks.Block) error | ||
GetMany(context.Context, []cid.Cid) ([]blocks.Block, []cid.Cid, error) | ||
} | ||
|
||
// Viewer can be implemented by blockstores that offer zero-copy access to | ||
// values. | ||
// | ||
|
@@ -310,6 +318,123 @@ | |
return output, nil | ||
} | ||
|
||
// NewGetManyBlockstore returns a default GetManyBlockstore implementation | ||
// using the provided datastore.TxnDatastore backend. | ||
func NewGetManyBlockstore(d ds.TxnDatastore, opts ...Option) GetManyBlockstore { | ||
bs := &blockstore{ | ||
datastore: batchingTxnDatastoreStub{TxnDatastore: d}, | ||
} | ||
|
||
for _, o := range opts { | ||
o.f(bs) | ||
} | ||
|
||
if !bs.noPrefix { | ||
bs.datastore = dsns.Wrap(bs.datastore, BlockPrefix) | ||
d = dsns.WrapTxnDatastore(d, BlockPrefix) | ||
} | ||
|
||
gmbs := &getManyBlockstore{ | ||
blockstore: bs, | ||
datastore: d, | ||
} | ||
|
||
return gmbs | ||
} | ||
|
||
type getManyBlockstore struct { | ||
*blockstore | ||
datastore ds.TxnDatastore | ||
} | ||
|
||
type batchingTxnDatastoreStub struct { | ||
ds.BatchingFeature | ||
ds.TxnDatastore | ||
} | ||
|
||
func (bs *getManyBlockstore) 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]) | ||
return []blocks.Block{block}, nil, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't make sense to not return the CID here given it's in the signature, but also it doesn't seem like |
||
} | ||
|
||
t, err := bs.datastore.NewTransaction(ctx, false) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
blks := make([]blocks.Block, 0, len(cs)) | ||
missingCIDs := make([]cid.Cid, 0, len(cs)) | ||
for _, c := range cs { | ||
if !c.Defined() { | ||
logger.Error("undefined cid in blockstore") | ||
return nil, nil, ipld.ErrNotFound{Cid: c} | ||
} | ||
bdata, err := t.Get(ctx, dshelp.MultihashToDsKey(c.Hash())) | ||
if err != nil { | ||
if err == ds.ErrNotFound { | ||
missingCIDs = append(missingCIDs, c) | ||
} else { | ||
return nil, nil, err | ||
} | ||
} else { | ||
if bs.blockstore.rehash.Load() { | ||
rbcid, err := c.Prefix().Sum(bdata) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if !rbcid.Equals(c) { | ||
return nil, nil, fmt.Errorf("block in storage has different hash (%x) than requested (%x)", rbcid.Hash(), c.Hash()) | ||
} | ||
|
||
blk, err := blocks.NewBlockWithCid(bdata, rbcid) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
blks = append(blks, blk) | ||
} else { | ||
blk, err := blocks.NewBlockWithCid(bdata, c) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
blks = append(blks, blk) | ||
} | ||
} | ||
} | ||
return blks, missingCIDs, t.Commit(ctx) | ||
} | ||
|
||
func (bs *getManyBlockstore) PutMany(ctx context.Context, blocks []blocks.Block) error { | ||
if len(blocks) == 1 { | ||
// performance fast-path | ||
return bs.Put(ctx, blocks[0]) | ||
} | ||
|
||
t, err := bs.datastore.NewTransaction(ctx, false) | ||
if err != nil { | ||
return err | ||
} | ||
for _, b := range blocks { | ||
k := dshelp.MultihashToDsKey(b.Cid().Hash()) | ||
|
||
if !bs.blockstore.writeThrough { | ||
exists, err := bs.datastore.Has(ctx, k) | ||
if err == nil && exists { | ||
continue | ||
} | ||
} | ||
|
||
err = t.Put(ctx, k, b.RawData()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return t.Commit(ctx) | ||
} | ||
|
||
// NewGCLocker returns a default implementation of | ||
// GCLocker using standard [RW] mutexes. | ||
func NewGCLocker() GCLocker { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two recommendations:
[]blocks.Block
and[]cid.Cid
sinceBlock
contains a.Cid()
method https://github.com/ipfs/go-block-format/blob/v0.2.0/blocks.go#L19-L25(<-chan blocks.Block, error)
or(<-chan BlockOption
) depending on how you want errors returnedIf returning an asynchronous object (e.g. channel or iterator) might be worth taking a look at ipfs/kubo#4592 to make sure you don't run into some common pitfalls. With Go generics now iterators may also make this easier than it used to be.