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

feat: plumb through context changes #268

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 2 additions & 2 deletions car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestRoundtrip(t *testing.T) {
}

bserv := dstest.Bserv()
ch, err := car.LoadCar(bserv.Blockstore(), buf)
ch, err := car.LoadCar(context.Background(), bserv.Blockstore(), buf)
if err != nil {
t.Fatal(err)
}
Expand All @@ -63,7 +63,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(context.Background(), nd.Cid())
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/car/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func writeFiles(ctx context.Context, bs *blockstore.ReadWrite, paths ...string)
if !ok {
return nil, fmt.Errorf("not a cidlink")
}
blk, err := bs.Get(cl.Cid)
blk, err := bs.Get(ctx, cl.Cid)
if err != nil {
return nil, err
}
Expand All @@ -86,7 +86,7 @@ func writeFiles(ctx context.Context, bs *blockstore.ReadWrite, paths ...string)
if err != nil {
return err
}
bs.Put(blk)
bs.Put(ctx, blk)
return nil
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/car/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ExtractCar(c *cli.Context) error {
if !ok {
return nil, fmt.Errorf("not a cidlink")
}
blk, err := bs.Get(cl.Cid)
blk, err := bs.Get(c.Context, cl.Cid)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/car/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func FilterCar(c *cli.Context) error {
return err
}
if _, ok := cidMap[blk.Cid()]; ok {
if err := bs.Put(blk); err != nil {
if err := bs.Put(c.Context, blk); err != nil {
return err
}
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/car/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func GetCarBlock(c *cli.Context) error {
return err
}

blk, err := bs.Get(blkCid)
blk, err := bs.Get(c.Context, blkCid)
if err != nil {
return err
}
Expand Down Expand Up @@ -97,15 +97,15 @@ func GetCarDag(c *cli.Context) error {

switch c.Int("version") {
case 2:
return writeCarV2(rootCid, output, bs, strict, sel, linkVisitOnlyOnce)
return writeCarV2(c.Context, rootCid, output, bs, strict, sel, linkVisitOnlyOnce)
case 1:
return writeCarV1(rootCid, output, bs, strict, sel, linkVisitOnlyOnce)
return writeCarV1(c.Context, rootCid, output, bs, strict, sel, linkVisitOnlyOnce)
default:
return fmt.Errorf("invalid CAR version %d", c.Int("version"))
}
}

func writeCarV2(rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict bool, sel datamodel.Node, linkVisitOnlyOnce bool) error {
func writeCarV2(ctx context.Context, rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict bool, sel datamodel.Node, linkVisitOnlyOnce bool) error {
_ = os.Remove(output)

outStore, err := blockstore.OpenReadWrite(output, []cid.Cid{rootCid}, blockstore.AllowDuplicatePuts(false))
Expand All @@ -117,7 +117,7 @@ func writeCarV2(rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict
ls.TrustedStorage = true
ls.StorageReadOpener = func(_ linking.LinkContext, l datamodel.Link) (io.Reader, error) {
if cl, ok := l.(cidlink.Link); ok {
blk, err := bs.Get(cl.Cid)
blk, err := bs.Get(ctx, cl.Cid)
if err != nil {
if err == ipfsbs.ErrNotFound {
if strict {
Expand Down Expand Up @@ -167,12 +167,12 @@ func writeCarV2(rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict
return outStore.Finalize()
}

func writeCarV1(rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict bool, sel datamodel.Node, linkVisitOnlyOnce bool) error {
func writeCarV1(ctx context.Context, rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict bool, sel datamodel.Node, linkVisitOnlyOnce bool) error {
opts := make([]car.Option, 0)
if linkVisitOnlyOnce {
opts = append(opts, car.TraverseLinksOnlyOnce())
}
sc := car.NewSelectiveCar(context.Background(), bs, []car.Dag{{Root: rootCid, Selector: sel}}, opts...)
sc := car.NewSelectiveCar(ctx, bs, []car.Dag{{Root: rootCid, Selector: sel}}, opts...)
f, err := os.Create(output)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/car/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func listUnixfs(c *cli.Context, outStream io.Writer) error {
if !ok {
return nil, fmt.Errorf("not a cidlink")
}
blk, err := bs.Get(cl.Cid)
blk, err := bs.Get(c.Context, cl.Cid)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-cid v0.1.0
github.com/ipfs/go-ipfs-blockstore v1.0.3
github.com/ipfs/go-ipfs-blockstore v1.1.1
github.com/ipfs/go-unixfsnode v1.1.4-0.20211105121048-b9b6e9dc571e
github.com/ipld/go-car v0.3.2-0.20211001222544-c93f5367a75c
github.com/ipld/go-car/v2 v2.1.0
github.com/ipld/go-car v0.3.3-0.20211122222057-8a25087f0f84
github.com/ipld/go-car/v2 v2.1.1-0.20211122222057-8a25087f0f84
github.com/ipld/go-codec-dagpb v1.3.0
github.com/ipld/go-ipld-prime v0.12.4-0.20211014180653-3ba656a3bc6b
github.com/multiformats/go-multicodec v0.3.1-0.20210902112759-1539a079fd61
Expand Down
Loading