ARTEMIS-2017 Eliminate LRUCache from SelectorParser
The LRUCache is not thread-safe and it's usage in SelectorParser could cause growth beyond its configured max size. Instead of modifying the LRUCache to be thread-safe or synchronizing access to it in SelectorParser it should just be removed since it's not on a hot path.
This commit is contained in:
parent
01800233c3
commit
2d7c5322a7
|
@ -24,77 +24,61 @@ import org.apache.activemq.artemis.selector.filter.FilterException;
|
||||||
import org.apache.activemq.artemis.selector.hyphenated.HyphenatedParser;
|
import org.apache.activemq.artemis.selector.hyphenated.HyphenatedParser;
|
||||||
import org.apache.activemq.artemis.selector.strict.StrictParser;
|
import org.apache.activemq.artemis.selector.strict.StrictParser;
|
||||||
|
|
||||||
/**
|
|
||||||
*/
|
|
||||||
public class SelectorParser {
|
public class SelectorParser {
|
||||||
|
|
||||||
private static final LRUCache<String, Object> cache = new LRUCache<>(100);
|
|
||||||
private static final String CONVERT_STRING_EXPRESSIONS_PREFIX = "convert_string_expressions:";
|
private static final String CONVERT_STRING_EXPRESSIONS_PREFIX = "convert_string_expressions:";
|
||||||
private static final String HYPHENATED_PROPS_PREFIX = "hyphenated_props:";
|
private static final String HYPHENATED_PROPS_PREFIX = "hyphenated_props:";
|
||||||
private static final String NO_CONVERT_STRING_EXPRESSIONS_PREFIX = "no_convert_string_expressions:";
|
private static final String NO_CONVERT_STRING_EXPRESSIONS_PREFIX = "no_convert_string_expressions:";
|
||||||
private static final String NO_HYPHENATED_PROPS_PREFIX = "no_hyphenated_props:";
|
private static final String NO_HYPHENATED_PROPS_PREFIX = "no_hyphenated_props:";
|
||||||
|
|
||||||
public static BooleanExpression parse(String sql) throws FilterException {
|
public static BooleanExpression parse(String sql) throws FilterException {
|
||||||
Object result = cache.get(sql);
|
String actual = sql;
|
||||||
if (result instanceof FilterException) {
|
boolean convertStringExpressions = false;
|
||||||
throw (FilterException) result;
|
boolean hyphenatedProps = false;
|
||||||
} else if (result instanceof BooleanExpression) {
|
while (true) {
|
||||||
return (BooleanExpression) result;
|
if (actual.startsWith(CONVERT_STRING_EXPRESSIONS_PREFIX)) {
|
||||||
} else {
|
convertStringExpressions = true;
|
||||||
String actual = sql;
|
actual = actual.substring(CONVERT_STRING_EXPRESSIONS_PREFIX.length());
|
||||||
boolean convertStringExpressions = false;
|
continue;
|
||||||
boolean hyphenatedProps = false;
|
|
||||||
while (true) {
|
|
||||||
if (actual.startsWith(CONVERT_STRING_EXPRESSIONS_PREFIX)) {
|
|
||||||
convertStringExpressions = true;
|
|
||||||
actual = actual.substring(CONVERT_STRING_EXPRESSIONS_PREFIX.length());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (actual.startsWith(HYPHENATED_PROPS_PREFIX)) {
|
|
||||||
hyphenatedProps = true;
|
|
||||||
actual = actual.substring(HYPHENATED_PROPS_PREFIX.length());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (actual.startsWith(NO_CONVERT_STRING_EXPRESSIONS_PREFIX)) {
|
|
||||||
convertStringExpressions = false;
|
|
||||||
actual = actual.substring(NO_CONVERT_STRING_EXPRESSIONS_PREFIX.length());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (actual.startsWith(NO_HYPHENATED_PROPS_PREFIX)) {
|
|
||||||
hyphenatedProps = false;
|
|
||||||
actual = actual.substring(NO_HYPHENATED_PROPS_PREFIX.length());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
if (actual.startsWith(HYPHENATED_PROPS_PREFIX)) {
|
||||||
|
hyphenatedProps = true;
|
||||||
|
actual = actual.substring(HYPHENATED_PROPS_PREFIX.length());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (actual.startsWith(NO_CONVERT_STRING_EXPRESSIONS_PREFIX)) {
|
||||||
|
convertStringExpressions = false;
|
||||||
|
actual = actual.substring(NO_CONVERT_STRING_EXPRESSIONS_PREFIX.length());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (actual.startsWith(NO_HYPHENATED_PROPS_PREFIX)) {
|
||||||
|
hyphenatedProps = false;
|
||||||
|
actual = actual.substring(NO_HYPHENATED_PROPS_PREFIX.length());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (convertStringExpressions) {
|
if (convertStringExpressions) {
|
||||||
ComparisonExpression.CONVERT_STRING_EXPRESSIONS.set(true);
|
ComparisonExpression.CONVERT_STRING_EXPRESSIONS.set(true);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
BooleanExpression e = null;
|
||||||
|
if (hyphenatedProps) {
|
||||||
|
HyphenatedParser parser = new HyphenatedParser(new StringReader(actual));
|
||||||
|
e = parser.JmsSelector();
|
||||||
|
} else {
|
||||||
|
StrictParser parser = new StrictParser(new StringReader(actual));
|
||||||
|
e = parser.JmsSelector();
|
||||||
}
|
}
|
||||||
try {
|
return e;
|
||||||
BooleanExpression e = null;
|
} catch (Throwable e) {
|
||||||
if (hyphenatedProps) {
|
FilterException fe = new FilterException(actual, e);
|
||||||
HyphenatedParser parser = new HyphenatedParser(new StringReader(actual));
|
throw fe;
|
||||||
e = parser.JmsSelector();
|
} finally {
|
||||||
} else {
|
if (convertStringExpressions) {
|
||||||
StrictParser parser = new StrictParser(new StringReader(actual));
|
ComparisonExpression.CONVERT_STRING_EXPRESSIONS.remove();
|
||||||
e = parser.JmsSelector();
|
|
||||||
}
|
|
||||||
cache.put(sql, e);
|
|
||||||
return e;
|
|
||||||
} catch (Throwable e) {
|
|
||||||
FilterException fe = new FilterException(actual, e);
|
|
||||||
cache.put(sql, fe);
|
|
||||||
throw fe;
|
|
||||||
} finally {
|
|
||||||
if (convertStringExpressions) {
|
|
||||||
ComparisonExpression.CONVERT_STRING_EXPRESSIONS.remove();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void clearCache() {
|
|
||||||
cache.clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue