@@ -945,6 +945,80 @@ func TestBaseDeltaBlockPostings(t *testing.T) {
945945 })
946946}
947947
948+ func TestBitmapPostings (t * testing.T ) {
949+ num := 1000
950+ // mock a list as postings
951+ ls := make ([]uint32 , num )
952+ ls [0 ] = 2
953+ for i := 1 ; i < num ; i ++ {
954+ ls [i ] = ls [i - 1 ] + uint32 (rand .Int31n (25 )) + 2
955+ // ls[i] = ls[i-1] + 2
956+ }
957+
958+ buf := encoding.Encbuf {}
959+ writeBitmapPostings (& buf , ls )
960+ // t.Log("len", len(buf.Get()))
961+
962+ t .Run ("Iteration" , func (t * testing.T ) {
963+ bp := newBitmapPostings (buf .Get ())
964+ for i := 0 ; i < num ; i ++ {
965+ testutil .Assert (t , bp .Next () == true , "" )
966+ // t.Log("ls[i] =", ls[i], "bp.At() =", bp.At())
967+ testutil .Equals (t , uint64 (ls [i ]), bp .At ())
968+ }
969+
970+ testutil .Assert (t , bp .Next () == false , "" )
971+ testutil .Assert (t , bp .Err () == nil , "" )
972+ })
973+
974+ t .Run ("Seek" , func (t * testing.T ) {
975+ table := []struct {
976+ seek uint32
977+ val uint32
978+ found bool
979+ }{
980+ {
981+ ls [0 ] - 1 , ls [0 ], true ,
982+ },
983+ {
984+ ls [4 ], ls [4 ], true ,
985+ },
986+ {
987+ ls [500 ] - 1 , ls [500 ], true ,
988+ },
989+ {
990+ ls [600 ] + 1 , ls [601 ], true ,
991+ },
992+ {
993+ ls [600 ] + 1 , ls [601 ], true ,
994+ },
995+ {
996+ ls [600 ] + 1 , ls [601 ], true ,
997+ },
998+ {
999+ ls [0 ], ls [601 ], true ,
1000+ },
1001+ {
1002+ ls [600 ], ls [601 ], true ,
1003+ },
1004+ {
1005+ ls [999 ], ls [999 ], true ,
1006+ },
1007+ {
1008+ ls [999 ] + 10 , ls [999 ], false ,
1009+ },
1010+ }
1011+
1012+ bp := newBitmapPostings (buf .Get ())
1013+
1014+ for _ , v := range table {
1015+ testutil .Equals (t , v .found , bp .Seek (uint64 (v .seek )))
1016+ testutil .Equals (t , uint64 (v .val ), bp .At ())
1017+ testutil .Assert (t , bp .Err () == nil , "" )
1018+ }
1019+ })
1020+ }
1021+
9481022func BenchmarkPostings (b * testing.B ) {
9491023 num := 100000
9501024 // mock a list as postings
@@ -977,6 +1051,11 @@ func BenchmarkPostings(b *testing.B) {
9771051 writeBaseDeltaBlockPostings (& bufBDB , ls )
9781052 // b.Log(len(bufBDB.Get()))
9791053
1054+ // bitmapPostings.
1055+ bufBM := encoding.Encbuf {}
1056+ writeBitmapPostings (& bufBM , ls )
1057+ // b.Log("bitmapPostings size", bitmapBits, "bits =", len(bufBM.Get()))
1058+
9801059 table := []struct {
9811060 seek uint32
9821061 val uint32
@@ -1070,6 +1149,20 @@ func BenchmarkPostings(b *testing.B) {
10701149 testutil .Assert (bench , bdbp .Err () == nil , "" )
10711150 }
10721151 })
1152+ b .Run ("bitmapPostingsIteration" , func (bench * testing.B ) {
1153+ bench .ResetTimer ()
1154+ bench .ReportAllocs ()
1155+ for j := 0 ; j < bench .N ; j ++ {
1156+ bm := newBitmapPostings (bufBM .Get ())
1157+
1158+ for i := 0 ; i < num ; i ++ {
1159+ testutil .Assert (bench , bm .Next () == true , "" )
1160+ testutil .Equals (bench , uint64 (ls [i ]), bm .At ())
1161+ }
1162+ testutil .Assert (bench , bm .Next () == false , "" )
1163+ testutil .Assert (bench , bm .Err () == nil , "" )
1164+ }
1165+ })
10731166
10741167 b .Run ("bigEndianSeek" , func (bench * testing.B ) {
10751168 bench .ResetTimer ()
@@ -1123,6 +1216,19 @@ func BenchmarkPostings(b *testing.B) {
11231216 }
11241217 }
11251218 })
1219+ b .Run ("bitmapPostingsSeek" , func (bench * testing.B ) {
1220+ bench .ResetTimer ()
1221+ bench .ReportAllocs ()
1222+ for j := 0 ; j < bench .N ; j ++ {
1223+ bm := newBitmapPostings (bufBM .Get ())
1224+
1225+ for _ , v := range table {
1226+ testutil .Equals (bench , v .found , bm .Seek (uint64 (v .seek )))
1227+ testutil .Equals (bench , uint64 (v .val ), bm .At ())
1228+ testutil .Assert (bench , bm .Err () == nil , "" )
1229+ }
1230+ }
1231+ })
11261232}
11271233
11281234func TestIntersectWithMerge (t * testing.T ) {
0 commit comments