Commit c6cc3314 by Ashutosh Mestry Committed by apoorvnaik

ATLAS-2229: Added thread-safety for SimpleDateFormat.

ATLAS-2229: Modified approach for detecting keywords. Removed unused keywords. Signed-off-by: 's avatarapoorvnaik <apoorvnaik@apache.org>
parent 5c925920
......@@ -68,6 +68,7 @@ import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.*;
import java.util.stream.Stream;
import static org.apache.atlas.AtlasErrorCode.CLASSIFICATION_NOT_FOUND;
import static org.apache.atlas.AtlasErrorCode.DISCOVERY_QUERY_FAILED;
......@@ -897,9 +898,7 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
@Override
public String getDslQueryUsingTypeNameClassification(String query, String typeName, String classification) {
final String whereDSLKeyword = "where";
final String isaDSLKeyword = "isa";
final String isDSLKeyword = "is";
final String limitDSLKeyword = "limit";
final String[] keywords = new String[]{whereDSLKeyword, "isa", "is", "limit", "orderby", "has"};
final String whereFormat = whereDSLKeyword + " %s";
String queryStr = query == null ? "" : query;
......@@ -907,10 +906,7 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
if (StringUtils.isNotEmpty(typeName)) {
if(StringUtils.isNotEmpty(query)) {
String s = query.toLowerCase();
if(!s.startsWith(whereDSLKeyword) &&
!s.startsWith(limitDSLKeyword) &&
!s.startsWith(isaDSLKeyword) &&
!s.startsWith(isDSLKeyword)) {
if(!Stream.of(keywords).anyMatch(x -> s.startsWith(x))) {
queryStr = String.format(whereFormat, query);
}
}
......@@ -921,7 +917,7 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
if (StringUtils.isNotEmpty(classification)) {
// isa works with a type name only - like hive_column isa PII; it doesn't work with more complex query
if (StringUtils.isEmpty(query)) {
queryStr += String.format("%s %s %s", queryStr, isaDSLKeyword, classification);
queryStr += String.format("%s %s %s", queryStr, "isa", classification);
}
}
return queryStr;
......
......@@ -38,7 +38,7 @@ import java.util.stream.Stream;
public class GremlinQueryComposer {
private static final Logger LOG = LoggerFactory.getLogger(GremlinQueryComposer.class);
private final String DATE_FORMAT_ISO8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
private static final String ISO8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
private final int DEFAULT_QUERY_RESULT_LIMIT = 25;
private final int DEFAULT_QUERY_RESULT_OFFSET = 0;
......@@ -50,13 +50,22 @@ public class GremlinQueryComposer {
private int providedLimit = DEFAULT_QUERY_RESULT_LIMIT;
private int providedOffset = DEFAULT_QUERY_RESULT_OFFSET;
private Context context;
private final DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_ISO8601_FORMAT);
private static final ThreadLocal<DateFormat> DSL_DATE_FORMAT = new ThreadLocal<DateFormat>() {
@Override
public DateFormat initialValue() {
DateFormat ret = new SimpleDateFormat(ISO8601_FORMAT);
ret.setTimeZone(TimeZone.getTimeZone("UTC"));
return ret;
}
};
public GremlinQueryComposer(Lookup registryLookup, final AtlasDSL.QueryMetadata qmd, boolean isNestedQuery) {
this.isNestedQuery = isNestedQuery;
this.lookup = registryLookup;
this.queryMetadata = qmd;
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
init();
}
......@@ -74,7 +83,6 @@ public class GremlinQueryComposer {
this.lookup = lookup;
this.context = context;
this.queryMetadata = qmd;
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
init();
}
......@@ -351,7 +359,7 @@ public class GremlinQueryComposer {
public long getDateFormat(String s) {
try {
return dateFormat.parse(s).getTime();
return DSL_DATE_FORMAT.get().parse(s).getTime();
} catch (ParseException ex) {
errorList.add(ex.getMessage());
}
......
......@@ -121,8 +121,6 @@ K_SUM: S U M ;
K_COUNT: C O U N T ;
K_LOOP: L O O P ;
K_OFFSET: O F F S E T ;
K_AS: A S ;
......@@ -137,8 +135,6 @@ K_ASC: A S C ;
K_DESC: D E S C ;
K_WITHPATH: W I T H P A T H ;
K_TRUE: T R U E ;
K_FALSE: F A L S E ;
......@@ -158,12 +154,10 @@ KEYWORD: K_LIKE
| K_OR
| K_GROUPBY
| K_ORDERBY
| K_WITHPATH
| K_SUM
| K_MIN
| K_MAX
| K_OFFSET
| K_LOOP
| K_FROM
| K_DESC
| K_ASC
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment