Skip to content

Commit

Permalink
Fix the build breakage and improve the output so it is machine parsea…
Browse files Browse the repository at this point in the history
…ble.
  • Loading branch information
manishrjain committed Mar 8, 2019
1 parent 75e07ee commit ed1c357
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
12 changes: 5 additions & 7 deletions badger/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ to the Dgraph team.
}
if opt.sizeHistogram {
// use prefix as nil since we want to list all keys
db.ShowKeyValueSizeHistogram(nil)
db.PrintHistogram(nil)
}
},
}
Expand All @@ -104,16 +104,14 @@ func dur(src, dst time.Time) string {

func tableInfo(dir, valueDir string, db *badger.DB) error {
tables := db.Tables()
fmt.Printf("\n%s SSTables %[1]s\n", strings.Repeat("=", 45))
fmt.Printf("%-5s\t%-10s\t%-30s\t%-30s\t%-7s\n", "ID", "Level",
"Left-Key(in hex) (Time)", "Right-Key(in hex) (Time)", "Total Keys")
fmt.Printf("%s\n", strings.Repeat("=", 100))
fmt.Println()
fmt.Println("SSTable [Li, Id, Total Keys including internal keys] [Left Key, Version -> Right Key, Version]")
for _, t := range tables {
lk, lt := y.ParseKey(t.Left), y.ParseTs(t.Left)
rk, rt := y.ParseKey(t.Right), y.ParseTs(t.Right)

fmt.Printf("%-5d\tL%-9d\t%-30s\t%-30s\t%-7d\n", t.ID, t.Level,
fmt.Sprintf("%X (v%d)", lk, lt), fmt.Sprintf("%X (v%d)", rk, rt), t.KeyCount)
fmt.Printf("SSTable [L%d, %03d, %07d] [%20X, v%d -> %20X, v%d]\n",
t.Level, t.ID, t.KeyCount, lk, lt, rk, rt)
}
fmt.Println()
return nil
Expand Down
22 changes: 11 additions & 11 deletions histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import (
"math"
)

// PrintKeyValueHistogram builds and displays the key-value size histogram.
// PrintHistogram builds and displays the key-value size histogram.
// When keyPrefix is set, only the keys that have prefix "keyPrefix" are
// considered for creating the histogram
func (db *DB) PrintKeyValueHistogram(keyPrefix []byte) {
func (db *DB) PrintHistogram(keyPrefix []byte) {
if db == nil {
fmt.Println("\nCannot build histogram: DB is nil.")
return
}
histogram := db.buildKeyValueSizeHistogram(keyPrefix)
histogram := db.buildHistogram(keyPrefix)
fmt.Printf("Histogram of key sizes (in bytes)\n")
histogram.keySizeHistogram.printHistogram()
fmt.Printf("Histogram of value sizes (in bytes)\n")
Expand All @@ -46,18 +46,18 @@ type histogramData struct {
sum int64
}

// keyValueSizeHistogram contains keySize histogram and valueSize histogram
type keyValueSizeHistogram struct {
// sizeHistogram contains keySize histogram and valueSize histogram
type sizeHistogram struct {
keySizeHistogram, valueSizeHistogram histogramData
}

// newKeyValueSizeHistogram returns a new instance of keyValueSizeHistogram with
// newSizeHistogram returns a new instance of keyValueSizeHistogram with
// properly initialized fields.
func newKeyValueSizeHistogram() *keyValueSizeHistogram {
func newSizeHistogram() *sizeHistogram {
// TODO(ibrahim): find appropriate bin size.
keyBins := createHistogramBins(1, 16)
valueBins := createHistogramBins(1, 30)
return &keyValueSizeHistogram{
return &sizeHistogram{
keySizeHistogram: histogramData{
bins: keyBins,
countPerBin: make([]int64, len(keyBins)+1),
Expand Down Expand Up @@ -113,17 +113,17 @@ func (histogram *histogramData) Update(value int64) {
}
}

// buildKeyValueSizeHistogram builds the key-value size histogram.
// buildHistogram builds the key-value size histogram.
// When keyPrefix is set, only the keys that have prefix "keyPrefix" are
// considered for creating the histogram
func (db *DB) buildKeyValueSizeHistogram(keyPrefix []byte) *keyValueSizeHistogram {
func (db *DB) buildHistogram(keyPrefix []byte) *sizeHistogram {
txn := db.NewTransaction(false)
defer txn.Discard()

itr := txn.NewIterator(DefaultIteratorOptions)
defer itr.Close()

badgerHistogram := newKeyValueSizeHistogram()
badgerHistogram := newSizeHistogram()

// Collect key and value sizes.
for itr.Seek(keyPrefix); itr.ValidForPrefix(keyPrefix); itr.Next() {
Expand Down
4 changes: 2 additions & 2 deletions histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestBuildKeyValueSizeHistogram(t *testing.T) {
})
require.NoError(t, err)

histogram := db.buildKeyValueSizeHistogram(nil)
histogram := db.buildHistogram(nil)
keyHistogram := histogram.keySizeHistogram
valueHistogram := histogram.valueSizeHistogram

Expand Down Expand Up @@ -79,7 +79,7 @@ func TestBuildKeyValueSizeHistogram(t *testing.T) {
})
require.NoError(t, err)

histogram := db.buildKeyValueSizeHistogram(nil)
histogram := db.buildHistogram(nil)
keyHistogram := histogram.keySizeHistogram
valueHistogram := histogram.valueSizeHistogram

Expand Down

0 comments on commit ed1c357

Please sign in to comment.