github.com/ledgerwatch/erigon-lib@v1.0.0/kv/bitmapdb/fixed_size_test.go (about) 1 /* 2 Copyright 2021 Erigon contributors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package bitmapdb 18 19 import ( 20 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/ledgerwatch/log/v3" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestFixedSizeBitmaps(t *testing.T) { 29 30 tmpDir, require := t.TempDir(), require.New(t) 31 must := require.NoError 32 idxPath := filepath.Join(tmpDir, "idx.tmp") 33 wr, err := NewFixedSizeBitmapsWriter(idxPath, 14, 7, log.New()) 34 require.NoError(err) 35 defer wr.Close() 36 37 must(wr.AddArray(0, []uint64{3, 9, 11})) 38 must(wr.AddArray(1, []uint64{1, 2, 3})) 39 must(wr.AddArray(2, []uint64{4, 8, 13})) 40 must(wr.AddArray(3, []uint64{1, 13})) 41 must(wr.AddArray(4, []uint64{1, 13})) 42 must(wr.AddArray(5, []uint64{1, 13})) 43 must(wr.AddArray(6, []uint64{0, 9, 13})) 44 must(wr.AddArray(7, []uint64{7})) 45 46 require.Error(wr.AddArray(8, []uint64{8})) 47 err = wr.Build() 48 require.NoError(err) 49 50 bm, err := OpenFixedSizeBitmaps(idxPath, 14) 51 require.NoError(err) 52 defer bm.Close() 53 54 at := func(item uint64) []uint64 { 55 n, err := bm.At(item) 56 require.NoError(err) 57 return n 58 } 59 60 require.Equal([]uint64{3, 9, 11}, at(0)) 61 require.Equal([]uint64{1, 2, 3}, at(1)) 62 require.Equal([]uint64{4, 8, 13}, at(2)) 63 require.Equal([]uint64{1, 13}, at(3)) 64 require.Equal([]uint64{1, 13}, at(4)) 65 require.Equal([]uint64{1, 13}, at(5)) 66 require.Equal([]uint64{0, 9, 13}, at(6)) 67 require.Equal([]uint64{7}, at(7)) 68 69 fst, snd, ok, ok2, err := bm.First2At(7, 0) 70 require.NoError(err) 71 require.Equal(uint64(7), fst) 72 require.Equal(uint64(0), snd) 73 require.Equal(true, ok) 74 require.Equal(false, ok2) 75 76 fst, snd, ok, ok2, err = bm.First2At(2, 8) 77 require.NoError(err) 78 require.Equal(uint64(8), fst) 79 require.Equal(uint64(13), snd) 80 require.Equal(true, ok) 81 require.Equal(true, ok2) 82 83 fst, snd, ok, ok2, err = bm.First2At(2, 9) 84 require.NoError(err) 85 require.Equal(uint64(13), fst) 86 require.Equal(uint64(0), snd) 87 require.Equal(true, ok) 88 require.Equal(false, ok2) 89 90 _, err = bm.At(8) 91 require.Error(err) 92 } 93 94 func TestPageAlined(t *testing.T) { 95 tmpDir, require := t.TempDir(), require.New(t) 96 idxPath := filepath.Join(tmpDir, "idx.tmp") 97 98 bm2, err := NewFixedSizeBitmapsWriter(idxPath, 128, 100, log.New()) 99 require.NoError(err) 100 require.Equal((128/8*100/os.Getpagesize()+1)*os.Getpagesize(), bm2.size) 101 defer bm2.Close() 102 bm2.Close() 103 104 bm3, err := NewFixedSizeBitmapsWriter(idxPath, 128, 1000, log.New()) 105 require.NoError(err) 106 require.Equal((128/8*1000/os.Getpagesize()+1)*os.Getpagesize(), bm3.size) 107 defer bm3.Close() 108 }