Skip to content

Commit 251a98a

Browse files
committed
bug fix and refactoring wrt bytes written stat computation
1 parent 5dd118e commit 251a98a

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

contentcoder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func newChunkedContentCoder(chunkSize uint64, maxDocNum uint64,
8181
// and re used. You cannot change the chunk size.
8282
func (c *chunkedContentCoder) Reset() {
8383
c.currChunk = 0
84+
c.bytesWritten = 0
8485
c.final = c.final[:0]
8586
c.chunkBuf.Reset()
8687
c.chunkMetaBuf.Reset()

intcoder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func newChunkedIntCoder(chunkSize uint64, maxDocNum uint64) *chunkedIntCoder {
5858
// from previous use. you cannot change the chunk size or max doc num.
5959
func (c *chunkedIntCoder) Reset() {
6060
c.final = c.final[:0]
61+
c.bytesWritten = 0
6162
c.chunkBuf.Reset()
6263
c.currChunk = 0
6364
for i := range c.chunkLens {
@@ -106,7 +107,6 @@ func (c *chunkedIntCoder) Add(docNum uint64, vals ...uint64) error {
106107
if err != nil {
107108
return err
108109
}
109-
c.incrementBytesWritten(uint64(wb))
110110
}
111111

112112
return nil
@@ -129,6 +129,7 @@ func (c *chunkedIntCoder) AddBytes(docNum uint64, buf []byte) error {
129129
// to be encoded.
130130
func (c *chunkedIntCoder) Close() {
131131
encodingBytes := c.chunkBuf.Bytes()
132+
c.incrementBytesWritten(uint64(len(encodingBytes)))
132133
c.chunkLens[c.currChunk] = uint64(len(encodingBytes))
133134
c.final = append(c.final, encodingBytes...)
134135
c.currChunk = uint64(cap(c.chunkLens)) // sentinel to detect double close

new.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,13 @@ func (*ZapPlugin) newWithChunkMode(results []index.Document,
7878
s.FieldsMap, s.FieldsInv, uint64(len(results)),
7979
storedIndexOffset, fieldsIndexOffset, fdvIndexOffset, dictOffsets)
8080

81+
// get the bytes written before the interim's reset() call
82+
// write it to the newly formed segment base.
83+
totalBytesWritten := s.getBytesWritten()
8184
if err == nil && s.reset() == nil {
8285
s.lastNumDocs = len(results)
8386
s.lastOutSize = len(br.Bytes())
84-
sb.setBytesWritten(s.getBytesWritten())
87+
sb.setBytesWritten(totalBytesWritten)
8588
interimPool.Put(s)
8689
}
8790

@@ -192,6 +195,10 @@ func (s *interim) reset() (err error) {
192195
s.lastNumDocs = 0
193196
s.lastOutSize = 0
194197

198+
// reset the bytes written stat count
199+
// to avoid leaking of bytesWritten across reuse cycles.
200+
s.setBytesWritten(0)
201+
195202
return err
196203
}
197204

@@ -699,7 +706,6 @@ func (s *interim) writeDicts() (fdvIndexOffset uint64, dictOffsets []uint64, err
699706
if err != nil {
700707
return 0, nil, err
701708
}
702-
prevBytesWritten := locEncoder.getBytesWritten()
703709
for _, loc := range locs[locOffset : locOffset+freqNorm.numLocs] {
704710
err = locEncoder.Add(docNum,
705711
uint64(loc.fieldID), loc.pos, loc.start, loc.end,
@@ -713,9 +719,6 @@ func (s *interim) writeDicts() (fdvIndexOffset uint64, dictOffsets []uint64, err
713719
return 0, nil, err
714720
}
715721
}
716-
if locEncoder.getBytesWritten()-prevBytesWritten > 0 {
717-
s.incrementBytesWritten(locEncoder.getBytesWritten() - prevBytesWritten)
718-
}
719722
locOffset += freqNorm.numLocs
720723
}
721724

@@ -728,6 +731,7 @@ func (s *interim) writeDicts() (fdvIndexOffset uint64, dictOffsets []uint64, err
728731

729732
tfEncoder.Close()
730733
locEncoder.Close()
734+
s.incrementBytesWritten(locEncoder.getBytesWritten())
731735

732736
postingsOffset, err :=
733737
writePostings(postingsBS, tfEncoder, locEncoder, nil, s.w, buf)
@@ -801,7 +805,7 @@ func (s *interim) writeDicts() (fdvIndexOffset uint64, dictOffsets []uint64, err
801805
return 0, nil, err
802806
}
803807

804-
s.setBytesWritten(s.getBytesWritten())
808+
s.incrementBytesWritten(fdvEncoder.getBytesWritten())
805809

806810
fdvOffsetsStart[fieldID] = uint64(s.w.Count())
807811

0 commit comments

Comments
 (0)