提交 7ad501b1 authored 作者: Thomas Mueller's avatar Thomas Mueller

A bit faster.

上级 d5bb9cb7
...@@ -44,6 +44,7 @@ public class CompareLike extends Condition { ...@@ -44,6 +44,7 @@ public class CompareLike extends Condition {
private int[] types; private int[] types;
private int patternLength; private int patternLength;
private boolean ignoreCase; private boolean ignoreCase;
private boolean fastCompare;
public CompareLike(CompareMode compareMode, Expression left, Expression right, Expression escape, boolean regexp) { public CompareLike(CompareMode compareMode, Expression left, Expression right, Expression escape, boolean regexp) {
this.compareMode = compareMode; this.compareMode = compareMode;
...@@ -221,22 +222,20 @@ public class CompareLike extends Condition { ...@@ -221,22 +222,20 @@ public class CompareLike extends Condition {
// result = patternRegexp.matcher(value).matches(); // result = patternRegexp.matcher(value).matches();
result = patternRegexp.matcher(value).find(); result = patternRegexp.matcher(value).find();
} else { } else {
result = compareAt(value, 0, 0, value.length()); result = compareAt(value, 0, 0, value.length(), pattern, types);
} }
return ValueBoolean.get(result); return ValueBoolean.get(result);
} }
private boolean compare(String s, int pi, int si) { private boolean compare(char[] pattern, String s, int pi, int si) {
// TODO check if this is correct according to Unicode rules (code points) return pattern[pi] == s.charAt(si) || (!fastCompare && compareMode.equalsChars(patternString, pi, s, si, ignoreCase));
return compareMode.equalsChars(patternString, pi, s, si, ignoreCase);
} }
private boolean compareAt(String s, int pi, int si, int sLen) { private boolean compareAt(String s, int pi, int si, int sLen, char[] pattern, int[] types) {
for (; pi < patternLength; pi++) { for (; pi < patternLength; pi++) {
int type = types[pi]; switch (types[pi]) {
switch (type) {
case MATCH: case MATCH:
if ((si >= sLen) || !compare(s, pi, si++)) { if ((si >= sLen) || !compare(pattern, s, pi, si++)) {
return false; return false;
} }
break; break;
...@@ -250,14 +249,14 @@ public class CompareLike extends Condition { ...@@ -250,14 +249,14 @@ public class CompareLike extends Condition {
return true; return true;
} }
while (si < sLen) { while (si < sLen) {
if (compare(s, pi, si) && compareAt(s, pi, si, sLen)) { if (compare(pattern, s, pi, si) && compareAt(s, pi, si, sLen, pattern, types)) {
return true; return true;
} }
si++; si++;
} }
return false; return false;
default: default:
Message.throwInternalError("type=" + type); Message.throwInternalError();
} }
} }
return si == sLen; return si == sLen;
...@@ -273,10 +272,13 @@ public class CompareLike extends Condition { ...@@ -273,10 +272,13 @@ public class CompareLike extends Condition {
*/ */
public boolean test(String testPattern, String value, char escapeChar) throws SQLException { public boolean test(String testPattern, String value, char escapeChar) throws SQLException {
initPattern(testPattern, escapeChar); initPattern(testPattern, escapeChar);
return compareAt(value, 0, 0, value.length()); return compareAt(value, 0, 0, value.length(), pattern, types);
} }
private void initPattern(String p, Character escape) throws SQLException { private void initPattern(String p, Character escape) throws SQLException {
if (compareMode.getName().equals(CompareMode.OFF) && !ignoreCase) {
fastCompare = true;
}
if (regexp) { if (regexp) {
patternString = p; patternString = p;
try { try {
...@@ -286,7 +288,7 @@ public class CompareLike extends Condition { ...@@ -286,7 +288,7 @@ public class CompareLike extends Condition {
patternRegexp = Pattern.compile(p); patternRegexp = Pattern.compile(p);
} }
} catch (PatternSyntaxException e) { } catch (PatternSyntaxException e) {
throw Message.getSQLException(ErrorCode.LIKE_ESCAPE_ERROR_1, new String[]{p}, e); throw Message.getSQLException(ErrorCode.LIKE_ESCAPE_ERROR_1, e, p);
} }
return; return;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论