Skip to content

Commit

Permalink
Merge cars command (#125)
Browse files Browse the repository at this point in the history
* add merge car method

* add merge car method

* header

* duplicate const
  • Loading branch information
anjor authored Jul 30, 2024
1 parent c9b1840 commit bcfb24a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
91 changes: 91 additions & 0 deletions cmd-merge-cars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"bufio"
"encoding/binary"
"fmt"
"io"
"os"

"github.com/urfave/cli/v2"
)

const varintSize = 10

func newCmd_MergeCars() *cli.Command {
var outputFile string
return &cli.Command{
Name: "merge-cars",
Description: "Merges split car files into a single file",
Usage: "Merges split car files into a single file",
ArgsUsage: "<list of car files>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output-file",
Aliases: []string{"o"},
Usage: "Output file name",
Required: true,
Destination: &outputFile,
},
},
Action: func(c *cli.Context) error {
paths := c.Args().Slice()

out, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer out.Close()

w := bufio.NewWriter(out)

if _, err := io.WriteString(w, nulRootCarHeader); err != nil {
return fmt.Errorf("failed to write empty header: %w", err)
}

for _, path := range paths {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open input file %s: %w", path, err)
}
defer f.Close()

r := bufio.NewReader(f)
err = discardHeader(r)
if err != nil {
return fmt.Errorf("failed to discard header: %w", err)
}

io.Copy(w, r)

}

return nil
},
}
}

func discardHeader(streamBuf *bufio.Reader) error {
maybeHeaderLen, err := streamBuf.Peek(varintSize)
if err != nil {
return fmt.Errorf("failed to read header: %s\n", err)
}

hdrLen, viLen := binary.Uvarint(maybeHeaderLen)
if hdrLen <= 0 || viLen < 0 {
return fmt.Errorf("unexpected header len = %d, varint len = %d\n", hdrLen, viLen)
}

_, err = io.CopyN(io.Discard, streamBuf, int64(viLen))
if err != nil {
return fmt.Errorf("failed to discard header varint: %s\n", err)
}

// ignoring header decoding for now
_, err = io.CopyN(io.Discard, streamBuf, int64(hdrLen))
if err != nil {
return fmt.Errorf("failed to discard header header: %s\n", err)
}

return nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func main() {
newCmd_Version(),
newCmd_rpc(),
newCmd_check_deals(),
newCmd_MergeCars(),
newCmd_SplitCar(),
},
}
Expand Down

0 comments on commit bcfb24a

Please sign in to comment.