Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new segments now support 1-hit encoding #1337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion index/scorch/segment/zap/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,15 @@ func (s *interim) writeDicts() (fdvIndexOffset uint64, dictOffsets []uint64, err
locs := s.Locs[pid]
locOffset := 0

var lastDocNum, lastFreq, lastNorm uint64
postingsItr := postingsBS.Iterator()
for postingsItr.HasNext() {
docNum := uint64(postingsItr.Next())
lastDocNum = docNum

freqNorm := freqNorms[freqNormOffset]
lastFreq = freqNorm.freq
lastNorm = uint64(math.Float32bits(freqNorm.norm))

err = tfEncoder.Add(docNum,
encodeFreqHasLocs(freqNorm.freq, freqNorm.numLocs > 0),
Expand Down Expand Up @@ -699,8 +703,21 @@ func (s *interim) writeDicts() (fdvIndexOffset uint64, dictOffsets []uint64, err
tfEncoder.Close()
locEncoder.Close()

// determines whether to use "1-hit" encoding optimization
// when a term appears in only 1 doc, with no loc info,
// has freq of 1, and the docNum fits into 31-bits
use1HitEncoding := func(termCardinality uint64) (bool, uint64, uint64) {
if termCardinality == uint64(1) && locEncoder.FinalSize() <= 0 {
docNum := uint64(postingsBS.Minimum())
if under32Bits(docNum) && docNum == lastDocNum && lastFreq == 1 {
return true, docNum, lastNorm
}
}
return false, 0, 0
}

postingsOffset, err :=
writePostings(postingsBS, tfEncoder, locEncoder, nil, s.w, buf)
writePostings(postingsBS, tfEncoder, locEncoder, use1HitEncoding, s.w, buf)
if err != nil {
return 0, nil, err
}
Expand All @@ -714,6 +731,9 @@ func (s *interim) writeDicts() (fdvIndexOffset uint64, dictOffsets []uint64, err

tfEncoder.Reset()
locEncoder.Reset()
lastDocNum = 0
lastFreq = 0
lastNorm = 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually these are either in the wrong place, or not necessary in this verison, as these variables go out of scope here...

}

err = s.builder.Close()
Expand Down