Skip to main content

histogram.go - Overview

  1. Overview This file provides functionality to build and display histograms of key and value sizes in a Badger database. It includes functions to create bins, update histogram data, build the histogram from the database, and print the histogram in a human-readable format.

  2. Detailed Documentation

Function: (db *DB) PrintHistogram(keyPrefix []byte)

  • Purpose: Builds and displays the key-value size histogram. If keyPrefix is provided, only keys with that prefix are considered.
  • Parameters:
    • keyPrefix ([]byte): The prefix to filter keys by. If nil or empty, all keys are considered.
  • Returns: None

Type: histogramData

  • Purpose: Stores data related to a histogram, including bins, counts per bin, total count, min, max, and sum of values.
  • Fields:
    • bins ([]int64): The boundaries of the histogram bins.
    • countPerBin ([]int64): The number of values falling into each bin.
    • totalCount (int64): The total number of values in the histogram.
    • min (int64): The minimum value in the histogram.
    • max (int64): The maximum value in the histogram.
    • sum (int64): The sum of all values in the histogram.

Type: sizeHistogram

  • Purpose: Contains two histogramData fields, one for key sizes and one for value sizes.
  • Fields:
    • keySizeHistogram (histogramData): Histogram data for key sizes.
    • valueSizeHistogram (histogramData): Histogram data for value sizes.

Function: newSizeHistogram() *sizeHistogram

  • Purpose: Creates and initializes a new sizeHistogram with pre-defined key and value bins.
  • Parameters: None
  • Returns: A pointer to a new sizeHistogram instance.

Function: createHistogramBins(minExponent, maxExponent uint32) []int64

  • Purpose: Creates bins for a histogram with sizes that are powers of two, ranging from 2^minExponent to 2^maxExponent.
  • Parameters:
    • minExponent (uint32): The minimum exponent for the bin sizes.
    • maxExponent (uint32): The maximum exponent for the bin sizes.
  • Returns: A slice of int64 representing the bin boundaries.

Function: (histogram *histogramData) Update(value int64)

  • Purpose: Updates the histogram data with a new value, updating the min, max, sum, and bin counts accordingly.
  • Parameters:
    • value (int64): The value to add to the histogram.
  • Returns: None

Function: (db *DB) buildHistogram(keyPrefix []byte) *sizeHistogram

  • Purpose: Builds a sizeHistogram by iterating through the Badger database and collecting key and value sizes. If keyPrefix is provided, only keys with that prefix are considered.
  • Parameters:
    • keyPrefix ([]byte): The prefix to filter keys by. If nil or empty, all keys are considered.
  • Returns: A pointer to a sizeHistogram containing the key and value size histograms.

Function: (histogram histogramData) printHistogram()

  • Purpose: Prints the histogram data in a human-readable format, including total count, min, max, mean, and the count for each bin range.
  • Parameters: None
  • Returns: None
  1. Code Examples
// Example usage (assuming db is an initialized *DB):
// db.PrintHistogram([]byte("prefix")) //builds and prints histogram for keys with the prefix "prefix"
  1. Getting Started Relevance