@@ -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