Skip to content

Commit

Permalink
[tool/oplog] Handles empty logs properly
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyanliu authored and Jack47 committed Jan 9, 2018
1 parent 57279b4 commit a0d5b7f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/tool/oplog/oplog.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ func RetrieveOpLog(c *cli.Context) error {
opLogPath := c.String("path")
opLog, err := gateway.NewOPLog(opLogPath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("open oplog failed: %v", err), 2)
return cli.NewExitError(fmt.Sprintf("open oplog failed: %v", err), 1)
}
defer opLog.Close()

maxSeq := opLog.MaxSeq()
begin := c.Uint64("begin")
if begin > maxSeq {
return cli.NewExitError(fmt.Sprintf("`begin` parameter invalid, it must be equal or lower than current max sequence: %d", maxSeq), 1)
return cli.NewExitError(
fmt.Sprintf("`begin` parameter invalid, it must be equal or lower than current max sequence: %d", maxSeq),
3)
}
count := c.Uint64("count")
if begin+count > maxSeq {
if maxSeq == 0 {
count = 0
} else if begin+count > maxSeq {
count = maxSeq - begin + 1
}

Expand All @@ -46,9 +50,12 @@ func RetrieveOpLog(c *cli.Context) error {
Count: count,
}

operations, err, _ := opLog.Retrieve(begin, count)
if err != nil {
return cli.NewExitError(fmt.Sprintf("retrieve oplog failed: %v", err), 3)
var operations []*gateway.Operation
if maxSeq > 0 {
operations, err, _ = opLog.Retrieve(begin, count)
if err != nil {
return cli.NewExitError(fmt.Sprintf("retrieve oplog failed: %v", err), 4)
}
}

oplogs.Operations = make([]json.RawMessage, len(operations))
Expand All @@ -61,8 +68,9 @@ func RetrieveOpLog(c *cli.Context) error {
}

if byt, err = json.Marshal(oplogs); err != nil {
return cli.NewExitError(fmt.Sprintf("marshal oplogs failed: %v", err), 4)
return cli.NewExitError(fmt.Sprintf("marshal oplogs failed: %v", err), 5)
}

fmt.Printf("%s\n", byt)
return nil
}

0 comments on commit a0d5b7f

Please sign in to comment.