Skip to content

Commit faa3c16

Browse files
authored
Core: Add DateFormatter interface for java time parsing (#33467)
The existing approach used date formatters when a format based string like `date_time||epoch_millis` was used, instead of the custom code. In order to properly solve this, a new interface called `DateFormatter` has been added, which now can be implemented for custom formatters. Currently there are two implementations, one using java time and one doing the epoch_millis formatter, which simply parses a number and then converts it to a date in UTC timezone. The DateFormatter interface now also has a method to retrieve the name of the formatter pattern, which is needed for mapping changes anyway. The existing `CompoundDateTimeFormatter` class has been removed, the name was not really nice anyway. One more minor change is the fact, that the new java time using FormatDateFormatter does not try to parse the date with its printer implementation first (which might be a strict one and fail), but a printer can now be specified in addition. This saves one potential failure/exception when parsing less strict dates. If only a printer is specified, the printer will also be used as a parser.
1 parent 222f422 commit faa3c16

File tree

17 files changed

+456
-249
lines changed

17 files changed

+456
-249
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/IndexGraveyard.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.elasticsearch.common.io.stream.Writeable;
2929
import org.elasticsearch.common.settings.Setting;
3030
import org.elasticsearch.common.settings.Settings;
31-
import org.elasticsearch.common.time.CompoundDateTimeFormatter;
31+
import org.elasticsearch.common.time.DateFormatter;
3232
import org.elasticsearch.common.time.DateFormatters;
3333
import org.elasticsearch.common.xcontent.ContextParser;
3434
import org.elasticsearch.common.xcontent.ObjectParser;
@@ -368,8 +368,7 @@ public static final class Tombstone implements ToXContentObject, Writeable {
368368
TOMBSTONE_PARSER.declareString((b, s) -> {}, new ParseField(DELETE_DATE_KEY));
369369
}
370370

371-
static final CompoundDateTimeFormatter FORMATTER =
372-
DateFormatters.forPattern("strict_date_optional_time").withZone(ZoneOffset.UTC);
371+
static final DateFormatter FORMATTER = DateFormatters.forPattern("strict_date_optional_time").withZone(ZoneOffset.UTC);
373372

374373
static ContextParser<Void, Tombstone> getParser() {
375374
return (parser, context) -> TOMBSTONE_PARSER.apply(parser, null).build();

server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.elasticsearch.common.settings.Setting;
3232
import org.elasticsearch.common.settings.Setting.Property;
3333
import org.elasticsearch.common.settings.Settings;
34-
import org.elasticsearch.common.time.CompoundDateTimeFormatter;
34+
import org.elasticsearch.common.time.DateFormatter;
3535
import org.elasticsearch.common.time.DateFormatters;
3636
import org.elasticsearch.common.unit.TimeValue;
3737
import org.elasticsearch.common.xcontent.ToXContentFragment;
@@ -48,8 +48,7 @@
4848
*/
4949
public final class UnassignedInfo implements ToXContentFragment, Writeable {
5050

51-
public static final CompoundDateTimeFormatter DATE_TIME_FORMATTER =
52-
DateFormatters.forPattern("dateOptionalTime").withZone(ZoneOffset.UTC);
51+
public static final DateFormatter DATE_TIME_FORMATTER = DateFormatters.forPattern("dateOptionalTime").withZone(ZoneOffset.UTC);
5352

5453
public static final Setting<TimeValue> INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING =
5554
Setting.positiveTimeSetting("index.unassigned.node_left.delayed_timeout", TimeValue.timeValueMinutes(1), Property.Dynamic,

server/src/main/java/org/elasticsearch/common/Table.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.elasticsearch.common;
2121

22-
import org.elasticsearch.common.time.CompoundDateTimeFormatter;
22+
import org.elasticsearch.common.time.DateFormatter;
2323
import org.elasticsearch.common.time.DateFormatters;
2424

2525
import java.time.Instant;
@@ -85,7 +85,7 @@ public Table endHeaders() {
8585
return this;
8686
}
8787

88-
private static final CompoundDateTimeFormatter FORMATTER = DateFormatters.forPattern("HH:mm:ss").withZone(ZoneOffset.UTC);
88+
private static final DateFormatter FORMATTER = DateFormatters.forPattern("HH:mm:ss").withZone(ZoneOffset.UTC);
8989

9090
public Table startRow() {
9191
if (headers.isEmpty()) {
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.common.time;
21+
22+
import java.time.ZoneId;
23+
import java.time.format.DateTimeParseException;
24+
import java.time.temporal.TemporalAccessor;
25+
import java.time.temporal.TemporalField;
26+
import java.util.Arrays;
27+
import java.util.Map;
28+
import java.util.stream.Collectors;
29+
30+
public interface DateFormatter {
31+
32+
/**
33+
* Try to parse input to a java time TemporalAccessor
34+
* @param input An arbitrary string resembling the string representation of a date or time
35+
* @throws DateTimeParseException If parsing fails, this exception will be thrown.
36+
* Note that it can contained suppressed exceptions when several formatters failed parse this value
37+
* @return The java time object containing the parsed input
38+
*/
39+
TemporalAccessor parse(String input);
40+
41+
/**
42+
* Create a copy of this formatter that is configured to parse dates in the specified time zone
43+
*
44+
* @param zoneId The time zone to act on
45+
* @return A copy of the date formatter this has been called on
46+
*/
47+
DateFormatter withZone(ZoneId zoneId);
48+
49+
/**
50+
* Print the supplied java time accessor in a string based representation according to this formatter
51+
*
52+
* @param accessor The temporal accessor used to format
53+
* @return The string result for the formatting
54+
*/
55+
String format(TemporalAccessor accessor);
56+
57+
/**
58+
* A name based format for this formatter. Can be one of the registered formatters like <code>epoch_millis</code> or
59+
* a configured format like <code>HH:mm:ss</code>
60+
*
61+
* @return The name of this formatter
62+
*/
63+
String pattern();
64+
65+
/**
66+
* Configure a formatter using default fields for a TemporalAccessor that should be used in case
67+
* the supplied date is not having all of those fields
68+
*
69+
* @param fields A <code>Map&lt;TemporalField, Long&gt;</code> of fields to be used as fallbacks
70+
* @return A new date formatter instance, that will use those fields during parsing
71+
*/
72+
DateFormatter parseDefaulting(Map<TemporalField, Long> fields);
73+
74+
/**
75+
* Merge several date formatters into a single one. Useful if you need to have several formatters with
76+
* different formats act as one, for example when you specify a
77+
* format like <code>date_hour||epoch_millis</code>
78+
*
79+
* @param formatters The list of date formatters to be merged together
80+
* @return The new date formtter containing the specified date formatters
81+
*/
82+
static DateFormatter merge(DateFormatter ... formatters) {
83+
return new MergedDateFormatter(formatters);
84+
}
85+
86+
class MergedDateFormatter implements DateFormatter {
87+
88+
private final String format;
89+
private final DateFormatter[] formatters;
90+
91+
MergedDateFormatter(DateFormatter ... formatters) {
92+
this.formatters = formatters;
93+
this.format = Arrays.stream(formatters).map(DateFormatter::pattern).collect(Collectors.joining("||"));
94+
}
95+
96+
@Override
97+
public TemporalAccessor parse(String input) {
98+
DateTimeParseException failure = null;
99+
for (DateFormatter formatter : formatters) {
100+
try {
101+
return formatter.parse(input);
102+
} catch (DateTimeParseException e) {
103+
if (failure == null) {
104+
failure = e;
105+
} else {
106+
failure.addSuppressed(e);
107+
}
108+
}
109+
}
110+
throw failure;
111+
}
112+
113+
@Override
114+
public DateFormatter withZone(ZoneId zoneId) {
115+
return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.withZone(zoneId)).toArray(DateFormatter[]::new));
116+
}
117+
118+
@Override
119+
public String format(TemporalAccessor accessor) {
120+
return formatters[0].format(accessor);
121+
}
122+
123+
@Override
124+
public String pattern() {
125+
return format;
126+
}
127+
128+
@Override
129+
public DateFormatter parseDefaulting(Map<TemporalField, Long> fields) {
130+
return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.parseDefaulting(fields)).toArray(DateFormatter[]::new));
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)