Skip to content

Commit baf863f

Browse files
committed
Move large amounts of processing out of format finder constructors
Major processing is now done in static factory methods and the constructors are trivial
1 parent cf2d675 commit baf863f

13 files changed

+120
-73
lines changed

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/AbstractStructuredLogStructureFinder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public abstract class AbstractStructuredLogStructureFinder extends AbstractLogSt
3434
* @return A tuple of (field name, timestamp format) if one can be found, or <code>null</code> if
3535
* there is no consistent timestamp.
3636
*/
37-
protected Tuple<String, TimestampMatch> guessTimestampField(List<String> explanation, List<Map<String, ?>> sampleRecords) {
37+
protected static Tuple<String, TimestampMatch> guessTimestampField(List<String> explanation, List<Map<String, ?>> sampleRecords) {
3838
if (sampleRecords.isEmpty()) {
3939
return null;
4040
}
@@ -70,7 +70,7 @@ protected Tuple<String, TimestampMatch> guessTimestampField(List<String> explana
7070
return null;
7171
}
7272

73-
private List<Tuple<String, TimestampMatch>> findCandidates(List<String> explanation, List<Map<String, ?>> sampleRecords) {
73+
private static List<Tuple<String, TimestampMatch>> findCandidates(List<String> explanation, List<Map<String, ?>> sampleRecords) {
7474

7575
List<Tuple<String, TimestampMatch>> candidates = new ArrayList<>();
7676

@@ -95,7 +95,7 @@ private List<Tuple<String, TimestampMatch>> findCandidates(List<String> explanat
9595
* @param sampleRecords The sampled records.
9696
* @return A map of field name to mapping settings.
9797
*/
98-
protected SortedMap<String, Object> guessMappings(List<String> explanation, List<Map<String, ?>> sampleRecords) {
98+
protected static SortedMap<String, Object> guessMappings(List<String> explanation, List<Map<String, ?>> sampleRecords) {
9999

100100
SortedMap<String, Object> mappings = new TreeMap<>();
101101

@@ -113,7 +113,7 @@ protected SortedMap<String, Object> guessMappings(List<String> explanation, List
113113
return mappings;
114114
}
115115

116-
Map<String, String> guessMapping(List<String> explanation, String fieldName, List<Object> fieldValues) {
116+
static Map<String, String> guessMapping(List<String> explanation, String fieldName, List<Object> fieldValues) {
117117

118118
if (fieldValues == null || fieldValues.isEmpty()) {
119119
// We can get here if all the records that contained a given field had a null value for it.

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/CsvLogStructureFinderFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public boolean canCreateFromSample(List<String> explanation, String sample) {
2929
@Override
3030
public LogStructureFinder createFromSample(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker)
3131
throws IOException {
32-
return new SeparatedValuesLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker,
32+
return SeparatedValuesLogStructureFinder.makeSeparatedValuesLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker,
3333
CsvPreference.EXCEL_PREFERENCE, false);
3434
}
3535
}

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/JsonLogStructureFinder.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ public class JsonLogStructureFinder extends AbstractStructuredLogStructureFinder
3030
private final List<String> sampleMessages;
3131
private final LogStructure structure;
3232

33-
JsonLogStructureFinder(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker) throws IOException {
33+
static JsonLogStructureFinder makeJsonLogStructureFinder(List<String> explanation, String sample, String charsetName,
34+
Boolean hasByteOrderMarker) throws IOException {
3435

3536
List<Map<String, ?>> sampleRecords = new ArrayList<>();
3637

37-
sampleMessages = Arrays.asList(sample.split("\n"));
38+
List<String> sampleMessages = Arrays.asList(sample.split("\n"));
3839
for (String sampleMessage : sampleMessages) {
3940
XContentParser parser = jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
4041
sampleMessage);
@@ -58,15 +59,22 @@ public class JsonLogStructureFinder extends AbstractStructuredLogStructureFinder
5859
SortedMap<String, Object> mappings = guessMappings(explanation, sampleRecords);
5960
mappings.put(DEFAULT_TIMESTAMP_FIELD, Collections.singletonMap(MAPPING_TYPE_SETTING, "date"));
6061

61-
structure = structureBuilder
62+
LogStructure structure = structureBuilder
6263
.setMappings(mappings)
6364
.setExplanation(explanation)
6465
.build();
66+
67+
return new JsonLogStructureFinder(sampleMessages, structure);
68+
}
69+
70+
private JsonLogStructureFinder(List<String> sampleMessages, LogStructure structure) {
71+
this.sampleMessages = Collections.unmodifiableList(sampleMessages);
72+
this.structure = structure;
6573
}
6674

6775
@Override
6876
public List<String> getSampleMessages() {
69-
return Collections.unmodifiableList(sampleMessages);
77+
return sampleMessages;
7078
}
7179

7280
@Override

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/JsonLogStructureFinderFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new ContextPrintingStringReader(
6363
@Override
6464
public LogStructureFinder createFromSample(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker)
6565
throws IOException {
66-
return new JsonLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker);
66+
return JsonLogStructureFinder.makeJsonLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker);
6767
}
6868

6969
private static class ContextPrintingStringReader extends StringReader {

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/PipeSeparatedValuesLogStructureFinderFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public boolean canCreateFromSample(List<String> explanation, String sample) {
3232
@Override
3333
public LogStructureFinder createFromSample(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker)
3434
throws IOException {
35-
return new SeparatedValuesLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker, PIPE_PREFERENCE, true);
35+
return SeparatedValuesLogStructureFinder.makeSeparatedValuesLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker,
36+
PIPE_PREFERENCE, true);
3637
}
3738
}

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/SemiColonSeparatedValuesLogStructureFinderFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public boolean canCreateFromSample(List<String> explanation, String sample) {
3131
@Override
3232
public LogStructureFinder createFromSample(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker)
3333
throws IOException {
34-
return new SeparatedValuesLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker,
34+
return SeparatedValuesLogStructureFinder.makeSeparatedValuesLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker,
3535
CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE, false);
3636
}
3737
}

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/SeparatedValuesLogStructureFinder.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ public class SeparatedValuesLogStructureFinder extends AbstractStructuredLogStru
3636
private final List<String> sampleMessages;
3737
private final LogStructure structure;
3838

39-
SeparatedValuesLogStructureFinder(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker,
40-
CsvPreference csvPreference, boolean trimFields) throws IOException {
39+
static SeparatedValuesLogStructureFinder makeSeparatedValuesLogStructureFinder(List<String> explanation, String sample,
40+
String charsetName, Boolean hasByteOrderMarker,
41+
CsvPreference csvPreference, boolean trimFields)
42+
throws IOException {
4143

4244
Tuple<List<List<String>>, List<Integer>> parsed = readRows(sample, csvPreference);
4345
List<List<String>> rows = parsed.v1();
@@ -53,7 +55,7 @@ public class SeparatedValuesLogStructureFinder extends AbstractStructuredLogStru
5355
}
5456

5557
List<String> sampleLines = Arrays.asList(sample.split("\n"));
56-
sampleMessages = new ArrayList<>();
58+
List<String> sampleMessages = new ArrayList<>();
5759
List<Map<String, ?>> sampleRecords = new ArrayList<>();
5860
int prevMessageEndLineNumber = isHeaderInFile ? lineNumbers.get(0) : -1;
5961
for (int index = isHeaderInFile ? 1 : 0; index < rows.size(); ++index) {
@@ -124,15 +126,22 @@ public class SeparatedValuesLogStructureFinder extends AbstractStructuredLogStru
124126
SortedMap<String, Object> mappings = guessMappings(explanation, sampleRecords);
125127
mappings.put(DEFAULT_TIMESTAMP_FIELD, Collections.singletonMap(MAPPING_TYPE_SETTING, "date"));
126128

127-
structure = structureBuilder
129+
LogStructure structure = structureBuilder
128130
.setMappings(mappings)
129131
.setExplanation(explanation)
130132
.build();
133+
134+
return new SeparatedValuesLogStructureFinder(sampleMessages, structure);
135+
}
136+
137+
private SeparatedValuesLogStructureFinder(List<String> sampleMessages, LogStructure structure) {
138+
this.sampleMessages = Collections.unmodifiableList(sampleMessages);
139+
this.structure = structure;
131140
}
132141

133142
@Override
134143
public List<String> getSampleMessages() {
135-
return Collections.unmodifiableList(sampleMessages);
144+
return sampleMessages;
136145
}
137146

138147
@Override

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/TextLogStructureFinder.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class TextLogStructureFinder extends AbstractLogStructureFinder implement
2525
private final List<String> sampleMessages;
2626
private final LogStructure structure;
2727

28-
TextLogStructureFinder(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker) {
28+
static TextLogStructureFinder makeTextLogStructureFinder(List<String> explanation, String sample, String charsetName,
29+
Boolean hasByteOrderMarker) {
2930

3031
String[] sampleLines = sample.split("\n");
3132
Tuple<TimestampMatch, Set<String>> bestTimestamp = mostLikelyTimestamp(sampleLines);
@@ -37,7 +38,7 @@ public class TextLogStructureFinder extends AbstractLogStructureFinder implement
3738

3839
explanation.add("Most likely timestamp format is [" + bestTimestamp.v1() + "]");
3940

40-
sampleMessages = new ArrayList<>();
41+
List<String> sampleMessages = new ArrayList<>();
4142
StringBuilder preamble = new StringBuilder();
4243
int linesConsumed = 0;
4344
StringBuilder message = null;
@@ -94,19 +95,26 @@ public class TextLogStructureFinder extends AbstractLogStructureFinder implement
9495
grokPattern = grokPatternCreator.createGrokPatternFromExamples(bestTimestamp.v1().grokPatternName, interimTimestampField);
9596
}
9697

97-
structure = structureBuilder
98+
LogStructure structure = structureBuilder
9899
.setTimestampField(interimTimestampField)
99100
.setTimestampFormats(bestTimestamp.v1().dateFormats)
100101
.setNeedClientTimezone(bestTimestamp.v1().hasTimezoneDependentParsing())
101102
.setGrokPattern(grokPattern)
102103
.setMappings(mappings)
103104
.setExplanation(explanation)
104105
.build();
106+
107+
return new TextLogStructureFinder(sampleMessages, structure);
108+
}
109+
110+
private TextLogStructureFinder(List<String> sampleMessages, LogStructure structure) {
111+
this.sampleMessages = Collections.unmodifiableList(sampleMessages);
112+
this.structure = structure;
105113
}
106114

107115
@Override
108116
public List<String> getSampleMessages() {
109-
return Collections.unmodifiableList(sampleMessages);
117+
return sampleMessages;
110118
}
111119

112120
@Override

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/TextLogStructureFinderFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public boolean canCreateFromSample(List<String> explanation, String sample) {
3333
}
3434

3535
@Override
36-
public LogStructureFinder createFromSample(List<String> explanation, String sample, String charsetName,
37-
Boolean hasByteOrderMarker) {
38-
return new TextLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker);
36+
public LogStructureFinder createFromSample(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker) {
37+
return TextLogStructureFinder.makeTextLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker);
3938
}
4039
}

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/TsvLogStructureFinderFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public boolean canCreateFromSample(List<String> explanation, String sample) {
2929
@Override
3030
public LogStructureFinder createFromSample(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker)
3131
throws IOException {
32-
return new SeparatedValuesLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker, CsvPreference.TAB_PREFERENCE,
33-
false);
32+
return SeparatedValuesLogStructureFinder.makeSeparatedValuesLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker,
33+
CsvPreference.TAB_PREFERENCE, false);
3434
}
3535
}

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/XmlLogStructureFinder.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public class XmlLogStructureFinder extends AbstractStructuredLogStructureFinder
3535
private final List<String> sampleMessages;
3636
private final LogStructure structure;
3737

38-
XmlLogStructureFinder(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker)
38+
static XmlLogStructureFinder makeXmlLogStructureFinder(List<String> explanation, String sample, String charsetName,
39+
Boolean hasByteOrderMarker)
3940
throws IOException, ParserConfigurationException, SAXException {
4041

4142
String messagePrefix;
@@ -47,7 +48,7 @@ public class XmlLogStructureFinder extends AbstractStructuredLogStructureFinder
4748
docBuilderFactory.setNamespaceAware(false);
4849
docBuilderFactory.setValidating(false);
4950

50-
sampleMessages = new ArrayList<>();
51+
List<String> sampleMessages = new ArrayList<>();
5152
List<Map<String, ?>> sampleRecords = new ArrayList<>();
5253

5354
String[] sampleDocEnds = sample.split(Pattern.quote(messagePrefix));
@@ -102,15 +103,22 @@ public class XmlLogStructureFinder extends AbstractStructuredLogStructureFinder
102103
outerMappings.put(topLevelTag, secondLevelProperties);
103104
outerMappings.put(DEFAULT_TIMESTAMP_FIELD, Collections.singletonMap(MAPPING_TYPE_SETTING, "date"));
104105

105-
structure = structureBuilder
106+
LogStructure structure = structureBuilder
106107
.setMappings(outerMappings)
107108
.setExplanation(explanation)
108109
.build();
110+
111+
return new XmlLogStructureFinder(sampleMessages, structure);
112+
}
113+
114+
private XmlLogStructureFinder(List<String> sampleMessages, LogStructure structure) {
115+
this.sampleMessages = Collections.unmodifiableList(sampleMessages);
116+
this.structure = structure;
109117
}
110118

111119
@Override
112120
public List<String> getSampleMessages() {
113-
return Collections.unmodifiableList(sampleMessages);
121+
return sampleMessages;
114122
}
115123

116124
@Override

x-pack/plugin/ml/log-structure-finder/src/main/java/org/elasticsearch/xpack/ml/logstructurefinder/XmlLogStructureFinderFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,6 @@ public boolean canCreateFromSample(List<String> explanation, String sample) {
117117
@Override
118118
public LogStructureFinder createFromSample(List<String> explanation, String sample, String charsetName, Boolean hasByteOrderMarker)
119119
throws IOException, ParserConfigurationException, SAXException {
120-
return new XmlLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker);
120+
return XmlLogStructureFinder.makeXmlLogStructureFinder(explanation, sample, charsetName, hasByteOrderMarker);
121121
}
122122
}

0 commit comments

Comments
 (0)