From 10b100fe0f4f6aae6c4a7971fab16c755214de3f Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Sat, 5 Dec 2015 19:19:08 +0100 Subject: [PATCH] Fix `bolt stats` output --- cmd/bolt/main.go | 5 ++++- cmd/bolt/main_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/cmd/bolt/main.go b/cmd/bolt/main.go index c41ebe4..b96e6f7 100644 --- a/cmd/bolt/main.go +++ b/cmd/bolt/main.go @@ -825,7 +825,10 @@ func (cmd *StatsCommand) Run(args ...string) error { fmt.Fprintln(cmd.Stdout, "Bucket statistics") fmt.Fprintf(cmd.Stdout, "\tTotal number of buckets: %d\n", s.BucketN) - percentage = int(float32(s.InlineBucketN) * 100.0 / float32(s.BucketN)) + percentage = 0 + if s.BucketN != 0 { + percentage = int(float32(s.InlineBucketN) * 100.0 / float32(s.BucketN)) + } fmt.Fprintf(cmd.Stdout, "\tTotal number on inlined buckets: %d (%d%%)\n", s.InlineBucketN, percentage) percentage = 0 if s.LeafInuse != 0 { diff --git a/cmd/bolt/main_test.go b/cmd/bolt/main_test.go index b9e8c67..c378b79 100644 --- a/cmd/bolt/main_test.go +++ b/cmd/bolt/main_test.go @@ -24,6 +24,46 @@ func TestInfoCommand_Run(t *testing.T) { } } +// Ensure the "stats" command executes correctly with an empty database. +func TestStatsCommand_Run_EmptyDatabase(t *testing.T) { + // Ignore + if os.Getpagesize() != 4096 { + t.Skip("system does not use 4KB page size") + } + + db := MustOpen(0666, nil) + defer db.Close() + db.DB.Close() + + // Generate expected result. + exp := "Aggregate statistics for 0 buckets\n\n" + + "Page count statistics\n" + + "\tNumber of logical branch pages: 0\n" + + "\tNumber of physical branch overflow pages: 0\n" + + "\tNumber of logical leaf pages: 0\n" + + "\tNumber of physical leaf overflow pages: 0\n" + + "Tree statistics\n" + + "\tNumber of keys/value pairs: 0\n" + + "\tNumber of levels in B+tree: 0\n" + + "Page size utilization\n" + + "\tBytes allocated for physical branch pages: 0\n" + + "\tBytes actually used for branch data: 0 (0%)\n" + + "\tBytes allocated for physical leaf pages: 0\n" + + "\tBytes actually used for leaf data: 0 (0%)\n" + + "Bucket statistics\n" + + "\tTotal number of buckets: 0\n" + + "\tTotal number on inlined buckets: 0 (0%)\n" + + "\tBytes used for inlined buckets: 0 (0%)\n" + + // Run the command. + m := NewMain() + if err := m.Run("stats", db.Path); err != nil { + t.Fatal(err) + } else if m.Stdout.String() != exp { + t.Fatalf("unexpected stdout:\n\n%s", m.Stdout.String()) + } +} + // Ensure the "stats" command can execute correctly. func TestStatsCommand_Run(t *testing.T) { // Ignore