(cmd *cobra.Command, dbPath string, prefix string)
| 61 | } |
| 62 | |
| 63 | func statsFunc(cmd *cobra.Command, dbPath string, prefix string) error { |
| 64 | if _, err := checkSourceDBPath(dbPath); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | // open database. |
| 69 | db, err := bolt.Open(dbPath, 0600, &bolt.Options{ |
| 70 | ReadOnly: true, |
| 71 | PreLoadFreelist: true, |
| 72 | }) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | defer db.Close() |
| 77 | |
| 78 | return db.View(func(tx *bolt.Tx) error { |
| 79 | var s bolt.BucketStats |
| 80 | var count int |
| 81 | if err := tx.ForEach(func(name []byte, b *bolt.Bucket) error { |
| 82 | if bytes.HasPrefix(name, []byte(prefix)) { |
| 83 | s.Add(b.Stats()) |
| 84 | count += 1 |
| 85 | } |
| 86 | return nil |
| 87 | }); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | fmt.Fprintf(cmd.OutOrStdout(), "Aggregate statistics for %d buckets\n\n", count) |
| 92 | |
| 93 | fmt.Fprintln(cmd.OutOrStdout(), "Page count statistics") |
| 94 | fmt.Fprintf(cmd.OutOrStdout(), "\tNumber of logical branch pages: %d\n", s.BranchPageN) |
| 95 | fmt.Fprintf(cmd.OutOrStdout(), "\tNumber of physical branch overflow pages: %d\n", s.BranchOverflowN) |
| 96 | fmt.Fprintf(cmd.OutOrStdout(), "\tNumber of logical leaf pages: %d\n", s.LeafPageN) |
| 97 | fmt.Fprintf(cmd.OutOrStdout(), "\tNumber of physical leaf overflow pages: %d\n", s.LeafOverflowN) |
| 98 | |
| 99 | fmt.Fprintln(cmd.OutOrStdout(), "Tree statistics") |
| 100 | fmt.Fprintf(cmd.OutOrStdout(), "\tNumber of keys/value pairs: %d\n", s.KeyN) |
| 101 | fmt.Fprintf(cmd.OutOrStdout(), "\tNumber of levels in B+tree: %d\n", s.Depth) |
| 102 | |
| 103 | fmt.Fprintln(cmd.OutOrStdout(), "Page size utilization") |
| 104 | fmt.Fprintf(cmd.OutOrStdout(), "\tBytes allocated for physical branch pages: %d\n", s.BranchAlloc) |
| 105 | var percentage int |
| 106 | if s.BranchAlloc != 0 { |
| 107 | percentage = int(float32(s.BranchInuse) * 100.0 / float32(s.BranchAlloc)) |
| 108 | } |
| 109 | fmt.Fprintf(cmd.OutOrStdout(), "\tBytes actually used for branch data: %d (%d%%)\n", s.BranchInuse, percentage) |
| 110 | fmt.Fprintf(cmd.OutOrStdout(), "\tBytes allocated for physical leaf pages: %d\n", s.LeafAlloc) |
| 111 | percentage = 0 |
| 112 | if s.LeafAlloc != 0 { |
| 113 | percentage = int(float32(s.LeafInuse) * 100.0 / float32(s.LeafAlloc)) |
| 114 | } |
| 115 | fmt.Fprintf(cmd.OutOrStdout(), "\tBytes actually used for leaf data: %d (%d%%)\n", s.LeafInuse, percentage) |
| 116 | |
| 117 | fmt.Fprintln(cmd.OutOrStdout(), "Bucket statistics") |
| 118 | fmt.Fprintf(cmd.OutOrStdout(), "\tTotal number of buckets: %d\n", s.BucketN) |
| 119 | percentage = 0 |
| 120 | if s.BucketN != 0 { |
no test coverage detected