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

Merge pull request #1145 from katzyn/misc

Reduce code duplication
...@@ -208,7 +208,10 @@ public class Parser { ...@@ -208,7 +208,10 @@ public class Parser {
new Comparator<TableFilter>() { new Comparator<TableFilter>() {
@Override @Override
public int compare(TableFilter o1, TableFilter o2) { public int compare(TableFilter o1, TableFilter o2) {
return o1 == o2 ? 0 : compareTableFilters(o1, o2); if (o1 == o2)
return 0;
assert o1.getOrderInFrom() != o2.getOrderInFrom();
return o1.getOrderInFrom() > o2.getOrderInFrom() ? 1 : -1;
} }
}; };
...@@ -2160,41 +2163,8 @@ public class Parser { ...@@ -2160,41 +2163,8 @@ public class Parser {
// Parser can reorder joined table filters, need to explicitly sort them // Parser can reorder joined table filters, need to explicitly sort them
// to get the order as it was in the original query. // to get the order as it was in the original query.
if (session.isForceJoinOrder()) { if (session.isForceJoinOrder()) {
sortTableFilters(command.getTopFilters()); Collections.sort(command.getTopFilters(), TABLE_FILTER_COMPARATOR);
}
}
private static void sortTableFilters(ArrayList<TableFilter> filters) {
if (filters.size() < 2) {
return;
}
// Most probably we are already sorted correctly.
boolean sorted = true;
TableFilter prev = filters.get(0);
for (int i = 1; i < filters.size(); i++) {
TableFilter next = filters.get(i);
if (compareTableFilters(prev, next) > 0) {
sorted = false;
break;
}
prev = next;
} }
// If not, then sort manually.
if (!sorted) {
Collections.sort(filters, TABLE_FILTER_COMPARATOR);
}
}
/**
* Find out which of the table filters appears first in the "from" clause.
*
* @param o1 the first table filter
* @param o2 the second table filter
* @return -1 if o1 appears first, and 1 if o2 appears first
*/
static int compareTableFilters(TableFilter o1, TableFilter o2) {
assert o1.getOrderInFrom() != o2.getOrderInFrom();
return o1.getOrderInFrom() > o2.getOrderInFrom() ? 1 : -1;
} }
private void parseJoinTableFilter(TableFilter top, final Select command) { private void parseJoinTableFilter(TableFilter top, final Select command) {
......
...@@ -73,7 +73,6 @@ import org.h2.util.SourceCompiler; ...@@ -73,7 +73,6 @@ import org.h2.util.SourceCompiler;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.TempFileDeleter; import org.h2.util.TempFileDeleter;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.h2.value.CaseInsensitiveConcurrentMap;
import org.h2.value.CaseInsensitiveMap; import org.h2.value.CaseInsensitiveMap;
import org.h2.value.CompareMode; import org.h2.value.CompareMode;
import org.h2.value.NullableKeyConcurrentMap; import org.h2.value.NullableKeyConcurrentMap;
...@@ -2860,9 +2859,7 @@ public class Database implements DataHandler { ...@@ -2860,9 +2859,7 @@ public class Database implements DataHandler {
* @return the hash map * @return the hash map
*/ */
public <V> ConcurrentHashMap<String, V> newConcurrentStringMap() { public <V> ConcurrentHashMap<String, V> newConcurrentStringMap() {
return dbSettings.databaseToUpper ? return new NullableKeyConcurrentMap<>(!dbSettings.databaseToUpper);
new NullableKeyConcurrentMap<V>() :
new CaseInsensitiveConcurrentMap<V>();
} }
/** /**
......
...@@ -121,7 +121,6 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex { ...@@ -121,7 +121,6 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex {
@Override @Override
public void addBufferedRows(List<String> bufferNames) { public void addBufferedRows(List<String> bufferNames) {
ArrayList<String> mapNames = new ArrayList<>(bufferNames);
CompareMode compareMode = database.getCompareMode(); CompareMode compareMode = database.getCompareMode();
int buffersCount = bufferNames.size(); int buffersCount = bufferNames.size();
Queue<Source> queue = new PriorityQueue<>(buffersCount, new Source.Comparator(compareMode)); Queue<Source> queue = new PriorityQueue<>(buffersCount, new Source.Comparator(compareMode));
...@@ -156,7 +155,7 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex { ...@@ -156,7 +155,7 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex {
} }
} }
} finally { } finally {
for (String tempMapName : mapNames) { for (String tempMapName : bufferNames) {
MVMap<ValueArray, Value> map = openMap(tempMapName); MVMap<ValueArray, Value> map = openMap(tempMapName);
map.getStore().removeMap(map); map.getStore().removeMap(map);
} }
......
...@@ -672,10 +672,9 @@ public class Schema extends DbObjectBase { ...@@ -672,10 +672,9 @@ public class Schema extends DbObjectBase {
public void remove(SchemaObject obj) { public void remove(SchemaObject obj) {
String objName = obj.getName(); String objName = obj.getName();
Map<String, SchemaObject> map = getMap(obj.getType()); Map<String, SchemaObject> map = getMap(obj.getType());
if (SysProperties.CHECK && !map.containsKey(objName)) { if (map.remove(objName) == null) {
DbException.throwInternalError("not found: " + objName); DbException.throwInternalError("not found: " + objName);
} }
map.remove(objName);
freeUniqueName(objName); freeUniqueName(objName);
} }
......
/*
* Copyright 2004-2018 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.value;
import java.util.concurrent.ConcurrentHashMap;
import org.h2.util.StringUtils;
/**
* A concurrent hash map with a case-insensitive string key, that also allows
* NULL as a key.
*
* @param <V> the value type
*/
public class CaseInsensitiveConcurrentMap<V> extends ConcurrentHashMap<String, V> {
private static final long serialVersionUID = 1L;
private static final String NULL = new String(new byte[0]);
@Override
public V get(Object key) {
return super.get(toUpper(key));
}
@Override
public V put(String key, V value) {
return super.put(toUpper(key), value);
}
@Override
public boolean containsKey(Object key) {
return super.containsKey(toUpper(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());
}
}
...@@ -7,15 +7,29 @@ package org.h2.value; ...@@ -7,15 +7,29 @@ package org.h2.value;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.h2.util.StringUtils;
/** /**
* A concurrent hash map that allows null keys * A concurrent hash map with string keys that allows null keys.
* *
* @param <V> the value type * @param <V> the value type
*/ */
public class NullableKeyConcurrentMap<V> extends ConcurrentHashMap<String, V> { public class NullableKeyConcurrentMap<V> extends ConcurrentHashMap<String, V> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final String NULL = new String(new byte[0]); 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 @Override
public V get(Object key) { public V get(Object key) {
...@@ -37,8 +51,12 @@ public class NullableKeyConcurrentMap<V> extends ConcurrentHashMap<String, V> { ...@@ -37,8 +51,12 @@ public class NullableKeyConcurrentMap<V> extends ConcurrentHashMap<String, V> {
return super.remove(toUpper(key)); return super.remove(toUpper(key));
} }
private static String toUpper(Object key) { private String toUpper(Object key) {
return key == null ? NULL : key.toString(); if (key == null) {
return NULL;
}
String s = key.toString();
return toUpper ? StringUtils.toUpperEnglish(s) : s;
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论