Skip to content

Commit

Permalink
Ingest DateProcessor (small) speedup, optimize collections code in Da…
Browse files Browse the repository at this point in the history
…teFormatter.forPattern (#91521)
  • Loading branch information
joegallo authored Nov 14, 2022
1 parent ecce75a commit 0ee5c76
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private ZoneId newDateTimeZone(Map<String, Object> params) {
}

private Locale newLocale(Map<String, Object> params) {
return (locale == null) ? Locale.ROOT : LocaleUtils.parse(locale.newInstance(params).execute());
return locale == null ? Locale.ROOT : LocaleUtils.parse(locale.newInstance(params).execute());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

public interface DateFormatter {

Expand Down Expand Up @@ -114,14 +113,17 @@ static DateFormatter forPattern(String input, Version supportedVersion) {
input = input.substring(1);
}

List<String> patterns = splitCombinedPatterns(input);
List<DateFormatter> formatters = patterns.stream().map(p -> {
// forPattern can be hot (e.g. executing a date processor on each document in a 1000 document bulk index request),
// so this is a for each loop instead of the equivalent stream pipeline
String[] patterns = splitCombinedPatterns(input);
List<DateFormatter> formatters = new ArrayList<>(patterns.length);
for (String pattern : patterns) {
// make sure we still support camel case for indices created before 8.0
if (supportedVersion.before(Version.V_8_0_0)) {
return LegacyFormatNames.camelCaseToSnakeCase(p);
pattern = LegacyFormatNames.camelCaseToSnakeCase(pattern);
}
return p;
}).map(DateFormatters::forPattern).collect(Collectors.toList());
formatters.add(DateFormatters.forPattern(pattern));
}

if (formatters.size() == 1) {
return formatters.get(0);
Expand All @@ -130,13 +132,12 @@ static DateFormatter forPattern(String input, Version supportedVersion) {
return JavaDateFormatter.combined(input, formatters);
}

static List<String> splitCombinedPatterns(String input) {
List<String> patterns = new ArrayList<>();
for (String pattern : Strings.delimitedListToStringArray(input, "||")) {
static String[] splitCombinedPatterns(String input) {
String[] patterns = Strings.delimitedListToStringArray(input, "||");
for (String pattern : patterns) {
if (Strings.hasLength(pattern) == false) {
throw new IllegalArgumentException("Cannot have empty element in multi date format pattern: " + input);
}
patterns.add(pattern);
}
return patterns;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ static DeprecationIssue deprecatedCamelCasePattern(IndexMetadata indexMetadata)

private static boolean isDateFieldWithCamelCasePattern(Map<?, ?> property) {
if ("date".equals(property.get("type")) && property.containsKey("format")) {
List<String> patterns = DateFormatter.splitCombinedPatterns((String) property.get("format"));
String[] patterns = DateFormatter.splitCombinedPatterns((String) property.get("format"));
for (String pattern : patterns) {
LegacyFormatNames format = LegacyFormatNames.forName(pattern);
return format != null && format.isCamelCase(pattern);
Expand All @@ -239,7 +239,7 @@ private static boolean isDateFieldWithCamelCasePattern(Map<?, ?> property) {
private static String changeFormatToSnakeCase(String type, Map.Entry<?, ?> entry) {
Map<?, ?> value = (Map<?, ?>) entry.getValue();
final String formatFieldValue = (String) value.get("format");
List<String> patterns = DateFormatter.splitCombinedPatterns(formatFieldValue);
String[] patterns = DateFormatter.splitCombinedPatterns(formatFieldValue);
StringBuilder sb = new StringBuilder(
"Convert [" + entry.getKey() + "] format [" + formatFieldValue + "] " + "which contains deprecated camel case to snake case. "
);
Expand Down

0 comments on commit 0ee5c76

Please sign in to comment.