1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition;
import com.yahoo.document.DataType;
import com.yahoo.document.Field;
import com.yahoo.searchdefinition.document.SDDocumentType;
import com.yahoo.searchdefinition.document.SDField;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author Einar M R Rosenvinge
*/
public class FieldOperationApplierForStructs extends FieldOperationApplier {
@Override
public void process(SDDocumentType sdoc) {
for (SDDocumentType type : sdoc.getAllTypes()) {
if (type.isStruct()) {
apply(type);
copyFields(type, sdoc);
}
}
}
@SuppressWarnings("deprecation")
private void copyFields(SDDocumentType structType, SDDocumentType sdoc) {
//find all fields in OTHER types that have this type:
List<SDDocumentType> list = new ArrayList<>();
list.add(sdoc);
list.addAll(sdoc.getTypes());
for (SDDocumentType anyType : list) {
Iterator<Field> fields = anyType.fieldIterator();
while (fields.hasNext()) {
SDField field = (SDField) fields.next();
maybePopulateField(sdoc, field, structType);
}
}
}
private void maybePopulateField(SDDocumentType sdoc, SDField field, SDDocumentType structType) {
DataType structUsedByField = field.getFirstStructRecursive();
if (structUsedByField == null) {
return;
}
if (structUsedByField.getName().equals(structType.getName())) {
//this field is using this type!!
field.populateWithStructFields(sdoc, field.getName(), field.getDataType(), 0);
field.populateWithStructMatching(sdoc, field.getDataType(), field.getMatching());
}
}
public void processSchemaFields(Schema schema) {
var sdoc = schema.getDocument();
if (sdoc == null) return;
for (SDDocumentType type : sdoc.getAllTypes()) {
if (type.isStruct()) {
for (SDField field : schema.allExtraFields()) {
maybePopulateField(sdoc, field, type);
}
}
}
}
}
|