From 9483624c1cb4893e8a32b96d735f28db825a5ccc Mon Sep 17 00:00:00 2001 From: Laurent Senta Date: Thu, 22 Jun 2023 09:55:20 +0200 Subject: [PATCH] feat: add inverse and version to filter cmd --- cmd/car/car.go | 10 ++++++++++ cmd/car/filter.go | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/cmd/car/car.go b/cmd/car/car.go index d8ee0ce8..6e7c63a2 100644 --- a/cmd/car/car.go +++ b/cmd/car/car.go @@ -116,6 +116,16 @@ func main1() int { Name: "append", Usage: "Append cids to an existing output file", }, + &cli.BoolFlag{ + Name: "inverse", + Usage: "Inverse the filter (this will remove cids from the car file)", + Value: false, + }, + &cli.IntFlag{ + Name: "version", + Value: 2, + Usage: "Write output as a v1 or v2 format car", + }, }, }, { diff --git a/cmd/car/filter.go b/cmd/car/filter.go index a76b6bd0..3c163049 100644 --- a/cmd/car/filter.go +++ b/cmd/car/filter.go @@ -42,15 +42,30 @@ func FilterCar(c *cli.Context) error { if err != nil { return err } - fmt.Printf("filtering to %d cids\n", len(cidMap)) + if c.Bool("inverse") { + fmt.Printf("filtering out %d cids\n", len(cidMap)) + } else { + fmt.Printf("filtering to %d cids\n", len(cidMap)) + } outRoots := make([]cid.Cid, 0) for _, r := range rd.Roots { - if _, ok := cidMap[r]; ok { + if matchFilter(c, r, cidMap) { outRoots = append(outRoots, r) } } + version := c.Int("version") + options := []carv2.Option{} + switch version { + case 1: + options = []carv2.Option{blockstore.WriteAsCarV1(true)} + case 2: + // already the default + default: + return fmt.Errorf("invalid CAR version %d", c.Int("version")) + } + outPath := c.Args().Get(1) if !c.Bool("append") { if _, err := os.Stat(outPath); err == nil || !os.IsNotExist(err) { @@ -60,6 +75,10 @@ func FilterCar(c *cli.Context) error { } } } else { + if version != 2 { + return fmt.Errorf("can only append to version 2 car files") + } + // roots will need to be whatever is in the output already. cv2r, err := carv2.OpenReader(outPath) if err != nil { @@ -79,7 +98,7 @@ func FilterCar(c *cli.Context) error { fmt.Fprintf(os.Stderr, "warning: no roots defined after filtering\n") } - bs, err := blockstore.OpenReadWrite(outPath, outRoots) + bs, err := blockstore.OpenReadWrite(outPath, outRoots, options...) if err != nil { return err } @@ -92,7 +111,7 @@ func FilterCar(c *cli.Context) error { } return err } - if _, ok := cidMap[blk.Cid()]; ok { + if matchFilter(c, blk.Cid(), cidMap) { if err := bs.Put(c.Context, blk); err != nil { return err } @@ -101,6 +120,13 @@ func FilterCar(c *cli.Context) error { return bs.Finalize() } +func matchFilter(ctx *cli.Context, c cid.Cid, cidMap map[cid.Cid]struct{}) bool { + if _, ok := cidMap[c]; ok { + return !ctx.Bool("inverse") + } + return ctx.Bool("inverse") +} + func parseCIDS(r io.Reader) (map[cid.Cid]struct{}, error) { cids := make(map[cid.Cid]struct{}) br := bufio.NewReader(r)