1515import org .apache .lucene .codecs .FilterCodec ;
1616import org .apache .lucene .codecs .NormsProducer ;
1717import org .apache .lucene .codecs .PostingsFormat ;
18+ import org .apache .lucene .index .FieldInfo ;
1819import org .apache .lucene .index .FieldInfos ;
1920import org .apache .lucene .index .Fields ;
2021import org .apache .lucene .index .FilterLeafReader ;
2122import org .apache .lucene .index .SegmentReadState ;
2223import org .apache .lucene .index .SegmentWriteState ;
2324import org .apache .lucene .index .Terms ;
2425import org .apache .lucene .index .TermsEnum ;
25- import org .apache .lucene .internal .hppc .IntIntHashMap ;
2626import org .apache .lucene .util .BytesRef ;
2727import org .elasticsearch .common .util .FeatureFlag ;
2828
@@ -63,22 +63,18 @@ public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException
6363 static final class TrackingLengthFieldsConsumer extends FieldsConsumer {
6464 final SegmentWriteState state ;
6565 final FieldsConsumer in ;
66- final IntIntHashMap termsBytesPerField ;
66+ final long [] totalBytes ;
6767
6868 TrackingLengthFieldsConsumer (SegmentWriteState state , FieldsConsumer in ) {
6969 this .state = state ;
7070 this .in = in ;
71- this .termsBytesPerField = new IntIntHashMap ( state . fieldInfos . size ()) ;
71+ this .totalBytes = new long [ 1 ] ;
7272 }
7373
7474 @ Override
7575 public void write (Fields fields , NormsProducer norms ) throws IOException {
76- in .write (new TrackingLengthFields (fields , termsBytesPerField , state .fieldInfos ), norms );
77- long totalBytes = 0 ;
78- for (int bytes : termsBytesPerField .values ) {
79- totalBytes += bytes ;
80- }
81- state .segmentInfo .putAttribute (IN_MEMORY_POSTINGS_BYTES_KEY , Long .toString (totalBytes ));
76+ in .write (new TrackingLengthFields (fields , totalBytes , state .fieldInfos ), norms );
77+ state .segmentInfo .putAttribute (IN_MEMORY_POSTINGS_BYTES_KEY , Long .toString (totalBytes [0 ]));
8278 }
8379
8480 @ Override
@@ -88,12 +84,18 @@ public void close() throws IOException {
8884 }
8985
9086 static final class TrackingLengthFields extends FilterLeafReader .FilterFields {
91- final IntIntHashMap termsBytesPerField ;
87+ final long [] totalBytes ;
88+ final boolean [] seenFields ;
9289 final FieldInfos fieldInfos ;
9390
94- TrackingLengthFields (Fields in , IntIntHashMap termsBytesPerField , FieldInfos fieldInfos ) {
91+ TrackingLengthFields (Fields in , long [] totalBytes , FieldInfos fieldInfos ) {
9592 super (in );
96- this .termsBytesPerField = termsBytesPerField ;
93+ this .totalBytes = totalBytes ;
94+ int maxFieldNumber = 0 ;
95+ for (FieldInfo info : fieldInfos ) {
96+ maxFieldNumber = Math .max (info .getFieldNumber (), maxFieldNumber );
97+ }
98+ this .seenFields = new boolean [maxFieldNumber + 1 ];
9799 this .fieldInfos = fieldInfos ;
98100 }
99101
@@ -104,10 +106,12 @@ public Terms terms(String field) throws IOException {
104106 return null ;
105107 }
106108 int fieldNum = fieldInfos .fieldInfo (field ).number ;
107- return new TrackingLengthTerms (
108- terms ,
109- bytes -> termsBytesPerField .put (fieldNum , Math .max (termsBytesPerField .getOrDefault (fieldNum , 0 ), bytes ))
110- );
109+ return new TrackingLengthTerms (terms , bytes -> {
110+ if (seenFields [fieldNum ] == false ) {
111+ totalBytes [0 ] += bytes ;
112+ seenFields [fieldNum ] = true ;
113+ }
114+ });
111115 }
112116 }
113117
0 commit comments