Skip to content

Commit

Permalink
Refactor DateTimeFormatter Access under featireflag
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhat Sharma <[email protected]>
  • Loading branch information
Prabhat Sharma committed Oct 4, 2023
1 parent e0f1d72 commit f195436
Show file tree
Hide file tree
Showing 22 changed files with 47 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected Settings featureFlagSettings() {
}

private ZonedDateTime date(String date) {
return DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date));
return DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date));
}

private static String format(ZonedDateTime date, String pattern) {
Expand Down Expand Up @@ -1624,8 +1624,8 @@ public void testScriptCaching() throws Exception {
.setSettings(Settings.builder().put("requests.cache.enable", true).put("number_of_shards", 1).put("number_of_replicas", 1))
.get()
);
String date = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format(date(1, 1));
String date2 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format(date(2, 1));
String date = DateFieldMapper.getDefaultDateTimeFormatter().format(date(1, 1));
String date2 = DateFieldMapper.getDefaultDateTimeFormatter().format(date(2, 1));
indexRandom(
true,
client().prepareIndex("cache_test_idx").setId("1").setSource("d", date),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected Settings featureFlagSettings() {
}

private ZonedDateTime date(String date) {
return DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date));
return DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date));
}

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.opensearch.common.time.DateMathParser;
import org.opensearch.common.time.DateUtils;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.FeatureFlags;
import org.opensearch.common.util.LocaleUtils;
import org.opensearch.index.fielddata.IndexFieldData;
import org.opensearch.index.fielddata.IndexNumericFieldData.NumericType;
Expand Down Expand Up @@ -101,6 +102,12 @@ public final class DateFieldMapper extends ParametrizedFieldMapper {
"strict_date_optional_time"
);

public static DateFormatter getDefaultDateTimeFormatter() {
return FeatureFlags.isEnabled(FeatureFlags.DATETIME_FORMATTER_CACHING)
? DEFAULT_DATE_TIME_FORMATTER
: LEGACY_DEFAULT_DATE_TIME_FORMATTER;
}

/**
* Resolution of the date time
*
Expand Down Expand Up @@ -231,13 +238,13 @@ public static class Builder extends ParametrizedFieldMapper.Builder {
"format",
false,
m -> toType(m).format,
DEFAULT_DATE_TIME_FORMATTER.pattern()
getDefaultDateTimeFormatter().pattern()
);
private final Parameter<String> printFormat = Parameter.stringParam(
"print_format",
false,
m -> toType(m).printFormat,
DEFAULT_DATE_TIME_FORMATTER.printPattern()
getDefaultDateTimeFormatter().printPattern()
).acceptsNull();
private final Parameter<Locale> locale = new Parameter<>(
"locale",
Expand Down Expand Up @@ -365,15 +372,15 @@ public DateFieldType(
}

public DateFieldType(String name) {
this(name, true, false, true, DEFAULT_DATE_TIME_FORMATTER, Resolution.MILLISECONDS, null, Collections.emptyMap());
this(name, true, false, true, getDefaultDateTimeFormatter(), Resolution.MILLISECONDS, null, Collections.emptyMap());
}

public DateFieldType(String name, DateFormatter dateFormatter) {
this(name, true, false, true, dateFormatter, Resolution.MILLISECONDS, null, Collections.emptyMap());
}

public DateFieldType(String name, Resolution resolution) {
this(name, true, false, true, DEFAULT_DATE_TIME_FORMATTER, resolution, null, Collections.emptyMap());
this(name, true, false, true, getDefaultDateTimeFormatter(), resolution, null, Collections.emptyMap());
}

public DateFieldType(String name, Resolution resolution, DateFormatter dateFormatter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class RangeFieldMapper extends ParametrizedFieldMapper {
*/
public static class Defaults {
public static final Explicit<Boolean> COERCE = new Explicit<>(true, false);
public static final DateFormatter DATE_FORMATTER = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER;
public static final DateFormatter DATE_FORMATTER = DateFieldMapper.getDefaultDateTimeFormatter();
}

// this is private since it has a different default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public Query rangeQuery(
) {
ZoneId zone = (timeZone == null) ? ZoneOffset.UTC : timeZone;

DateMathParser dateMathParser = (parser == null) ? DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.toDateMathParser() : parser;
DateMathParser dateMathParser = (parser == null) ? DateFieldMapper.getDefaultDateTimeFormatter().toDateMathParser() : parser;
boolean roundUp = includeLower == false; // using "gt" should round lower bound up
Long low = lowerTerm == null
? minValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class RootObjectMapper extends ObjectMapper {
*/
public static class Defaults {
public static final DateFormatter[] DYNAMIC_DATE_TIME_FORMATTERS = new DateFormatter[] {
DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER,
DateFieldMapper.getDefaultDateTimeFormatter(),
DateFormatter.forPattern("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis") };
public static final boolean DATE_DETECTION = true;
public static final boolean NUMERIC_DETECTION = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public double decayNumericGauss(double docValue) {
*
*/
private static final ZoneId defaultZoneId = ZoneId.of("UTC");
private static final DateMathParser dateParser = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.toDateMathParser();
private static final DateMathParser dateParser = DateFieldMapper.getDefaultDateTimeFormatter().toDateMathParser();

/**
* Linear date decay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public String getWriteableName() {

@Override
public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().before(Version.V_3_0_0) && formatter.equals(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER)) {
if (out.getVersion().before(Version.V_3_0_0) && formatter.equals(DateFieldMapper.getDefaultDateTimeFormatter())) {
out.writeString(DateFieldMapper.LEGACY_DEFAULT_DATE_TIME_FORMATTER.pattern()); // required for backwards compatibility
} else {
out.writeString(formatter.pattern());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public ValuesSource replaceMissing(ValuesSource valuesSource, Object rawMissing,
@Override
public DocValueFormat getFormatter(String format, ZoneId tz) {
return new DocValueFormat.DateTime(
format == null ? DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER : DateFormatter.forPattern(format),
format == null ? DateFieldMapper.getDefaultDateTimeFormatter() : DateFormatter.forPattern(format),
tz == null ? ZoneOffset.UTC : tz,
// If we were just looking at fields, we could read the resolution from the field settings, but we need to deal with script
// output, which has no way to indicate the resolution, so we need to default to something. Milliseconds is the standard.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public enum ValueType implements Writeable {
"date",
"date",
CoreValuesSourceType.DATE,
new DocValueFormat.DateTime(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, ZoneOffset.UTC, DateFieldMapper.Resolution.MILLISECONDS)
new DocValueFormat.DateTime(DateFieldMapper.getDefaultDateTimeFormatter(), ZoneOffset.UTC, DateFieldMapper.Resolution.MILLISECONDS)
),
IP((byte) 6, "ip", "ip", CoreValuesSourceType.IP, DocValueFormat.IP),
// TODO: what is the difference between "number" and "numeric"?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void isFieldWithinRangeTestCase(DateFieldType ft) throws IOException {
w.addDocument(doc);
DirectoryReader reader = DirectoryReader.open(w);

DateMathParser alternateFormat = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.toDateMathParser();
DateMathParser alternateFormat = DateFieldMapper.getDefaultDateTimeFormatter().toDateMathParser();
doTestIsFieldWithinQuery(ft, reader, null, null);
doTestIsFieldWithinQuery(ft, reader, null, alternateFormat);
doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, null);
Expand Down Expand Up @@ -158,7 +158,7 @@ private void doTestIsFieldWithinQuery(DateFieldType ft, DirectoryReader reader,

public void testValueFormat() {
MappedFieldType ft = new DateFieldType("field");
long instant = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse("2015-10-12T14:10:55"))
long instant = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse("2015-10-12T14:10:55"))
.toInstant()
.toEpochMilli();

Expand All @@ -167,14 +167,14 @@ public void testValueFormat() {
assertEquals("2015", new DateFieldType("field").docValueFormat("YYYY", ZoneOffset.UTC).format(instant));
assertEquals(instant, ft.docValueFormat(null, ZoneOffset.UTC).parseLong("2015-10-12T14:10:55", false, null));
assertEquals(instant + 999, ft.docValueFormat(null, ZoneOffset.UTC).parseLong("2015-10-12T14:10:55", true, null));
long i = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse("2015-10-13")).toInstant().toEpochMilli();
long i = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse("2015-10-13")).toInstant().toEpochMilli();
assertEquals(i - 1, ft.docValueFormat(null, ZoneOffset.UTC).parseLong("2015-10-12||/d", true, null));
}

public void testValueForSearch() {
MappedFieldType ft = new DateFieldType("field");
String date = "2015-10-12T12:09:55.000Z";
long instant = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(date);
long instant = DateFieldMapper.getDefaultDateTimeFormatter().parseMillis(date);
assertEquals(date, ft.valueForDisplay(instant));
}

Expand Down Expand Up @@ -205,7 +205,7 @@ public void testTermQuery() {
);
MappedFieldType ft = new DateFieldType("field");
String date = "2015-10-12T14:10:55";
long instant = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date)).toInstant().toEpochMilli();
long instant = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date)).toInstant().toEpochMilli();
Query expected = new IndexOrDocValuesQuery(
LongPoint.newRangeQuery("field", instant, instant + 999),
SortedNumericDocValuesField.newSlowRangeQuery("field", instant, instant + 999)
Expand All @@ -217,7 +217,7 @@ public void testTermQuery() {
false,
false,
true,
DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER,
DateFieldMapper.getDefaultDateTimeFormatter(),
Resolution.MILLISECONDS,
null,
Collections.emptyMap()
Expand Down Expand Up @@ -254,8 +254,8 @@ public void testRangeQuery() throws IOException {
MappedFieldType ft = new DateFieldType("field");
String date1 = "2015-10-12T14:10:55";
String date2 = "2016-04-28T11:33:52";
long instant1 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date1)).toInstant().toEpochMilli();
long instant2 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date2)).toInstant().toEpochMilli() + 999;
long instant1 = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date1)).toInstant().toEpochMilli();
long instant2 = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date2)).toInstant().toEpochMilli() + 999;
Query expected = new IndexOrDocValuesQuery(
LongPoint.newRangeQuery("field", instant1, instant2),
SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2)
Expand All @@ -280,7 +280,7 @@ public void testRangeQuery() throws IOException {
false,
false,
true,
DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER,
DateFieldMapper.getDefaultDateTimeFormatter(),
Resolution.MILLISECONDS,
null,
Collections.emptyMap()
Expand Down Expand Up @@ -326,8 +326,8 @@ public void testRangeQueryWithIndexSort() {
MappedFieldType ft = new DateFieldType("field");
String date1 = "2015-10-12T14:10:55";
String date2 = "2016-04-28T11:33:52";
long instant1 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date1)).toInstant().toEpochMilli();
long instant2 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date2)).toInstant().toEpochMilli() + 999;
long instant1 = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date1)).toInstant().toEpochMilli();
long instant2 = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date2)).toInstant().toEpochMilli() + 999;

Query pointQuery = LongPoint.newRangeQuery("field", instant1, instant2);
Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ protected RangeQueryBuilder doCreateTestQueryBuilder() {
ZonedDateTime start = now.minusMillis(randomIntBetween(0, 1000000)).atZone(ZoneOffset.UTC);
ZonedDateTime end = now.plusMillis(randomIntBetween(0, 1000000)).atZone(ZoneOffset.UTC);
query = new RangeQueryBuilder(randomFrom(DATE_FIELD_NAME, DATE_RANGE_FIELD_NAME, DATE_ALIAS_FIELD_NAME));
query.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format(start));
query.to(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.format(end));
query.from(DateFieldMapper.getDefaultDateTimeFormatter().format(start));
query.to(DateFieldMapper.getDefaultDateTimeFormatter().format(end));
// Create timestamp option only then we have a date mapper,
// otherwise we could trigger exception.
if (createShardContext().getMapperService().fieldType(DATE_FIELD_NAME) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private ValuesSourceConfig getVSConfig(
indexed,
false,
true,
DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER,
DateFieldMapper.getDefaultDateTimeFormatter(),
resolution,
null,
Collections.emptyMap()
Expand Down Expand Up @@ -184,7 +184,7 @@ public void testShortcutIsApplicable() throws IOException {
assertNull(pointReaderShim(mockSearchContext(null), null, getVSConfig("number", resolution, false, context)));
}
// Check that we decode a dates "just like" the doc values instance.
Instant expected = Instant.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse("2020-01-01T00:00:00Z"));
Instant expected = Instant.from(DateFieldMapper.getDefaultDateTimeFormatter().parse("2020-01-01T00:00:00Z"));
byte[] scratch = new byte[8];
LongPoint.encodeDimension(DateFieldMapper.Resolution.MILLISECONDS.convert(expected), scratch, 0);
assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected final DateFieldMapper.DateFieldType aggregableDateFieldType(boolean us
isSearchable,
false,
true,
DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER,
DateFieldMapper.getDefaultDateTimeFormatter(),
useNanosecondResolution ? DateFieldMapper.Resolution.NANOSECONDS : DateFieldMapper.Resolution.MILLISECONDS,
null,
Collections.emptyMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ private void testSearchCase(
}

private static long asLong(String dateTime) {
return DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(dateTime)).toInstant().toEpochMilli();
return DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(dateTime)).toInstant().toEpochMilli();
}

private static long asLong(String dateTime, DateFieldMapper.DateFieldType fieldType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ private void testCase(
}

private static long asLong(String dateTime) {
return DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(dateTime)).toInstant().toEpochMilli();
return DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(dateTime)).toInstant().toEpochMilli();
}

private static ZonedDateTime asZDT(String dateTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private void testCase(
true,
false,
true,
DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER,
DateFieldMapper.getDefaultDateTimeFormatter(),
resolution,
null,
Collections.emptyMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void testDateFieldNanosecondResolution() throws IOException {
true,
false,
true,
DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER,
DateFieldMapper.getDefaultDateTimeFormatter(),
DateFieldMapper.Resolution.NANOSECONDS,
null,
Collections.emptyMap()
Expand Down Expand Up @@ -167,7 +167,7 @@ public void testMissingDateWithDateField() throws IOException {
true,
false,
true,
DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER,
DateFieldMapper.getDefaultDateTimeFormatter(),
DateFieldMapper.Resolution.NANOSECONDS,
null,
Collections.emptyMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ public void testSameAggNames() throws IOException {
}

private static long asLong(String dateTime) {
return DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(dateTime)).toInstant().toEpochMilli();
return DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(dateTime)).toInstant().toEpochMilli();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,6 @@ private void executeTestCase(
}

private static long asLong(String dateTime) {
return DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(dateTime)).toInstant().toEpochMilli();
return DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(dateTime)).toInstant().toEpochMilli();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,6 @@ private void executeTestCase(Query query, DateHistogramAggregationBuilder aggBui
}

private static long asLong(String dateTime) {
return DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(dateTime)).toInstant().toEpochMilli();
return DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(dateTime)).toInstant().toEpochMilli();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,6 @@ protected static Map<String, Object> createAfterKey(Object... fields) {
}

protected static long asLong(String dateTime) {
return DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(dateTime)).toInstant().toEpochMilli();
return DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(dateTime)).toInstant().toEpochMilli();
}
}

0 comments on commit f195436

Please sign in to comment.