提交 27a103a2 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not use null keys in maps

上级 8c1c6835
......@@ -107,7 +107,7 @@ public class AlterTableAddConstraint extends SchemaCommand {
}
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
}
if (getSchema().findConstraint(session, constraintName) != null) {
if (constraintName != null && getSchema().findConstraint(session, constraintName) != null) {
if (ifNotExists) {
return 0;
}
......
......@@ -69,7 +69,7 @@ public class CreateIndex extends SchemaCommand {
}
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
}
if (getSchema().findIndex(session, indexName) != null) {
if (indexName != null && getSchema().findIndex(session, indexName) != null) {
if (ifNotExists) {
return 0;
}
......
......@@ -82,7 +82,7 @@ import org.h2.util.TempFileDeleter;
import org.h2.util.Utils;
import org.h2.value.CaseInsensitiveMap;
import org.h2.value.CompareMode;
import org.h2.value.NullableKeyConcurrentMap;
import org.h2.value.CaseInsensitiveConcurrentMap;
import org.h2.value.Value;
import org.h2.value.ValueInt;
......@@ -3041,9 +3041,7 @@ public class Database implements DataHandler {
* @return the hash map
*/
public <V> HashMap<String, V> newStringMap() {
return dbSettings.databaseToUpper ?
new HashMap<String, V>() :
new CaseInsensitiveMap<V>();
return dbSettings.databaseToUpper ? new HashMap<String, V>() : new CaseInsensitiveMap<V>();
}
/**
......@@ -3054,7 +3052,7 @@ public class Database implements DataHandler {
* @return the hash map
*/
public <V> ConcurrentHashMap<String, V> newConcurrentStringMap() {
return new NullableKeyConcurrentMap<>(!dbSettings.databaseToUpper);
return dbSettings.databaseToUpper ? new ConcurrentHashMap<String, V>() : new CaseInsensitiveConcurrentMap<V>();
}
/**
......
......@@ -830,6 +830,9 @@ public class MetaTable extends Table {
final ArrayList<Table> tablesToList;
if (indexFrom != null && indexFrom.equals(indexTo)) {
String tableName = identifier(indexFrom.getString());
if (tableName == null) {
break;
}
tablesToList = getTablesByName(session, tableName);
} else {
tablesToList = getAllTables(session);
......@@ -947,6 +950,9 @@ public class MetaTable extends Table {
final ArrayList<Table> tablesToList;
if (indexFrom != null && indexFrom.equals(indexTo)) {
String tableName = identifier(indexFrom.getString());
if (tableName == null) {
break;
}
tablesToList = getTablesByName(session, tableName);
} else {
tablesToList = getAllTables(session);
......
......@@ -10,53 +10,32 @@ import java.util.concurrent.ConcurrentHashMap;
import org.h2.util.StringUtils;
/**
* A concurrent hash map with string keys that allows null keys.
* A concurrent hash map with case-insensitive string keys.
*
* @param <V> the value type
*/
public class NullableKeyConcurrentMap<V> extends ConcurrentHashMap<String, V> {
public class CaseInsensitiveConcurrentMap<V> extends ConcurrentHashMap<String, V> {
private static final long serialVersionUID = 1L;
private static final String NULL = new String();
private final boolean toUpper;
/**
* Create new instance of map.
*
* @param toUpper
* whether keys should be converted to upper case
*/
public NullableKeyConcurrentMap(boolean toUpper) {
this.toUpper = toUpper;
}
@Override
public V get(Object key) {
return super.get(toUpper(key));
return super.get(StringUtils.toUpperEnglish((String) key));
}
@Override
public V put(String key, V value) {
return super.put(toUpper(key), value);
return super.put(StringUtils.toUpperEnglish(key), value);
}
@Override
public boolean containsKey(Object key) {
return super.containsKey(toUpper(key));
return super.containsKey(StringUtils.toUpperEnglish((String) key));
}
@Override
public V remove(Object key) {
return super.remove(toUpper(key));
}
private String toUpper(Object key) {
if (key == null) {
return NULL;
}
String s = key.toString();
return toUpper ? StringUtils.toUpperEnglish(s) : s;
return super.remove(StringUtils.toUpperEnglish((String) key));
}
}
......@@ -19,26 +19,22 @@ public class CaseInsensitiveMap<V> extends HashMap<String, V> {
@Override
public V get(Object key) {
return super.get(toUpper(key));
return super.get(StringUtils.toUpperEnglish((String) key));
}
@Override
public V put(String key, V value) {
return super.put(toUpper(key), value);
return super.put(StringUtils.toUpperEnglish(key), value);
}
@Override
public boolean containsKey(Object key) {
return super.containsKey(toUpper(key));
return super.containsKey(StringUtils.toUpperEnglish((String) key));
}
@Override
public V remove(Object key) {
return super.remove(toUpper(key));
}
private static String toUpper(Object key) {
return key == null ? null : StringUtils.toUpperEnglish(key.toString());
return super.remove(StringUtils.toUpperEnglish((String) key));
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论