erigon-pulse/kv/bitmapdb/fixed_size_test.go
Alex Sharov ac40ca5269
e3: locality index (#823)
Mainnet:
```
447M	accounts.0-544.l
45M	accounts.0-544.li
133M	code.0-544.l
14M	code.0-544.li
2.0G	storage.0-544.l
197M	storage.0-544.li
```

Decided no to use Roaring - because it can only keep full bitmap in RAM
(no way to stream into file). But it's more compact 2Gb -> 1.4Gb. Maybe
can shard large bitmap - or do other trick (storage has 1B keys -
sharding probably is cheap). Maybe in the future.
2023-01-07 12:30:57 +07:00

91 lines
2.3 KiB
Go

package bitmapdb
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestFixedSizeBitmaps(t *testing.T) {
tmpDir, require := t.TempDir(), require.New(t)
must := require.NoError
idxPath := filepath.Join(tmpDir, "idx.tmp")
wr, err := NewFixedSizeBitmapsWriter(idxPath, 14, 7)
require.NoError(err)
defer wr.Close()
must(wr.AddArray(0, []uint64{3, 9, 11}))
must(wr.AddArray(1, []uint64{1, 2, 3}))
must(wr.AddArray(2, []uint64{4, 8, 13}))
must(wr.AddArray(3, []uint64{1, 13}))
must(wr.AddArray(4, []uint64{1, 13}))
must(wr.AddArray(5, []uint64{1, 13}))
must(wr.AddArray(6, []uint64{0, 9, 13}))
must(wr.AddArray(7, []uint64{7}))
require.Error(wr.AddArray(8, []uint64{8}))
err = wr.Build()
require.NoError(err)
bm, err := OpenFixedSizeBitmaps(idxPath, 14)
require.NoError(err)
defer bm.Close()
at := func(item uint64) []uint64 {
n, err := bm.At(item)
require.NoError(err)
return n
}
require.Equal([]uint64{3, 9, 11}, at(0))
require.Equal([]uint64{1, 2, 3}, at(1))
require.Equal([]uint64{4, 8, 13}, at(2))
require.Equal([]uint64{1, 13}, at(3))
require.Equal([]uint64{1, 13}, at(4))
require.Equal([]uint64{1, 13}, at(5))
require.Equal([]uint64{0, 9, 13}, at(6))
require.Equal([]uint64{7}, at(7))
fst, snd, ok, ok2, err := bm.First2At(7, 0)
require.NoError(err)
require.Equal(uint64(7), fst)
require.Equal(uint64(0), snd)
require.Equal(true, ok)
require.Equal(false, ok2)
fst, snd, ok, ok2, err = bm.First2At(2, 8)
require.NoError(err)
require.Equal(uint64(8), fst)
require.Equal(uint64(13), snd)
require.Equal(true, ok)
require.Equal(true, ok2)
fst, snd, ok, ok2, err = bm.First2At(2, 9)
require.NoError(err)
require.Equal(uint64(13), fst)
require.Equal(uint64(0), snd)
require.Equal(true, ok)
require.Equal(false, ok2)
_, err = bm.At(8)
require.Error(err)
}
func TestPageAlined(t *testing.T) {
tmpDir, require := t.TempDir(), require.New(t)
idxPath := filepath.Join(tmpDir, "idx.tmp")
bm2, err := NewFixedSizeBitmapsWriter(idxPath, 128, 100)
require.NoError(err)
require.Equal((128/8*100/os.Getpagesize()+1)*os.Getpagesize(), bm2.size)
defer bm2.Close()
bm2.Close()
bm3, err := NewFixedSizeBitmapsWriter(idxPath, 128, 1000)
require.NoError(err)
require.Equal((128/8*1000/os.Getpagesize()+1)*os.Getpagesize(), bm3.size)
defer bm3.Close()
}