29
29
import java .util .Set ;
30
30
import java .util .function .Function ;
31
31
32
- import static java .util .Collections .emptyMap ;
33
-
34
32
/**
35
33
* Base {@link StoredFieldVisitor} that retrieves all non-redundant metadata.
36
34
*/
@@ -40,9 +38,9 @@ public class FieldsVisitor extends FieldNamesProvidingStoredFieldsVisitor {
40
38
private final boolean loadSource ;
41
39
final String sourceFieldName ;
42
40
private final Set <String > requiredFields ;
43
- protected BytesReference source ;
44
- protected String id ;
45
- protected Map <String , List <Object >> fieldsValues ;
41
+ private BytesReference source ;
42
+ private String id ;
43
+ private final Map <String , List <Object >> fieldsValues = new HashMap <>() ;
46
44
47
45
public FieldsVisitor (boolean loadSource ) {
48
46
this (loadSource , SourceFieldMapper .NAME );
@@ -58,24 +56,29 @@ public FieldsVisitor(boolean loadSource, String sourceFieldName) {
58
56
59
57
@ Override
60
58
public Status needsField (FieldInfo fieldInfo ) {
61
- if (requiredFields .remove (fieldInfo .name )) {
59
+ final String name = fieldInfo .name ;
60
+ var requiredFields = this .requiredFields ;
61
+ if (requiredFields .remove (name )) {
62
62
return Status .YES ;
63
63
}
64
64
// Always load _ignored to be explicit about ignored fields
65
65
// This works because _ignored is added as the first metadata mapper,
66
66
// so its stored fields always appear first in the list.
67
67
// Note that _ignored is also multi-valued, which is why it can't be removed from the set like other fields
68
- if (IgnoredFieldMapper .NAME .equals (fieldInfo . name )) {
68
+ if (IgnoredFieldMapper .NAME .equals (name )) {
69
69
return Status .YES ;
70
70
}
71
+ // All these fields are single-valued so we can stop when the set is empty
72
+ if (requiredFields .isEmpty ()) {
73
+ return Status .STOP ;
74
+ }
71
75
// support _uid for loading older indices
72
- if ("_uid" .equals (fieldInfo . name )) {
76
+ if ("_uid" .equals (name )) {
73
77
if (requiredFields .remove (IdFieldMapper .NAME ) || requiredFields .remove (LegacyTypeFieldMapper .NAME )) {
74
78
return Status .YES ;
75
79
}
76
80
}
77
- // All these fields are single-valued so we can stop when the set is empty
78
- return requiredFields .isEmpty () ? Status .STOP : Status .NO ;
81
+ return Status .NO ;
79
82
}
80
83
81
84
@ Override
@@ -98,33 +101,31 @@ public final void postProcess(Function<String, MappedFieldType> fieldTypeLookup)
98
101
99
102
@ Override
100
103
public void binaryField (FieldInfo fieldInfo , byte [] value ) {
101
- binaryField (fieldInfo , new BytesRef (value ));
102
- }
103
-
104
- private void binaryField (FieldInfo fieldInfo , BytesRef value ) {
105
- if (sourceFieldName .equals (fieldInfo .name )) {
104
+ final String name = fieldInfo .name ;
105
+ if (sourceFieldName .equals (name )) {
106
106
source = new BytesArray (value );
107
- } else if (IdFieldMapper .NAME .equals (fieldInfo . name )) {
108
- id = Uid .decodeId (value . bytes , value . offset , value .length );
107
+ } else if (IdFieldMapper .NAME .equals (name )) {
108
+ id = Uid .decodeId (value , 0 , value .length );
109
109
} else {
110
- addValue (fieldInfo . name , value );
110
+ addValue (name , new BytesRef ( value ) );
111
111
}
112
112
}
113
113
114
114
@ Override
115
115
public void stringField (FieldInfo fieldInfo , String value ) {
116
- assert sourceFieldName .equals (fieldInfo .name ) == false : "source field must go through binaryField" ;
117
- if ("_uid" .equals (fieldInfo .name )) {
116
+ final String name = fieldInfo .name ;
117
+ assert sourceFieldName .equals (name ) == false : "source field must go through binaryField" ;
118
+ if ("_uid" .equals (name )) {
118
119
// 5.x-only
119
120
int delimiterIndex = value .indexOf ('#' ); // type is not allowed to have # in it..., ids can
120
121
String type = value .substring (0 , delimiterIndex );
121
122
id = value .substring (delimiterIndex + 1 );
122
123
addValue (LegacyTypeFieldMapper .NAME , type );
123
- } else if (IdFieldMapper .NAME .equals (fieldInfo . name )) {
124
+ } else if (IdFieldMapper .NAME .equals (name )) {
124
125
// only applies to 5.x indices that have single_type = true
125
126
id = value ;
126
127
} else {
127
- addValue (fieldInfo . name , value );
128
+ addValue (name , value );
128
129
}
129
130
}
130
131
@@ -157,25 +158,20 @@ public String id() {
157
158
}
158
159
159
160
public String routing () {
160
- if (fieldsValues == null ) {
161
- return null ;
162
- }
163
161
List <Object > values = fieldsValues .get (RoutingFieldMapper .NAME );
164
162
if (values == null || values .isEmpty ()) {
165
163
return null ;
166
164
}
167
165
assert values .size () == 1 ;
168
- return values .get ( 0 ).toString ();
166
+ return values .getFirst ( ).toString ();
169
167
}
170
168
171
169
public Map <String , List <Object >> fields () {
172
- return fieldsValues != null ? fieldsValues : emptyMap () ;
170
+ return fieldsValues ;
173
171
}
174
172
175
173
public void reset () {
176
- if (fieldsValues != null ) {
177
- fieldsValues .clear ();
178
- }
174
+ fieldsValues .clear ();
179
175
source = null ;
180
176
id = null ;
181
177
@@ -186,11 +182,6 @@ public void reset() {
186
182
}
187
183
188
184
void addValue (String name , Object value ) {
189
- if (fieldsValues == null ) {
190
- fieldsValues = new HashMap <>();
191
- }
192
-
193
- List <Object > values = fieldsValues .computeIfAbsent (name , k -> new ArrayList <>(2 ));
194
- values .add (value );
185
+ fieldsValues .computeIfAbsent (name , k -> new ArrayList <>(2 )).add (value );
195
186
}
196
187
}
0 commit comments