Skip to content

Commit 3205d1f

Browse files
authored
Simplify FieldLookup (#94539)
FieldLookup is used to cache values loaded from stored fields for scripts or value fetchers. It still has leftover code from when it needed to support multiple data types, and has different caches for single or multiple values, all of which are now unnecessary. This PR removes all the extra indirection in favour of a simple object list cache.
1 parent 0d1a4ba commit 3205d1f

File tree

2 files changed

+22
-47
lines changed

2 files changed

+22
-47
lines changed

server/src/main/java/org/elasticsearch/search/lookup/FieldLookup.java

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,11 @@
1111

1212
import java.util.ArrayList;
1313
import java.util.List;
14-
import java.util.Map;
1514

1615
public class FieldLookup {
1716

18-
// we can cached fieldType completely per name, since its on an index/shard level (the lookup, and it does not change within the scope
19-
// of a search request)
2017
private final MappedFieldType fieldType;
21-
22-
private Map<String, List<Object>> fields;
23-
24-
private Object value;
25-
26-
private boolean valueLoaded = false;
27-
28-
private List<Object> values = new ArrayList<>();
29-
18+
private final List<Object> values = new ArrayList<>();
3019
private boolean valuesLoaded = false;
3120

3221
FieldLookup(MappedFieldType fieldType) {
@@ -37,51 +26,39 @@ MappedFieldType fieldType() {
3726
return fieldType;
3827
}
3928

40-
public Map<String, List<Object>> fields() {
41-
return fields;
42-
}
43-
4429
/**
4530
* Sets the post processed values.
4631
*/
47-
public void fields(Map<String, List<Object>> fields) {
48-
this.fields = fields;
32+
public void setValues(List<Object> values) {
33+
assert valuesLoaded == false : "Call clear() before calling setValues()";
34+
this.values.addAll(values);
35+
this.valuesLoaded = true;
36+
}
37+
38+
public boolean isLoaded() {
39+
return valuesLoaded;
4940
}
5041

5142
public void clear() {
52-
value = null;
53-
valueLoaded = false;
5443
values.clear();
5544
valuesLoaded = false;
56-
fields = null;
5745
}
5846

59-
public boolean isEmpty() {
60-
if (valueLoaded) {
61-
return value == null;
62-
}
63-
if (valuesLoaded) {
64-
return values.isEmpty();
65-
}
66-
return getValue() == null;
47+
// exposed by painless
48+
public List<Object> getValues() {
49+
assert valuesLoaded;
50+
return values;
6751
}
6852

53+
// exposed by painless
6954
public Object getValue() {
70-
if (valueLoaded) {
71-
return value;
72-
}
73-
valueLoaded = true;
74-
value = null;
75-
List<Object> values = fields.get(fieldType.name());
76-
return values != null ? value = values.get(0) : null;
55+
assert valuesLoaded;
56+
return values.isEmpty() ? null : values.get(0);
7757
}
7858

79-
public List<Object> getValues() {
80-
if (valuesLoaded) {
81-
return values;
82-
}
83-
valuesLoaded = true;
84-
values.clear();
85-
return values = fields().get(fieldType.name());
59+
// exposed by painless
60+
public boolean isEmpty() {
61+
assert valuesLoaded;
62+
return values.isEmpty();
8663
}
8764
}

server/src/main/java/org/elasticsearch/search/lookup/LeafStoredFieldsLookup.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.util.Set;
2323
import java.util.function.Function;
2424

25-
import static java.util.Collections.singletonMap;
26-
2725
@SuppressWarnings({ "unchecked", "rawtypes" })
2826
public class LeafStoredFieldsLookup implements Map<Object, FieldLookup> {
2927

@@ -133,11 +131,11 @@ private FieldLookup loadFieldData(String name) throws IOException {
133131
data = new FieldLookup(fieldType);
134132
cachedFieldData.put(name, data);
135133
}
136-
if (data.fields() == null) {
134+
if (data.isLoaded() == false) {
137135
List<Object> values = new ArrayList<>(2);
138136
SingleFieldsVisitor visitor = new SingleFieldsVisitor(data.fieldType(), values);
139137
storedFields.document(docId, visitor);
140-
data.fields(singletonMap(data.fieldType().name(), values));
138+
data.setValues(values);
141139
}
142140
return data;
143141
}

0 commit comments

Comments
 (0)