Skip to content

Commit 0f93844

Browse files
committed
Replace int hashmap by a counter in TrackingPostingsInMemoryBytesCodec
and use int hashset to keep track of seen fields.
1 parent 2864dd8 commit 0f93844

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

server/src/main/java/org/elasticsearch/index/codec/TrackingPostingsInMemoryBytesCodec.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.apache.lucene.index.SegmentWriteState;
2323
import org.apache.lucene.index.Terms;
2424
import org.apache.lucene.index.TermsEnum;
25-
import org.apache.lucene.internal.hppc.IntIntHashMap;
25+
import org.apache.lucene.internal.hppc.IntHashSet;
2626
import org.apache.lucene.util.BytesRef;
2727
import 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

Comments
 (0)