2222import org .apache .lucene .index .SegmentWriteState ;
2323import org .apache .lucene .index .Terms ;
2424import org .apache .lucene .index .TermsEnum ;
25- import org .apache .lucene .internal .hppc .IntIntHashMap ;
25+ import org .apache .lucene .internal .hppc .IntHashSet ;
2626import org .apache .lucene .util .BytesRef ;
2727import org .elasticsearch .common .util .FeatureFlag ;
2828
@@ -63,22 +63,20 @@ 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 IntHashSet seenFields ;
67+ final long [] totalBytes ;
6768
6869 TrackingLengthFieldsConsumer (SegmentWriteState state , FieldsConsumer in ) {
6970 this .state = state ;
7071 this .in = in ;
71- this .termsBytesPerField = new IntIntHashMap (state .fieldInfos .size ());
72+ this .totalBytes = new long [1 ];
73+ this .seenFields = new IntHashSet (state .fieldInfos .size ());
7274 }
7375
7476 @ Override
7577 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 ));
78+ in .write (new TrackingLengthFields (fields , state .fieldInfos , seenFields , totalBytes ), norms );
79+ state .segmentInfo .putAttribute (IN_MEMORY_POSTINGS_BYTES_KEY , Long .toString (totalBytes [0 ]));
8280 }
8381
8482 @ Override
@@ -88,13 +86,15 @@ public void close() throws IOException {
8886 }
8987
9088 static final class TrackingLengthFields extends FilterLeafReader .FilterFields {
91- final IntIntHashMap termsBytesPerField ;
9289 final FieldInfos fieldInfos ;
90+ final IntHashSet seenFields ;
91+ final long [] totalBytes ;
9392
94- TrackingLengthFields (Fields in , IntIntHashMap termsBytesPerField , FieldInfos fieldInfos ) {
93+ TrackingLengthFields (Fields in , FieldInfos fieldInfos , IntHashSet seenFields , long [] totalBytes ) {
9594 super (in );
96- this .termsBytesPerField = termsBytesPerField ;
95+ this .seenFields = seenFields ;
9796 this .fieldInfos = fieldInfos ;
97+ this .totalBytes = totalBytes ;
9898 }
9999
100100 @ Override
@@ -104,10 +104,11 @@ public Terms terms(String field) throws IOException {
104104 return null ;
105105 }
106106 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- );
107+ if (seenFields .add (fieldNum )) {
108+ return new TrackingLengthTerms (terms , bytes -> totalBytes [0 ] += bytes );
109+ } else {
110+ return terms ;
111+ }
111112 }
112113 }
113114
0 commit comments