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: add inverse and version to filter cmd #457

Merged
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
10 changes: 10 additions & 0 deletions cmd/car/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
{
Expand Down
34 changes: 30 additions & 4 deletions cmd/car/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
Expand Down