Skip to content

Commit

Permalink
Use FastDateFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
vernedeng committed Apr 23, 2024
1 parent c7c46b0 commit ccaa8b2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,17 @@

package org.apache.inlong.common.pojo.sort.dataflow.field.format;

import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static org.apache.inlong.common.pojo.sort.dataflow.field.format.Constants.DATE_AND_TIME_STANDARD_ISO_8601;
import static org.apache.inlong.common.pojo.sort.dataflow.field.format.Constants.DATE_AND_TIME_STANDARD_SQL;

/**
* The format information for {@link Date}s.
*/
Expand All @@ -46,28 +41,10 @@ public class DateFormatInfo implements BasicFormatInfo<Date> {
@Nonnull
private final String format;

@JsonIgnore
@Nullable
private final SimpleDateFormat simpleDateFormat;

@JsonIgnore
@Nullable
private final ThreadLocal<SimpleDateFormat> threadLocal;

@JsonCreator
public DateFormatInfo(
@JsonProperty(FIELD_FORMAT) @Nonnull String format) {
this.format = format;
this.threadLocal = new ThreadLocal<>();
if (!format.equals("SECONDS")
&& !format.equals("MILLIS")
&& !format.equals("MICROS")
&& !DATE_AND_TIME_STANDARD_SQL.equals(format)
&& !DATE_AND_TIME_STANDARD_ISO_8601.equals(format)) {
this.simpleDateFormat = new SimpleDateFormat(format);
} else {
this.simpleDateFormat = null;
}
}

public DateFormatInfo() {
Expand All @@ -84,17 +61,6 @@ public DateTypeInfo getTypeInfo() {
return DateTypeInfo.INSTANCE;
}

private SimpleDateFormat get() {
if (simpleDateFormat == null) {
throw new IllegalStateException();
}
SimpleDateFormat local = threadLocal.get();
if (local == null) {
threadLocal.set((SimpleDateFormat) simpleDateFormat.clone());
}
return threadLocal.get();
}

@Override
public String serialize(Date date) {
switch (format) {
Expand All @@ -113,7 +79,7 @@ public String serialize(Date date) {
return Long.toString(seconds);
}
default: {
return get().format(date);
return FastDateFormat.getInstance(format).format(date.getTime());
}
}
}
Expand All @@ -137,8 +103,8 @@ public Date deserialize(String text) throws ParseException {
return new Date(millis);
}
default: {
java.util.Date jDate = get().parse(text.trim());
return new Date(jDate.getTime());
java.util.Date date = FastDateFormat.getInstance(format).parse(text.trim());
return new Date(date.getTime());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,18 @@

package org.apache.inlong.common.pojo.sort.dataflow.field.format;

import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static org.apache.inlong.common.pojo.sort.dataflow.field.format.Constants.DATE_AND_TIME_STANDARD_ISO_8601;
import static org.apache.inlong.common.pojo.sort.dataflow.field.format.Constants.DATE_AND_TIME_STANDARD_SQL;

/**
* The format information for {@link Time}s.
*/
Expand All @@ -47,12 +42,6 @@ public class TimeFormatInfo implements BasicFormatInfo<Time> {
@JsonProperty(FIELD_FORMAT)
@Nonnull
private final String format;
@JsonIgnore
@Nullable
private final SimpleDateFormat simpleDateFormat;
@JsonIgnore
@Nullable
private final ThreadLocal<SimpleDateFormat> threadLocal;
@JsonProperty("precision")
private int precision;

Expand All @@ -62,16 +51,6 @@ public TimeFormatInfo(
@JsonProperty("precision") int precision) {
this.format = format;
this.precision = precision;
this.threadLocal = new ThreadLocal<>();
if (!format.equals("MICROS")
&& !format.equals("MILLIS")
&& !format.equals("SECONDS")
&& !DATE_AND_TIME_STANDARD_SQL.equals(format)
&& !DATE_AND_TIME_STANDARD_ISO_8601.equals(format)) {
this.simpleDateFormat = new SimpleDateFormat(format);
} else {
this.simpleDateFormat = null;
}
}

public TimeFormatInfo(@JsonProperty(FIELD_FORMAT) @Nonnull String format) {
Expand All @@ -82,17 +61,6 @@ public TimeFormatInfo() {
this("HH:mm:ss", DEFAULT_PRECISION_FOR_TIMESTAMP);
}

private SimpleDateFormat get() {
if (simpleDateFormat == null) {
throw new IllegalStateException();
}
SimpleDateFormat local = threadLocal.get();
if (local == null) {
threadLocal.set((SimpleDateFormat) simpleDateFormat.clone());
}
return threadLocal.get();
}

@Nonnull
public String getFormat() {
return format;
Expand Down Expand Up @@ -121,7 +89,7 @@ public String serialize(Time time) {
return Long.toString(seconds);
}
default: {
return get().format(time);
return FastDateFormat.getInstance(format).format(time.getTime());
}
}
}
Expand All @@ -144,7 +112,7 @@ public Time deserialize(String text) throws ParseException {
return new Time(millis);
}
default: {
Date date = get().parse(text);
Date date = FastDateFormat.getInstance(format).parse(text);
return new Time(date.getTime());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,18 @@

package org.apache.inlong.common.pojo.sort.dataflow.field.format;

import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static org.apache.inlong.common.pojo.sort.dataflow.field.format.Constants.DATE_AND_TIME_STANDARD_ISO_8601;
import static org.apache.inlong.common.pojo.sort.dataflow.field.format.Constants.DATE_AND_TIME_STANDARD_SQL;

/**
* The format information for {@link Timestamp}s.
*/
Expand All @@ -49,14 +44,6 @@ public class TimestampFormatInfo implements BasicFormatInfo<Timestamp> {
@Nonnull
private final String format;

@JsonIgnore
@Nullable
private final SimpleDateFormat simpleDateFormat;

@JsonIgnore
@Nullable
private final ThreadLocal<SimpleDateFormat> threadLocal;

@JsonProperty("precision")
private int precision;

Expand All @@ -66,16 +53,6 @@ public TimestampFormatInfo(
@JsonProperty("precision") int precision) {
this.format = format;
this.precision = precision;
this.threadLocal = new ThreadLocal<>();
if (!format.equals("MICROS")
&& !format.equals("MILLIS")
&& !format.equals("SECONDS")
&& !DATE_AND_TIME_STANDARD_SQL.equals(format)
&& !DATE_AND_TIME_STANDARD_ISO_8601.equals(format)) {
this.simpleDateFormat = new SimpleDateFormat(format);
} else {
this.simpleDateFormat = null;
}
}

public TimestampFormatInfo(@JsonProperty(FIELD_FORMAT) @Nonnull String format) {
Expand All @@ -90,17 +67,6 @@ public TimestampFormatInfo(@JsonProperty("precision") int precision) {
this("yyyy-MM-dd HH:mm:ss", precision);
}

private SimpleDateFormat get() {
if (simpleDateFormat == null) {
throw new IllegalStateException();
}
SimpleDateFormat local = threadLocal.get();
if (local == null) {
threadLocal.set((SimpleDateFormat) simpleDateFormat.clone());
}
return threadLocal.get();
}

@Nonnull
public String getFormat() {
return format;
Expand Down Expand Up @@ -130,7 +96,7 @@ public String serialize(Timestamp timestamp) {
return Long.toString(seconds);
}
default: {
return get().format(timestamp);
return FastDateFormat.getInstance(format).format(timestamp.getTime());
}
}
}
Expand All @@ -153,7 +119,7 @@ public Timestamp deserialize(String text) throws ParseException {
return new Timestamp(millis);
}
default: {
Date date = get().parse(text);
Date date = FastDateFormat.getInstance(format).parse(text);
return new Timestamp(date.getTime());
}
}
Expand Down

0 comments on commit ccaa8b2

Please sign in to comment.