Skip to content

Commit

Permalink
Add list and filter commands (#227)
Browse files Browse the repository at this point in the history
* better name for detaching index command
* add list and filter commands
  • Loading branch information
willscott authored Sep 10, 2021
1 parent 385b612 commit afcc7b3
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 6 deletions.
26 changes: 22 additions & 4 deletions v2/cmd/car/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,28 @@ func main() {
},
},
{
Name: "split",
Aliases: []string{"s"},
Usage: "Split an index to a detached file",
Action: SplitCar,
Name: "detach-index",
Usage: "Detach an index to a detached file",
Action: DetachCar,
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "List the CIDs in a car",
Action: ListCar,
},
{
Name: "filter",
Aliases: []string{"f"},
Usage: "Filter the CIDs in a car",
Action: FilterCar,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cid-file",
Usage: "A file to read CIDs from",
TakesFile: true,
},
},
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions v2/cmd/car/split.go → v2/cmd/car/detach.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/urfave/cli/v2"
)

// SplitCar is a command to output the index part of a car.
func SplitCar(c *cli.Context) error {
// DetachCar is a command to output the index part of a car.
func DetachCar(c *cli.Context) error {
r, err := carv2.OpenReader(c.Args().Get(0))
if err != nil {
return err
Expand Down
98 changes: 98 additions & 0 deletions v2/cmd/car/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"bufio"
"fmt"
"io"
"os"
"strings"

"github.com/ipfs/go-cid"
carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/blockstore"
icarv1 "github.com/ipld/go-car/v2/internal/carv1"
"github.com/urfave/cli/v2"
)

// FilterCar is a command to select a subset of a car by CID.
func FilterCar(c *cli.Context) error {
r, err := carv2.OpenReader(c.Args().Get(0))
if err != nil {
return err
}
defer r.Close()

if c.Args().Len() < 2 {
return fmt.Errorf("an output filename must be provided")
}
roots, err := r.Roots()
if err != nil {
return err
}
bs, err := blockstore.OpenReadWrite(c.Args().Get(1), roots)
if err != nil {
return err
}

// Get the set of CIDs from stdin.
inStream := os.Stdin
if c.IsSet("cidFile") {
inStream, err = os.Open(c.String("cidFile"))
if err != nil {
return err
}
defer inStream.Close()
}
cidMap, err := parseCIDS(inStream)
if err != nil {
return err
}
fmt.Printf("filtering to %d cids\n", len(cidMap))

rd, err := icarv1.NewCarReader(r.DataReader())
if err != nil {
return err
}

for {
blk, err := rd.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
if _, ok := cidMap[blk.Cid()]; ok {
if err := bs.Put(blk); err != nil {
return err
}
}
}
return bs.Finalize()
}

func parseCIDS(r io.Reader) (map[cid.Cid]struct{}, error) {
cids := make(map[cid.Cid]struct{})
br := bufio.NewReader(r)
for {
line, _, err := br.ReadLine()
if err != nil {
if err == io.EOF {
return cids, nil
}
return nil, err
}
trimLine := strings.TrimSpace(string(line))
if len(trimLine) == 0 {
continue
}
c, err := cid.Parse(trimLine)
if err != nil {
return nil, err
}
if _, ok := cids[c]; ok {
fmt.Fprintf(os.Stderr, "duplicate cid: %s\n", c)
}
cids[c] = struct{}{}
}
}
53 changes: 53 additions & 0 deletions v2/cmd/car/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"io"
"os"

carv2 "github.com/ipld/go-car/v2"
"github.com/urfave/cli/v2"
)

// ListCar is a command to output the cids in a car.
func ListCar(c *cli.Context) error {
inStream := os.Stdin
var err error
if c.Args().Len() >= 1 {
inStream, err = os.Open(c.Args().First())
if err != nil {
return err
}
defer inStream.Close()
}
rd, err := carv2.NewBlockReader(inStream)
if err != nil {
return err
}

outStream := os.Stdout
if c.Args().Len() >= 2 {
outStream, err = os.Create(c.Args().Get(1))
if err != nil {
return err
}
}
defer outStream.Close()

if err != nil {
return err
}

for {
blk, err := rd.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
fmt.Fprintf(outStream, "%s\n", blk.Cid())
}

return err
}

0 comments on commit afcc7b3

Please sign in to comment.