Unverified 提交 ed6735a0 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1147 from katzyn/misc

Assorted minor optimizations
...@@ -415,7 +415,7 @@ public class Insert extends Prepared implements ResultTarget { ...@@ -415,7 +415,7 @@ public class Insert extends Prepared implements ResultTarget {
for (Column column : duplicateKeyAssignmentMap.keySet()) { for (Column column : duplicateKeyAssignmentMap.keySet()) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
Expression ex = duplicateKeyAssignmentMap.get(column); Expression ex = duplicateKeyAssignmentMap.get(column);
buff.append(column.getSQL()).append("=").append(ex.getSQL()); buff.append(column.getSQL()).append('=').append(ex.getSQL());
} }
buff.append(" WHERE "); buff.append(" WHERE ");
Index foundIndex = (Index) de.getSource(); Index foundIndex = (Index) de.getSource();
......
...@@ -1164,7 +1164,7 @@ public class Select extends Query { ...@@ -1164,7 +1164,7 @@ public class Select extends Query {
buff.appendExceptFirst(","); buff.appendExceptFirst(",");
buff.append(c.getName()); buff.append(c.getName());
} }
buff.append(") AS ").append(t.getSQL()).append("\n"); buff.append(") AS ").append(t.getSQL()).append('\n');
} }
} }
} }
......
...@@ -2492,7 +2492,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -2492,7 +2492,7 @@ public class Function extends Expression implements FunctionCall {
StatementBuilder buff = new StatementBuilder(info.name); StatementBuilder buff = new StatementBuilder(info.name);
if (info.type == CASE) { if (info.type == CASE) {
if (args[0] != null) { if (args[0] != null) {
buff.append(" ").append(args[0].getSQL()); buff.append(' ').append(args[0].getSQL());
} }
for (int i = 1, len = args.length - 1; i < len; i += 2) { for (int i = 1, len = args.length - 1; i < len; i += 2) {
buff.append(" WHEN ").append(args[i].getSQL()); buff.append(" WHEN ").append(args[i].getSQL());
......
...@@ -10,11 +10,12 @@ import java.sql.PreparedStatement; ...@@ -10,11 +10,12 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.h2.util.SoftHashMap; import org.h2.util.SoftHashMap;
/** /**
...@@ -45,8 +46,7 @@ final class FullTextSettings { ...@@ -45,8 +46,7 @@ final class FullTextSettings {
/** /**
* The set of indexes in this database. * The set of indexes in this database.
*/ */
private final Map<Integer, IndexInfo> indexes = Collections private final ConcurrentHashMap<Integer, IndexInfo> indexes = new ConcurrentHashMap<>();
.synchronizedMap(new HashMap<Integer, IndexInfo>());
/** /**
* The prepared statement cache. * The prepared statement cache.
......
...@@ -491,8 +491,7 @@ public class PageDataIndex extends PageIndex { ...@@ -491,8 +491,7 @@ public class PageDataIndex extends PageIndex {
Iterator<Row> getDelta() { Iterator<Row> getDelta() {
if (delta == null) { if (delta == null) {
List<Row> e = Collections.emptyList(); return Collections.emptyIterator();
return e.iterator();
} }
return delta.iterator(); return delta.iterator();
} }
......
...@@ -251,8 +251,7 @@ public class ScanIndex extends BaseIndex { ...@@ -251,8 +251,7 @@ public class ScanIndex extends BaseIndex {
Iterator<Row> getDelta() { Iterator<Row> getDelta() {
if (delta == null) { if (delta == null) {
List<Row> e = Collections.emptyList(); return Collections.emptyIterator();
return e.iterator();
} }
return delta.iterator(); return delta.iterator();
} }
......
...@@ -258,7 +258,7 @@ public class MVPrimaryIndex extends BaseIndex { ...@@ -258,7 +258,7 @@ public class MVPrimaryIndex extends BaseIndex {
ValueLong v = (ValueLong) (first ? map.firstKey() : map.lastKey()); ValueLong v = (ValueLong) (first ? map.firstKey() : map.lastKey());
if (v == null) { if (v == null) {
return new MVStoreCursor(session, return new MVStoreCursor(session,
Collections.<Entry<Value, Value>> emptyList().iterator()); Collections.<Entry<Value, Value>> emptyIterator());
} }
Value value = map.get(v); Value value = map.get(v);
Entry<Value, Value> e = new AbstractMap.SimpleImmutableEntry<Value, Value>(v, value); Entry<Value, Value> e = new AbstractMap.SimpleImmutableEntry<Value, Value>(v, value);
......
...@@ -307,7 +307,7 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex { ...@@ -307,7 +307,7 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex {
} }
if (min == null) { if (min == null) {
return new MVStoreCursor(session, return new MVStoreCursor(session,
Collections.<Value>emptyList().iterator(), null); Collections.<Value>emptyIterator(), null);
} }
} }
return new MVStoreCursor(session, map.keyIterator(min), last); return new MVStoreCursor(session, map.keyIterator(min), last);
...@@ -394,7 +394,7 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex { ...@@ -394,7 +394,7 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex {
while (true) { while (true) {
if (key == null) { if (key == null) {
return new MVStoreCursor(session, return new MVStoreCursor(session,
Collections.<Value>emptyList().iterator(), null); Collections.<Value>emptyIterator(), null);
} }
if (((ValueArray) key).getList()[0] != ValueNull.INSTANCE) { if (((ValueArray) key).getList()[0] != ValueNull.INSTANCE) {
break; break;
......
...@@ -21,6 +21,8 @@ import java.util.HashSet; ...@@ -21,6 +21,8 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.h2.Driver; import org.h2.Driver;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.engine.Constants; import org.h2.engine.Constants;
...@@ -49,8 +51,7 @@ public class TcpServer implements Service { ...@@ -49,8 +51,7 @@ public class TcpServer implements Service {
*/ */
private static final String MANAGEMENT_DB_PREFIX = "management_db_"; private static final String MANAGEMENT_DB_PREFIX = "management_db_";
private static final Map<Integer, TcpServer> SERVERS = private static final ConcurrentHashMap<Integer, TcpServer> SERVERS = new ConcurrentHashMap<>();
Collections.synchronizedMap(new HashMap<Integer, TcpServer>());
private int port; private int port;
private boolean portIsSet; private boolean portIsSet;
......
...@@ -9,10 +9,8 @@ import java.io.IOException; ...@@ -9,10 +9,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.concurrent.ConcurrentHashMap;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
/** /**
...@@ -25,7 +23,7 @@ public abstract class FilePath { ...@@ -25,7 +23,7 @@ public abstract class FilePath {
private static FilePath defaultProvider; private static FilePath defaultProvider;
private static Map<String, FilePath> providers; private static ConcurrentHashMap<String, FilePath> providers;
/** /**
* The prefix for temporary files. * The prefix for temporary files.
...@@ -66,8 +64,7 @@ public abstract class FilePath { ...@@ -66,8 +64,7 @@ public abstract class FilePath {
private static void registerDefaultProviders() { private static void registerDefaultProviders() {
if (providers == null || defaultProvider == null) { if (providers == null || defaultProvider == null) {
Map<String, FilePath> map = Collections.synchronizedMap( ConcurrentHashMap<String, FilePath> map = new ConcurrentHashMap<>();
new HashMap<String, FilePath>());
for (String c : new String[] { for (String c : new String[] {
"org.h2.store.fs.FilePathDisk", "org.h2.store.fs.FilePathDisk",
"org.h2.store.fs.FilePathMem", "org.h2.store.fs.FilePathMem",
......
...@@ -5,10 +5,7 @@ ...@@ -5,10 +5,7 @@
*/ */
package org.h2.util; package org.h2.util;
import java.util.Collections; import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
/** /**
...@@ -17,10 +14,8 @@ import java.util.concurrent.atomic.AtomicBoolean; ...@@ -17,10 +14,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class SynchronizedVerifier { public class SynchronizedVerifier {
private static volatile boolean enabled; private static volatile boolean enabled;
private static final Map<Class<?>, AtomicBoolean> DETECT = private static final ConcurrentHashMap<Class<?>, AtomicBoolean> DETECT = new ConcurrentHashMap<>();
Collections.synchronizedMap(new HashMap<Class<?>, AtomicBoolean>()); private static final ConcurrentHashMap<Object, Object> CURRENT = new ConcurrentHashMap<>();
private static final Map<Object, Object> CURRENT =
Collections.synchronizedMap(new IdentityHashMap<>());
/** /**
* Enable or disable detection for a given class. * Enable or disable detection for a given class.
......
...@@ -265,7 +265,7 @@ public class ToDateParser { ...@@ -265,7 +265,7 @@ public class ToDateParser {
while (p.hasToParseData()) { while (p.hasToParseData()) {
List<ToDateTokenizer.FormatTokenEnum> tokenList = List<ToDateTokenizer.FormatTokenEnum> tokenList =
ToDateTokenizer.FormatTokenEnum.getTokensInQuestion(p.getFormatStr()); ToDateTokenizer.FormatTokenEnum.getTokensInQuestion(p.getFormatStr());
if (tokenList.isEmpty()) { if (tokenList == null) {
p.removeFirstChar(); p.removeFirstChar();
continue; continue;
} }
......
...@@ -9,9 +9,7 @@ import static java.lang.String.format; ...@@ -9,9 +9,7 @@ import static java.lang.String.format;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -616,10 +614,9 @@ class ToDateTokenizer { ...@@ -616,10 +614,9 @@ class ToDateTokenizer {
// where "T" and "Z" are inlined // where "T" and "Z" are inlined
INLINE(PARSLET_INLINE, PATTERN_INLINE); INLINE(PARSLET_INLINE, PATTERN_INLINE);
private static final List<FormatTokenEnum> EMPTY_LIST = Collections private static final List<FormatTokenEnum> INLINE_LIST = Collections.singletonList(INLINE);
.emptyList();
private static final Map<Character, List<FormatTokenEnum>> CACHE = new HashMap<>(64); private static List<FormatTokenEnum>[] TOKENS;
private final ToDateParslet toDateParslet; private final ToDateParslet toDateParslet;
private final Pattern patternToUse; private final Pattern patternToUse;
...@@ -648,60 +645,52 @@ class ToDateTokenizer { ...@@ -648,60 +645,52 @@ class ToDateTokenizer {
/** /**
* Optimization: Only return a list of {@link FormatTokenEnum} that * Optimization: Only return a list of {@link FormatTokenEnum} that
* share the same 1st char using the 1st char of the 'to parse' * share the same 1st char using the 1st char of the 'to parse'
* formatStr. Or return empty list if no match. * formatStr. Or return {@code null} if no match.
* *
* @param formatStr the format string * @param formatStr the format string
* @return the list of tokens * @return the list of tokens, or {@code null}
*/ */
static List<FormatTokenEnum> getTokensInQuestion(String formatStr) { static List<FormatTokenEnum> getTokensInQuestion(String formatStr) {
List<FormatTokenEnum> result = EMPTY_LIST;
if (CACHE.size() <= 0) {
initCache();
}
if (formatStr != null && formatStr.length() > 0) { if (formatStr != null && formatStr.length() > 0) {
Character key = Character.toUpperCase(formatStr.charAt(0)); char key = Character.toUpperCase(formatStr.charAt(0));
switch (key) { if (key >= 'A' && key <= 'Y') {
case '"': List<FormatTokenEnum>[] tokens = TOKENS;
result = new ArrayList<>(); if (tokens == null) {
result.add(INLINE); tokens = initTokens();
break;
default:
result = CACHE.get(key);
} }
return tokens[key - 'A'];
} else if (key == '"') {
return INLINE_LIST;
} }
if (result == null) {
result = EMPTY_LIST;
} }
return result; return null;
} }
private static synchronized void initCache() { @SuppressWarnings("unchecked")
if (CACHE.size() <= 0) { private static List<FormatTokenEnum>[] initTokens() {
List<FormatTokenEnum>[] tokens = new List[25];
for (FormatTokenEnum token : FormatTokenEnum.values()) { for (FormatTokenEnum token : FormatTokenEnum.values()) {
String name = token.name();
List<Character> tokenKeys = new ArrayList<>(); if (name.indexOf('_') >= 0) {
for (String tokenLets : name.split("_")) {
if (token.name().contains("_")) { putToCache(tokens, token, tokenLets);
String[] tokens = token.name().split("_");
for (String tokenLets : tokens) {
tokenKeys.add(tokenLets.toUpperCase().charAt(0));
} }
} else { } else {
tokenKeys.add(token.name().toUpperCase().charAt(0)); putToCache(tokens, token, name);
}
}
return TOKENS = tokens;
} }
for (Character tokenKey : tokenKeys) { private static void putToCache(List<FormatTokenEnum>[] cache, FormatTokenEnum token, String name) {
List<FormatTokenEnum> l = CACHE.get(tokenKey); int idx = Character.toUpperCase(name.charAt(0)) - 'A';
List<FormatTokenEnum> l = cache[idx];
if (l == null) { if (l == null) {
l = new ArrayList<>(1); l = new ArrayList<>(1);
CACHE.put(tokenKey, l); cache[idx] = l;
} }
l.add(token); l.add(token);
} }
}
}
}
/** /**
* Parse the format-string with passed token of {@link FormatTokenEnum}. * Parse the format-string with passed token of {@link FormatTokenEnum}.
......
...@@ -10,9 +10,7 @@ import java.sql.DriverManager; ...@@ -10,9 +10,7 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Collections; import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap;
import java.util.Map;
/** /**
* This sample application shows how to cache prepared statements. * This sample application shows how to cache prepared statements.
...@@ -21,9 +19,7 @@ public class CachedPreparedStatements { ...@@ -21,9 +19,7 @@ public class CachedPreparedStatements {
private Connection conn; private Connection conn;
private Statement stat; private Statement stat;
private final Map<String, PreparedStatement> prepared = private final ConcurrentHashMap<String, PreparedStatement> prepared = new ConcurrentHashMap<>();
Collections.synchronizedMap(
new HashMap<String, PreparedStatement>());
/** /**
* This method is called when executing this sample application from the * This method is called when executing this sample application from the
......
...@@ -10,9 +10,8 @@ import java.sql.DriverManager; ...@@ -10,9 +10,8 @@ import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Collections; import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap;
import java.util.Map;
import org.h2.api.Trigger; import org.h2.api.Trigger;
/** /**
...@@ -21,8 +20,7 @@ import org.h2.api.Trigger; ...@@ -21,8 +20,7 @@ import org.h2.api.Trigger;
*/ */
public class TriggerPassData implements Trigger { public class TriggerPassData implements Trigger {
private static final Map<String, TriggerPassData> TRIGGERS = private static final ConcurrentHashMap<String, TriggerPassData> TRIGGERS = new ConcurrentHashMap<>();
Collections.synchronizedMap(new HashMap<String, TriggerPassData>());
private String triggerData; private String triggerData;
/** /**
......
...@@ -117,11 +117,16 @@ public class TestFuzzOptimizations extends TestBase { ...@@ -117,11 +117,16 @@ public class TestFuzzOptimizations extends TestBase {
} }
executeAndCompare(condition, params, message); executeAndCompare(condition, params, message);
} }
if (!config.mvStore) { if (!config.mvStore && config.travis) {
/* /*
* Travis tests have problems with automatic garbage collection, so request a GC * Travis tests have problems with automatic garbage collection, so request a GC
* explicitly and print its results. * explicitly and print its results.
*/ */
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
println((Utils.getMemoryUsed() >> 10) + " MiB used"); println((Utils.getMemoryUsed() >> 10) + " MiB used");
} }
} }
......
...@@ -98,12 +98,12 @@ public interface SQLDialect { ...@@ -98,12 +98,12 @@ public interface SQLDialect {
buff.append(index.indexName); buff.append(index.indexName);
buff.append(" ON "); buff.append(" ON ");
buff.append(table); buff.append(table);
buff.append("("); buff.append('(');
for (String col : index.columnNames) { for (String col : index.columnNames) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(col); buff.append(col);
} }
buff.append(")"); buff.append(')');
return buff.toString(); return buff.toString();
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论