Skip to content
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

update context datastore #273

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions car.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func init() {
}

type Store interface {
Put(blocks.Block) error
Put(context.Context, blocks.Block) error
}

type ReadStore interface {
Get(cid.Cid) (blocks.Block, error)
Get(context.Context, cid.Cid) (blocks.Block, error)
}

type CarHeader struct {
Expand Down Expand Up @@ -163,30 +163,30 @@ func (cr *CarReader) Next() (blocks.Block, error) {
}

type batchStore interface {
PutMany([]blocks.Block) error
PutMany(context.Context, []blocks.Block) error
}

func LoadCar(s Store, r io.Reader) (*CarHeader, error) {
func LoadCar(ctx context.Context, s Store, r io.Reader) (*CarHeader, error) {
cr, err := NewCarReader(r)
if err != nil {
return nil, err
}

if bs, ok := s.(batchStore); ok {
return loadCarFast(bs, cr)
return loadCarFast(ctx, bs, cr)
}

return loadCarSlow(s, cr)
return loadCarSlow(ctx, s, cr)
}

func loadCarFast(s batchStore, cr *CarReader) (*CarHeader, error) {
func loadCarFast(ctx context.Context, s batchStore, cr *CarReader) (*CarHeader, error) {
var buf []blocks.Block
for {
blk, err := cr.Next()
if err != nil {
if err == io.EOF {
if len(buf) > 0 {
if err := s.PutMany(buf); err != nil {
if err := s.PutMany(ctx, buf); err != nil {
return nil, err
}
}
Expand All @@ -198,15 +198,15 @@ func loadCarFast(s batchStore, cr *CarReader) (*CarHeader, error) {
buf = append(buf, blk)

if len(buf) > 1000 {
if err := s.PutMany(buf); err != nil {
if err := s.PutMany(ctx, buf); err != nil {
return nil, err
}
buf = buf[:0]
}
}
}

func loadCarSlow(s Store, cr *CarReader) (*CarHeader, error) {
func loadCarSlow(ctx context.Context, s Store, cr *CarReader) (*CarHeader, error) {
for {
blk, err := cr.Next()
if err != nil {
Expand All @@ -216,7 +216,7 @@ func loadCarSlow(s Store, cr *CarReader) (*CarHeader, error) {
return nil, err
}

if err := s.Put(blk); err != nil {
if err := s.Put(ctx, blk); err != nil {
return nil, err
}
}
Expand Down
5 changes: 3 additions & 2 deletions car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func assertAddNodes(t *testing.T, ds format.DAGService, nds ...format.Node) {
}

func TestRoundtrip(t *testing.T) {
ctx := context.Background()
dserv := dstest.Mock()
a := merkledag.NewRawNode([]byte("aaaa"))
b := merkledag.NewRawNode([]byte("bbbb"))
Expand All @@ -48,7 +49,7 @@ func TestRoundtrip(t *testing.T) {
}

bserv := dstest.Bserv()
ch, err := car.LoadCar(bserv.Blockstore(), buf)
ch, err := car.LoadCar(ctx, bserv.Blockstore(), buf)
if err != nil {
t.Fatal(err)
}
Expand All @@ -63,7 +64,7 @@ func TestRoundtrip(t *testing.T) {

bs := bserv.Blockstore()
for _, nd := range []format.Node{a, b, c, nd1, nd2, nd3} {
has, err := bs.Has(nd.Cid())
has, err := bs.Has(ctx, nd.Cid())
if err != nil {
t.Fatal(err)
}
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ module github.com/ipld/go-car
require (
github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-datastore v0.5.1 // indirect
github.com/ipfs/go-ipfs-blockstore v1.1.2 // indirect
github.com/ipfs/go-ipld-cbor v0.0.5
github.com/ipfs/go-ipld-format v0.2.0
github.com/ipfs/go-merkledag v0.3.2
github.com/ipld/go-codec-dagpb v1.2.0
github.com/ipld/go-ipld-prime v0.12.3
github.com/multiformats/go-multihash v0.0.15
github.com/ipfs/go-merkledag v0.5.1
github.com/ipld/go-codec-dagpb v1.3.0
github.com/ipld/go-ipld-prime v0.14.3-0.20211207234443-319145880958
github.com/multiformats/go-multihash v0.1.0
github.com/stretchr/testify v1.7.0
)

Expand Down
Loading