Skip to content

Use thread safe DateTimeFormatter instead of SimpleDateFormat #4172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package org.springframework.ai.vectorstore.filter.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Pattern;

import org.springframework.ai.vectorstore.filter.Filter;
Expand All @@ -36,11 +36,10 @@ public class SimpleVectorStoreFilterExpressionConverter extends AbstractFilterEx

private static final Pattern DATE_FORMAT_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z");

private final SimpleDateFormat dateFormat;
private final DateTimeFormatter dateFormat;

public SimpleVectorStoreFilterExpressionConverter() {
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneOffset.UTC);
}

@Override
Expand Down Expand Up @@ -113,17 +112,17 @@ private void appendSpELContains(StringBuilder formattedList, StringBuilder conte
protected void doSingleValue(Object value, StringBuilder context) {
if (value instanceof Date date) {
context.append("'");
context.append(this.dateFormat.format(date));
context.append(this.dateFormat.format(date.toInstant()));
context.append("'");
}
else if (value instanceof String text) {
context.append("'");
if (DATE_FORMAT_PATTERN.matcher(text).matches()) {
try {
Date date = this.dateFormat.parse(text);
var date = this.dateFormat.parse(text);
context.append(this.dateFormat.format(date));
}
catch (ParseException e) {
catch (DateTimeParseException e) {
throw new IllegalArgumentException("Invalid date type:" + text, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package org.springframework.ai.vectorstore.azure;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Pattern;

import org.springframework.ai.vectorstore.azure.AzureVectorStore.MetadataField;
Expand All @@ -40,18 +40,17 @@
*/
public class AzureAiSearchFilterExpressionConverter extends AbstractFilterExpressionConverter {

private static Pattern DATE_FORMAT_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z");
private static final Pattern DATE_FORMAT_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z");

private final SimpleDateFormat dateFormat;
private final DateTimeFormatter dateFormat;

private List<String> allowedIdentifierNames;

public AzureAiSearchFilterExpressionConverter(List<MetadataField> filterMetadataFields) {
Assert.notNull(filterMetadataFields, "The filterMetadataFields can not null.");

this.allowedIdentifierNames = filterMetadataFields.stream().map(MetadataField::name).toList();
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneOffset.UTC);
}

@Override
Expand Down Expand Up @@ -137,15 +136,15 @@ protected void doValue(Filter.Value filterValue, StringBuilder context) {
@Override
protected void doSingleValue(Object value, StringBuilder context) {
if (value instanceof Date date) {
context.append(this.dateFormat.format(date));
context.append(this.dateFormat.format(date.toInstant()));
}
else if (value instanceof String text) {
if (DATE_FORMAT_PATTERN.matcher(text).matches()) {
try {
Date date = this.dateFormat.parse(text);
var date = this.dateFormat.parse(text);
context.append(this.dateFormat.format(date));
}
catch (ParseException e) {
catch (DateTimeParseException e) {
throw new IllegalArgumentException("Invalid date type:" + text, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package org.springframework.ai.vectorstore.elasticsearch;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Pattern;

import org.springframework.ai.vectorstore.filter.Filter;
Expand All @@ -40,11 +40,10 @@ public class ElasticsearchAiSearchFilterExpressionConverter extends AbstractFilt

private static final Pattern DATE_FORMAT_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z");

private final SimpleDateFormat dateFormat;
private final DateTimeFormatter dateFormat;

public ElasticsearchAiSearchFilterExpressionConverter() {
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneOffset.UTC);
}

@Override
Expand Down Expand Up @@ -121,15 +120,15 @@ protected void doValue(Filter.Value filterValue, StringBuilder context) {
@Override
protected void doSingleValue(Object value, StringBuilder context) {
if (value instanceof Date date) {
context.append(this.dateFormat.format(date));
context.append(this.dateFormat.format(date.toInstant()));
}
else if (value instanceof String text) {
if (DATE_FORMAT_PATTERN.matcher(text).matches()) {
try {
Date date = this.dateFormat.parse(text);
var date = this.dateFormat.parse(text);
context.append(this.dateFormat.format(date));
}
catch (ParseException e) {
catch (DateTimeParseException e) {
throw new IllegalArgumentException("Invalid date type:" + text, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -49,6 +50,18 @@ public void testDate() {
assertThat(vectorExpr).isEqualTo("metadata.activationDate:1970-01-01T00:00:02Z");
}

@Test
public void testDatesConcurrently() {
IntStream.range(0, 10).parallel().forEach(i -> {
String vectorExpr = this.converter.convertExpression(new Filter.Expression(EQ,
new Filter.Key("activationDate"), new Filter.Value(new Date(1704637752148L))));
String vectorExpr2 = this.converter.convertExpression(new Filter.Expression(EQ,
new Filter.Key("activationDate"), new Filter.Value(new Date(1704637753150L))));
assertThat(vectorExpr).isEqualTo("metadata.activationDate:2024-01-07T14:29:12Z");
assertThat(vectorExpr2).isEqualTo("metadata.activationDate:2024-01-07T14:29:13Z");
});
}

@Test
public void testEQ() {
String vectorExpr = this.converter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package org.springframework.ai.vectorstore.opensearch;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Pattern;

import org.springframework.ai.vectorstore.filter.Filter;
Expand All @@ -38,11 +38,10 @@ public class OpenSearchAiSearchFilterExpressionConverter extends AbstractFilterE

private static final Pattern DATE_FORMAT_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z");

private final SimpleDateFormat dateFormat;
private final DateTimeFormatter dateFormat;

public OpenSearchAiSearchFilterExpressionConverter() {
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
this.dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(ZoneOffset.UTC);
}

@Override
Expand Down Expand Up @@ -119,15 +118,15 @@ protected void doValue(Filter.Value filterValue, StringBuilder context) {
@Override
protected void doSingleValue(Object value, StringBuilder context) {
if (value instanceof Date date) {
context.append(this.dateFormat.format(date));
context.append(this.dateFormat.format(date.toInstant()));
}
else if (value instanceof String text) {
if (DATE_FORMAT_PATTERN.matcher(text).matches()) {
try {
Date date = this.dateFormat.parse(text);
var date = this.dateFormat.parse(text);
context.append(this.dateFormat.format(date));
}
catch (ParseException e) {
catch (DateTimeParseException e) {
throw new IllegalArgumentException("Invalid date type:" + text, e);
}
}
Expand Down