Skip to content

Commit

Permalink
Only print stats when --stats flag is passed
Browse files Browse the repository at this point in the history
This applies to both text and json output encoings.

- Stats data is now contained within a Stats datastructure
- Stats are printed after root so that first line of output is the same as previously, even when stats are output using --stats
  • Loading branch information
gammazero committed Sep 7, 2021
1 parent daab857 commit 875c3f0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
25 changes: 17 additions & 8 deletions core/commands/dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
)

const (
pinRootsOptionName = "pin-roots"
progressOptionName = "progress"
silentOptionName = "silent"
pinRootsOptionName = "pin-roots"
statsOptionName = "stats"
)

// DagCmd provides a subset of commands for interacting with ipld dag objects
Expand Down Expand Up @@ -53,10 +54,14 @@ type ResolveOutput struct {
RemPath string
}

type CarImportStats struct {
BlockCount uint64
}

// CarImportOutput is the output type of the 'dag import' commands
type CarImportOutput struct {
BlockCount *uint64 `json:",omitempty"`
Root *RootMeta `json:",omitempty"`
Root *RootMeta `json:",omitempty"`
Stats *CarImportStats `json:",omitempty"`
}

// RootMeta is the metadata for a root pinning response
Expand Down Expand Up @@ -195,8 +200,9 @@ Maximum supported CAR version: 1
cmds.FileArg("path", true, true, "The path of a .car file.").EnableStdin(),
},
Options: []cmds.Option{
cmds.BoolOption(silentOptionName, "No output."),
cmds.BoolOption(pinRootsOptionName, "Pin optional roots listed in the .car headers after importing.").WithDefault(true),
cmds.BoolOption(silentOptionName, "No output."),
cmds.BoolOption(statsOptionName, "Output stats."),
},
Type: CarImportOutput{},
Run: dagImport,
Expand All @@ -208,16 +214,19 @@ Maximum supported CAR version: 1
return nil
}

// event should have only one of `Root` or `BlockCount` set, not both
// event should have only one of `Root` or `Stats` set, not both
if event.Root == nil {
if event.BlockCount == nil {
if event.Stats == nil {
return fmt.Errorf("Unexpected message from DAG import")
}
fmt.Fprintf(w, "Imported %d blocks\n", *event.BlockCount)
stats, _ := req.Options[statsOptionName].(bool)
if stats {
fmt.Fprintf(w, "Imported %d blocks\n", event.Stats.BlockCount)
}
return nil
}

if event.BlockCount != nil {
if event.Stats != nil {
return fmt.Errorf("Unexpected message from DAG import")
}

Expand Down
15 changes: 11 additions & 4 deletions core/commands/dag/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ func dagImport(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment
return done.err
}

if err := res.Emit(&CarImportOutput{BlockCount: &done.blockCount}); err != nil {
return err
}

// It is not guaranteed that a root in a header is actually present in the same ( or any )
// .car file. This is the case in version 1, and ideally in further versions too
// Accumulate any root CID seen in a header, and supplement its actual node if/when encountered
Expand Down Expand Up @@ -119,6 +115,17 @@ func dagImport(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment
}
}

stats, _ := req.Options[statsOptionName].(bool)
if stats {
if err := res.Emit(&CarImportOutput{
Stats: &CarImportStats{
BlockCount: done.blockCount,
},
}); err != nil {
return err
}
}

return nil
}

Expand Down

0 comments on commit 875c3f0

Please sign in to comment.