提交 7ca819d6 authored 作者: Noel Grandin's avatar Noel Grandin

remove StringUtils.fromCacheOrNew

since we require Java7, this is no longer necessary
上级 723cd75d
......@@ -3267,7 +3267,7 @@ public class Parser {
}
i++;
}
currentToken = StringUtils.fromCacheOrNew(sqlCommand.substring(
currentToken = StringUtils.cache(sqlCommand.substring(
start, i));
currentTokenType = getTokenType(currentToken);
parseIndex = i;
......@@ -3290,7 +3290,7 @@ public class Parser {
}
i++;
}
currentToken = StringUtils.fromCacheOrNew(result);
currentToken = StringUtils.cache(result);
parseIndex = i;
currentTokenQuoted = true;
currentTokenType = IDENTIFIER;
......@@ -3386,7 +3386,7 @@ public class Parser {
}
currentToken = "'";
checkLiterals(true);
currentValue = ValueString.get(StringUtils.fromCacheOrNew(result),
currentValue = ValueString.get(StringUtils.cache(result),
database.getMode().treatEmptyStringsAsNull);
parseIndex = i;
currentTokenType = VALUE;
......@@ -3401,7 +3401,7 @@ public class Parser {
result = sqlCommand.substring(begin, i);
currentToken = "'";
checkLiterals(true);
currentValue = ValueString.get(StringUtils.fromCacheOrNew(result),
currentValue = ValueString.get(StringUtils.cache(result),
database.getMode().treatEmptyStringsAsNull);
parseIndex = i;
currentTokenType = VALUE;
......@@ -4785,7 +4785,7 @@ public class Parser {
Query withQuery = parseSelect();
read(")");
withQuery.prepare();
querySQL = StringUtils.fromCacheOrNew(withQuery.getPlanSQL());
querySQL = StringUtils.cache(withQuery.getPlanSQL());
ArrayList<Expression> withExpressions = withQuery.getExpressions();
for (int i = 0; i < cols.length; ++i) {
columnTemplates[i] = new Column(cols[i], withExpressions.get(i).getType());
......@@ -4819,7 +4819,7 @@ public class Parser {
String[] cols = parseColumnList();
command.setColumnNames(cols);
}
String select = StringUtils.fromCacheOrNew(sqlCommand
String select = StringUtils.cache(sqlCommand
.substring(parseIndex));
read("AS");
try {
......
......@@ -9,7 +9,6 @@ import java.lang.ref.SoftReference;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Locale;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.SysProperties;
......@@ -910,47 +909,6 @@ public class StringUtils {
return s;
}
/**
* Get a string from the cache, and if no such string has been found, create
* a new one with only this content. This solves out of memory problems if
* the string is a substring of another, large string. In Java, strings are
* shared, which could lead to memory problems. This avoid such problems.
*
* @param s the string
* @return a string that is guaranteed not be a substring of a large string
*/
public static String fromCacheOrNew(String s) {
if (!SysProperties.OBJECT_CACHE) {
return s;
}
if (s == null) {
return s;
} else if (s.length() == 0) {
return "";
}
int hash = s.hashCode();
String[] cache = getCache();
int index = hash & (SysProperties.OBJECT_CACHE_SIZE - 1);
if (cache == null) {
return s;
}
String cached = cache[index];
if (cached != null) {
if (s.equals(cached)) {
return cached;
}
}
// create a new object that is not shared
// (to avoid out of memory if it is a substring of a big String)
// (not longer needed for Java 7 update 6 and newer,
// but the performance overhead is very small for those
// versions where it is not needed)
// NOPMD
s = new String(s);
cache[index] = s;
return s;
}
/**
* Clear the cache. This method is used for testing.
*/
......
/*
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
* Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, and the
* EPL 1.0 (http://h2database.com/html/license.html). Initial Developer: H2
* Group
*/
package org.h2.test.unit;
import java.util.Locale;
import java.util.Random;
import org.h2.test.TestBase;
import org.h2.util.StringUtils;
......@@ -23,7 +22,6 @@ public class TestStringCache extends TestBase {
private final Random random = new Random(1);
private final String[] some = { null, "", "ABC",
"this is a medium sized string", "1", "2" };
private boolean returnNew;
private boolean useIntern;
/**
......@@ -40,11 +38,6 @@ public class TestStringCache extends TestBase {
@Override
public void test() throws InterruptedException {
testToUpperToLower();
returnNew = true;
StringUtils.clearCache();
testSingleThread(getSize(5000, 20000));
testMultiThreads();
returnNew = false;
StringUtils.clearCache();
testSingleThread(getSize(5000, 20000));
testMultiThreads();
......@@ -105,7 +98,6 @@ public class TestStringCache extends TestBase {
testToUpperCache();
testToUpperCache();
testToUpperCache();
returnNew = false;
for (int i = 0; i < 6; i++) {
useIntern = (i % 2) == 0;
long time = System.currentTimeMillis();
......@@ -124,7 +116,8 @@ public class TestStringCache extends TestBase {
}
return s;
}
int len = random.nextBoolean() ? random.nextInt(1000) : random.nextInt(10);
int len = random.nextBoolean() ? random.nextInt(1000)
: random.nextInt(10);
StringBuilder buff = new StringBuilder(len);
for (int i = 0; i < len; i++) {
buff.append(random.nextInt(0xfff));
......@@ -137,29 +130,16 @@ public class TestStringCache extends TestBase {
*/
void testString() {
String a = randomString();
if (returnNew) {
String b = StringUtils.fromCacheOrNew(a);
try {
assertEquals(a, b);
} catch (Exception e) {
TestBase.logError("error", e);
}
if (a != null && a == b && a.length() > 0) {
throw new AssertionError("a=" + System.identityHashCode(a) +
" b=" + System.identityHashCode(b));
}
String b;
if (useIntern) {
b = a == null ? null : a.intern();
} else {
String b;
if (useIntern) {
b = a == null ? null : a.intern();
} else {
b = StringUtils.cache(a);
}
try {
assertEquals(a, b);
} catch (Exception e) {
TestBase.logError("error", e);
}
b = StringUtils.cache(a);
}
try {
assertEquals(a, b);
} catch (Exception e) {
TestBase.logError("error", e);
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论