Browse Source

Move building fieldNames set to class constructor (#131722)

This patch moves building the set of fieldName prefixes in the
FallbackSyntheticSourceBlockLoader to the constructor. This set does not
change between invocations (since the fieldName is final), so we can
just do the work once when constructing the BlockLoader
instead of per-document.
Jordan Powers 2 months ago
parent
commit
a76f56b040

+ 22 - 13
server/src/main/java/org/elasticsearch/index/mapper/FallbackSyntheticSourceBlockLoader.java

@@ -70,19 +70,16 @@ public abstract class FallbackSyntheticSourceBlockLoader implements BlockLoader
         throw new UnsupportedOperationException();
     }
 
-    private record IgnoredSourceRowStrideReader<T>(String fieldName, Reader<T> reader) implements RowStrideReader {
-        @Override
-        public void read(int docId, StoredFields storedFields, Builder builder) throws IOException {
-            var ignoredSource = storedFields.storedFields().get(IgnoredSourceFieldMapper.NAME);
-            if (ignoredSource == null) {
-                builder.appendNull();
-                return;
-            }
-
-            Map<String, List<IgnoredSourceFieldMapper.NameValue>> valuesForFieldAndParents = new HashMap<>();
-
-            // Contains name of the field and all its parents
-            Set<String> fieldNames = new HashSet<>() {
+    private static class IgnoredSourceRowStrideReader<T> implements RowStrideReader {
+        // Contains name of the field and all its parents
+        private final Set<String> fieldNames;
+        private final String fieldName;
+        private final Reader<T> reader;
+
+        IgnoredSourceRowStrideReader(String fieldName, Reader<T> reader) {
+            this.fieldName = fieldName;
+            this.reader = reader;
+            this.fieldNames = new HashSet<>() {
                 {
                     add("_doc");
                 }
@@ -97,6 +94,18 @@ public abstract class FallbackSyntheticSourceBlockLoader implements BlockLoader
                 fieldNames.add(current.toString());
             }
 
+        }
+
+        @Override
+        public void read(int docId, StoredFields storedFields, Builder builder) throws IOException {
+            var ignoredSource = storedFields.storedFields().get(IgnoredSourceFieldMapper.NAME);
+            if (ignoredSource == null) {
+                builder.appendNull();
+                return;
+            }
+
+            Map<String, List<IgnoredSourceFieldMapper.NameValue>> valuesForFieldAndParents = new HashMap<>();
+
             for (Object value : ignoredSource) {
                 IgnoredSourceFieldMapper.NameValue nameValue = IgnoredSourceFieldMapper.decode(value);
                 if (fieldNames.contains(nameValue.name())) {