@@ -695,7 +695,7 @@ func (it *bigEndianPostings) Err() error {
695695}
696696
697697// 1 is bigEndian, 2 is baseDelta, 3 is deltaBlock, 4 is baseDeltaBlock, 5 is bitmapPostings, 6 is roaringBitmapPostings.
698- const postingsType = 4
698+ const postingsType = 2
699699
700700type bitSlice struct {
701701 bstream []byte
@@ -763,30 +763,25 @@ func newBaseDeltaPostings(bstream []byte, base uint64, width int, size int) *bas
763763 return & baseDeltaPostings {bs : bstream , width : width , base : base , size : size , cur : uint64 (base ), mask : (uint64 (1 ) << (uint64 (width ) << 3 )) - 1 , prel : 8 - width }
764764}
765765
766- func (it * baseDeltaPostings ) readBytes (off int ) int {
767- val := 0
768- for i := 0 ; i < it .width ; i ++ {
769- val = (val << 8 ) | int (it .bs [off + i ])
770- }
771- return val
772- }
773-
774766func (it * baseDeltaPostings ) At () uint64 {
775767 return it .cur
776768}
777769
778770func (it * baseDeltaPostings ) Next () bool {
779- if it .idx < it .size * it .width {
780- if it .idx - it .prel < 0 {
781- it .cur = uint64 (it .readBytes (it .idx )) + it .base
782- it .idx += it .width
783- return true
784- }
771+ if it .idx >= it .size * it .width {
772+ return false
773+ }
774+ if it .idx - it .prel >= 0 {
785775 it .cur = binary .BigEndian .Uint64 (it .bs [it .idx - it .prel :])& it .mask + it .base
786- it .idx += it .width
787- return true
776+ } else {
777+ it .cur = 0
778+ for i := 0 ; i < it .width ; i ++ {
779+ it .cur = (it .cur << 8 ) | uint64 (it .bs [it .idx + i ])
780+ }
781+ it .cur += it .base
788782 }
789- return false
783+ it .idx += it .width
784+ return true
790785}
791786
792787func (it * baseDeltaPostings ) Seek (x uint64 ) bool {
@@ -984,7 +979,7 @@ type baseDeltaBlockPostings struct {
984979 offset int // offset in bit.
985980 cur uint64
986981 base uint64
987- mask uint32
982+ mask uint64
988983 prel int
989984}
990985
@@ -1010,14 +1005,23 @@ func (it *baseDeltaBlockPostings) Next() bool {
10101005 it .count = int (val )
10111006 it .offset += n
10121007 it .bs .width = int (it .bs .bstream [it .offset ])
1013- it .mask = (uint32 (1 ) << uint (8 * it .bs .width )) - 1
1014- it .prel = 4 - it .bs .width
1008+ it .mask = (uint64 (1 ) << uint (8 * it .bs .width )) - 1
1009+ it .prel = 8 - it .bs .width
10151010 it .offset += 1
10161011 it .idxBlock = 1
10171012 return true
10181013 }
10191014
1020- it .cur = uint64 (binary .BigEndian .Uint32 (it .bs .bstream [it .offset - it .prel :])& it .mask ) + it .base
1015+ if it .offset - it .prel >= 0 {
1016+ it .cur = binary .BigEndian .Uint64 (it .bs .bstream [it .offset - it .prel :])& it .mask + it .base
1017+ } else {
1018+ it .cur = 0
1019+ for i := 0 ; i < it .bs .width ; i ++ {
1020+ it .cur = (it .cur << 8 ) | uint64 (it .bs .bstream [it .offset + i ])
1021+ }
1022+ it .cur += it .base
1023+ }
1024+ // it.cur = (binary.BigEndian.Uint64(it.bs.bstream[it.offset-it.prel:])&it.mask) + it.base
10211025 it .offset += it .bs .width
10221026 it .idxBlock += 1
10231027 if it .idxBlock == it .count {
@@ -1060,20 +1064,20 @@ func (it *baseDeltaBlockPostings) Seek(x uint64) bool {
10601064 it .count = int (val )
10611065 it .offset += n
10621066 it .bs .width = int (it .bs .bstream [it .offset ])
1063- it .mask = (uint32 (1 ) << uint (8 * it .bs .width )) - 1
1064- it .prel = 4 - it .bs .width
1067+ it .mask = (uint64 (1 ) << uint (8 * it .bs .width )) - 1
1068+ it .prel = 8 - it .bs .width
10651069 it .offset += 1
10661070 it .idxBlock = 1
10671071 if x <= it .base {
10681072 return true
10691073 } else {
10701074 temp := x - it .base
10711075 j := sort .Search (it .count - it .idxBlock , func (i int ) bool {
1072- return uint64 (binary .BigEndian .Uint32 (it .bs .bstream [it .offset + i * it .bs .width - it .prel :])& it .mask ) >= temp
1076+ return (binary .BigEndian .Uint64 (it .bs .bstream [it .offset + i * it .bs .width - it .prel :])& it .mask ) >= temp
10731077 })
10741078 if j < it .count - it .idxBlock {
10751079 it .offset += j * it .bs .width
1076- it .cur = uint64 (binary .BigEndian .Uint32 (it .bs .bstream [it .offset - it .prel :])& it .mask ) + it .base
1080+ it .cur = (binary .BigEndian .Uint64 (it .bs .bstream [it .offset - it .prel :])& it .mask ) + it .base
10771081 it .idxBlock += j + 1
10781082 if it .idxBlock == it .count {
10791083 // it.offset = startOff + ((i+1)<<deltaBlockBits)
@@ -1103,8 +1107,8 @@ func (it *baseDeltaBlockPostings) Seek(x uint64) bool {
11031107 it .count = int (val )
11041108 it .offset += n
11051109 it .bs .width = int (it .bs .bstream [it .offset ])
1106- it .mask = (uint32 (1 ) << uint (8 * it .bs .width )) - 1
1107- it .prel = 4 - it .bs .width
1110+ it .mask = (uint64 (1 ) << uint (8 * it .bs .width )) - 1
1111+ it .prel = 8 - it .bs .width
11081112 it .offset += 1
11091113 it .idxBlock = 1
11101114 }
@@ -1113,11 +1117,11 @@ func (it *baseDeltaBlockPostings) Seek(x uint64) bool {
11131117 } else {
11141118 temp := x - it .base
11151119 j := sort .Search (it .count - it .idxBlock , func (i int ) bool {
1116- return uint64 (binary .BigEndian .Uint32 (it .bs .bstream [it .offset + i * it .bs .width - it .prel :])& it .mask ) >= temp
1120+ return (binary .BigEndian .Uint64 (it .bs .bstream [it .offset + i * it .bs .width - it .prel :])& it .mask ) >= temp
11171121 })
11181122 if j < it .count - it .idxBlock {
11191123 it .offset += j * it .bs .width
1120- it .cur = uint64 (binary .BigEndian .Uint32 (it .bs .bstream [it .offset - it .prel :])& it .mask ) + it .base
1124+ it .cur = (binary .BigEndian .Uint64 (it .bs .bstream [it .offset - it .prel :])& it .mask ) + it .base
11211125 it .idxBlock += j + 1
11221126 if it .idxBlock == it .count {
11231127 // it.offset = startOff + deltaBlockSize
@@ -1172,7 +1176,56 @@ func writeBaseDeltaBlockPostings(e *encoding.Encbuf, arr []uint32) {
11721176 remaining -= ((bits .Len (uint (len (deltas ))) >> 3 ) + 1 )
11731177 for _ , delta := range deltas {
11741178 for j := max - 1 ; j >= 0 ; j -- {
1175- e .B = append (e .B , byte ((delta >> (8 * uint (j )) & 0xff )))
1179+ e .B = append (e .B , byte ((delta >> (uint (j ) << 3 ) & 0xff )))
1180+ }
1181+ remaining -= max
1182+ }
1183+
1184+ if i == len (arr ) {
1185+ break
1186+ }
1187+
1188+ for remaining > 0 {
1189+ e .PutByte (0 )
1190+ remaining -= 1
1191+ }
1192+ }
1193+ }
1194+
1195+ func writeBaseDeltaBlockPostings64 (e * encoding.Encbuf , arr []uint64 ) {
1196+ i := 0
1197+ startLen := len (e .B )
1198+ deltas := []uint64 {}
1199+ var remaining int
1200+ var base uint64
1201+ var max int
1202+ for i < len (arr ) {
1203+ e .PutUvarint64 (arr [i ]) // Put base.
1204+ remaining = deltaBlockSize - (len (e .B )- startLen )% deltaBlockSize - 1
1205+ deltas = deltas [:0 ]
1206+ base = arr [i ]
1207+ max = - 1
1208+ i += 1
1209+ for i < len (arr ) {
1210+ delta := arr [i ] - base
1211+ cur := (bits .Len64 (delta ) + 7 ) >> 3
1212+ if cur == 0 {
1213+ cur = 1
1214+ }
1215+ if remaining - cur * (len (deltas )+ 1 )- ((bits .Len (uint (len (deltas )))>> 3 )+ 1 ) >= 0 {
1216+ deltas = append (deltas , delta )
1217+ max = cur
1218+ } else {
1219+ break
1220+ }
1221+ i += 1
1222+ }
1223+ e .PutUvarint64 (uint64 (len (deltas ) + 1 ))
1224+ e .PutByte (byte (max ))
1225+ remaining -= ((bits .Len (uint (len (deltas ))) >> 3 ) + 1 )
1226+ for _ , delta := range deltas {
1227+ for j := max - 1 ; j >= 0 ; j -- {
1228+ e .B = append (e .B , byte ((delta >> (uint (j ) << 3 ) & 0xff )))
11761229 }
11771230 remaining -= max
11781231 }
0 commit comments