Skip to content

Commit

Permalink
update context datastore
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Dec 10, 2021
1 parent c35591a commit b04ac83
Show file tree
Hide file tree
Showing 6 changed files with 925 additions and 118 deletions.
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

0 comments on commit b04ac83

Please sign in to comment.