Files
zapdb/histogram_test.go
zeekayandHanzo Dev fe8d85d663 rename package badger -> zapdb
The module has been github.com/luxfi/zapdb for a while, but the package inside
it still declared itself badger, so every consumer had to write

	zapdb "github.com/luxfi/zapdb" // package badger

and the alias existed only to paper over the mismatch. 56 root files and the
134 qualified references inside the repo now say zapdb.

Backward compatible in both directions: an existing consumer with the alias
keeps compiling (the alias is now merely redundant), and one without it starts
compiling instead of failing on an undefined zapdb.

Directory badger/ and its cmd package are untouched — that is the CLI, and its
binary is named badger.

Build and vet clean; suite green except TestZRestore, which wants a /tmp/zb/src
benchmark fixture and fails identically without this change.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 14:24:18 -07:00

94 lines
2.9 KiB
Go

/*
* SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package zapdb
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestBuildKeyValueSizeHistogram(t *testing.T) {
t.Run("All same size key-values", func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
entries := int64(40)
err := db.Update(func(txn *Txn) error {
for i := rune(0); i < rune(entries); i++ {
err := txn.SetEntry(NewEntry([]byte(string(i)), []byte("B")))
if err != nil {
return err
}
}
return nil
})
require.NoError(t, err)
histogram := db.buildHistogram(nil)
keyHistogram := histogram.keySizeHistogram
valueHistogram := histogram.valueSizeHistogram
require.Equal(t, entries, keyHistogram.totalCount)
require.Equal(t, entries, valueHistogram.totalCount)
// Each entry is of size one. So the sum of sizes should be the same
// as number of entries
require.Equal(t, entries, valueHistogram.sum)
require.Equal(t, entries, keyHistogram.sum)
// All value sizes are same. The first bin should have all the values.
require.Equal(t, entries, valueHistogram.countPerBin[0])
require.Equal(t, entries, keyHistogram.countPerBin[0])
require.Equal(t, int64(1), keyHistogram.max)
require.Equal(t, int64(1), keyHistogram.min)
require.Equal(t, int64(1), valueHistogram.max)
require.Equal(t, int64(1), valueHistogram.min)
})
})
t.Run("different size key-values", func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
entries := int64(3)
err := db.Update(func(txn *Txn) error {
if err := txn.SetEntry(NewEntry([]byte("A"), []byte("B"))); err != nil {
return err
}
if err := txn.SetEntry(NewEntry([]byte("AA"), []byte("BB"))); err != nil {
return err
}
return txn.SetEntry(NewEntry([]byte("AAA"), []byte("BBB")))
})
require.NoError(t, err)
histogram := db.buildHistogram(nil)
keyHistogram := histogram.keySizeHistogram
valueHistogram := histogram.valueSizeHistogram
require.Equal(t, entries, keyHistogram.totalCount)
require.Equal(t, entries, valueHistogram.totalCount)
// Each entry is of size one. So the sum of sizes should be the same
// as number of entries
require.Equal(t, int64(6), valueHistogram.sum)
require.Equal(t, int64(6), keyHistogram.sum)
// Length 1 key is in first bucket, length 2 and 3 are in the second
// bucket
require.Equal(t, int64(1), valueHistogram.countPerBin[0])
require.Equal(t, int64(2), valueHistogram.countPerBin[1])
require.Equal(t, int64(1), keyHistogram.countPerBin[0])
require.Equal(t, int64(2), keyHistogram.countPerBin[1])
require.Equal(t, int64(3), keyHistogram.max)
require.Equal(t, int64(1), keyHistogram.min)
require.Equal(t, int64(3), valueHistogram.max)
require.Equal(t, int64(1), valueHistogram.min)
})
})
}