@@ -692,15 +692,15 @@ func (it *bigEndianPostings) Err() error {
692692}
693693
694694type prefixCompressedPostings struct {
695- bs []byte
696- cur uint64
697- inside bool
698- idx int // The current offset inside the bs.
699- footerAddr int
700- key uint64
701- numBlock int
702- blockIdx int // The current block idx.
703- nextBlock int // The starting offset of the next block.
695+ bs []byte
696+ cur uint64
697+ initialized bool
698+ idx int // The current offset inside the bs.
699+ footerAddr int
700+ key uint64
701+ numBlock int
702+ blockIdx int // The current block idx.
703+ nextBlock int // The starting offset of the next block.
704704}
705705
706706func newPrefixCompressedPostings (bstream []byte ) * prefixCompressedPostings {
@@ -716,7 +716,7 @@ func (it *prefixCompressedPostings) At() uint64 {
716716}
717717
718718func (it * prefixCompressedPostings ) Next () bool {
719- if it .inside { // Already entered the block.
719+ if it .initialized { // Already entered the block.
720720 if it .idx < it .nextBlock {
721721 it .cur = it .key | uint64 (binary .BigEndian .Uint16 (it .bs [it .idx :]))
722722 it .idx += 2
@@ -728,7 +728,7 @@ func (it *prefixCompressedPostings) Next() bool {
728728 if it .idx < it .footerAddr {
729729 it .key = binary .BigEndian .Uint64 (it .bs [it .idx :])
730730 it .idx += 8
731- it .inside = true
731+ it .initialized = true
732732 it .nextBlock = int (binary .BigEndian .Uint32 (it .bs [it .footerAddr + ((it .blockIdx + 1 )<< 2 ):]))
733733 it .cur = it .key | uint64 (binary .BigEndian .Uint16 (it .bs [it .idx :]))
734734 it .idx += 2
@@ -744,55 +744,60 @@ func (it *prefixCompressedPostings) seekInBlock(x uint64) bool {
744744 j := sort .Search (num , func (i int ) bool {
745745 return binary .BigEndian .Uint16 (it .bs [it .idx + (i << 1 ):]) >= curVal
746746 })
747- if j == num {
748- // Fast-path to the next block.
749- // The first element in next block should be >= x.
750- it .idx = it .nextBlock
751- it .blockIdx += 1
752- if it .idx >= it .footerAddr {
753- return false
754- }
755- it .key = binary .BigEndian .Uint64 (it .bs [it .idx :])
756- it .idx += 8
757- it .inside = true
758- it .nextBlock = int (binary .BigEndian .Uint32 (it .bs [it .footerAddr + ((it .blockIdx + 1 )<< 2 ):]))
759- it .cur = it .key | uint64 (binary .BigEndian .Uint16 (it .bs [it .idx :]))
760- it .idx += 2
747+ if j < num {
748+ it .cur = it .key | uint64 (binary .BigEndian .Uint16 (it .bs [it .idx + (j << 1 ):]))
749+ it .idx += (j + 1 ) << 1
761750 return true
762751 }
763- it .cur = it .key | uint64 (binary .BigEndian .Uint16 (it .bs [it .idx + (j << 1 ):]))
764- it .idx += (j + 1 ) << 1
752+ // Fast-path to the next block.
753+ // The first element in next block should be >= x.
754+ it .idx = it .nextBlock
755+ it .blockIdx += 1
756+ if it .idx >= it .footerAddr {
757+ return false
758+ }
759+ it .key = binary .BigEndian .Uint64 (it .bs [it .idx :])
760+ it .idx += 8
761+ it .initialized = true
762+ it .nextBlock = int (binary .BigEndian .Uint32 (it .bs [it .footerAddr + ((it .blockIdx + 1 )<< 2 ):]))
763+ it .cur = it .key | uint64 (binary .BigEndian .Uint16 (it .bs [it .idx :]))
764+ it .idx += 2
765765 return true
766766}
767767
768768func (it * prefixCompressedPostings ) Seek (x uint64 ) bool {
769769 if it .cur >= x {
770770 return true
771771 }
772- curKey := ( x >> 16 ) << 16
773- if it .inside && it .key == curKey {
772+ curKey := x & 0xffffffffffff0000
773+ if it .initialized && it .key == curKey {
774774 // Fast path for x in current block.
775775 return it .seekInBlock (x )
776- } else {
777- i := sort .Search (it .numBlock - it .blockIdx , func (i int ) bool {
778- off := int (binary .BigEndian .Uint32 (it .bs [it .footerAddr + ((it .blockIdx + i )<< 2 ):]))
779- k := binary .BigEndian .Uint64 (it .bs [off :])
780- return k >= curKey
781- })
782- if i == it .numBlock - it .blockIdx {
783- return false
784- }
785- it . blockIdx += i
786- if i > 0 {
787- it . idx = int ( binary . BigEndian . Uint32 ( it . bs [ it . footerAddr + (( it . blockIdx ) << 2 ):]))
788- }
776+ }
777+ i := sort .Search (it .numBlock - it .blockIdx , func (i int ) bool {
778+ off := int (binary .BigEndian .Uint32 (it .bs [it .footerAddr + ((it .blockIdx + i )<< 2 ):]))
779+ k := binary .BigEndian .Uint64 (it .bs [off :])
780+ return k >= curKey
781+ })
782+ if i == it .numBlock - it .blockIdx {
783+ it . idx = it . footerAddr
784+ return false
785+ }
786+ it . blockIdx += i
787+ if i > 0 {
788+ it . idx = int ( binary . BigEndian . Uint32 ( it . bs [ it . footerAddr + (( it . blockIdx ) << 2 ):]))
789789 }
790790 it .key = binary .BigEndian .Uint64 (it .bs [it .idx :])
791791 it .idx += 8
792792
793- it .inside = true
793+ it .initialized = true
794794
795795 it .nextBlock = int (binary .BigEndian .Uint32 (it .bs [it .footerAddr + ((it .blockIdx + 1 )<< 2 ):]))
796+ if it .key != curKey {
797+ it .cur = it .key | uint64 (binary .BigEndian .Uint16 (it .bs [it .idx :]))
798+ it .idx += 2
799+ return true
800+ }
796801 return it .seekInBlock (x )
797802}
798803
@@ -834,7 +839,6 @@ func writePrefixCompressedPostings(e *encoding.Encbuf, arr []uint64) {
834839 if curKey != key {
835840 // Move to next block.
836841 if idx != 0 {
837- // We don't need to store the starting offset of the first block because it won't be used.
838842 startingOffs = append (startingOffs , uint32 (len (e .B )))
839843 writePrefixCompressedPostingsBlock (e , vals , key , c )
840844 vals = vals [:0 ]
0 commit comments