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

Merge pull request #952 from katzyn/misc

Optimize arguments for MVMap.init()
......@@ -125,8 +125,7 @@ public class CreateTable extends CommandWithColumns {
try {
session.setUndoLogEnabled(false);
session.startStatementWithinTransaction();
Insert insert = null;
insert = new Insert(session);
Insert insert = new Insert(session);
insert.setSortedInsertMode(sortedInsertMode);
insert.setQuery(asQuery);
insert.setTable(table);
......
......@@ -632,9 +632,7 @@ public final class DataUtils {
c = s.charAt(i++);
if (c == '\\') {
if (i == size) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_FILE_CORRUPT,
"Not a map: {0}", s);
throw newIllegalStateException(ERROR_FILE_CORRUPT, "Not a map: {0}", s);
}
c = s.charAt(i++);
} else if (c == '\"') {
......@@ -663,8 +661,7 @@ public final class DataUtils {
int startKey = i;
i = s.indexOf(':', i);
if (i < 0) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_FILE_CORRUPT, "Not a map: {0}", s);
throw newIllegalStateException(ERROR_FILE_CORRUPT, "Not a map: {0}", s);
}
String key = s.substring(startKey, i++);
i = parseMapValue(buff, s, i, size);
......@@ -696,20 +693,19 @@ public final class DataUtils {
int startKey = i;
i = s.indexOf(':', i);
if (i < 0) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_FILE_CORRUPT, "Not a map: {0}", s);
throw newIllegalStateException(ERROR_FILE_CORRUPT, "Not a map: {0}", s);
}
if (i - startKey == 8 && s.regionMatches(startKey, "fletcher", 0, 8)) {
DataUtils.parseMapValue(buff, s, i + 1, size);
parseMapValue(buff, s, i + 1, size);
int check = (int) Long.parseLong(buff.toString(), 16);
if (check == DataUtils.getFletcher32(bytes, start, startKey - 1)) {
if (check == getFletcher32(bytes, start, startKey - 1)) {
return map;
}
// Corrupted map
return null;
}
String key = s.substring(startKey, i++);
i = DataUtils.parseMapValue(buff, s, i, size);
i = parseMapValue(buff, s, i, size);
map.put(key, buff.toString());
buff.setLength(0);
}
......@@ -725,16 +721,28 @@ public final class DataUtils {
* @throws IllegalStateException if parsing failed
*/
public static String getMapName(String s) {
return getFromMap(s, "name");
}
/**
* Parse a specified pair from key-value pair list.
*
* @param s the list
* @param key the name of the key
* @return value of the specified item, or {@code null}
* @throws IllegalStateException if parsing failed
*/
public static String getFromMap(String s, String key) {
int keyLength = key.length();
for (int i = 0, size = s.length(); i < size;) {
int startKey = i;
i = s.indexOf(':', i);
if (i < 0) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_FILE_CORRUPT, "Not a map: {0}", s);
throw newIllegalStateException(ERROR_FILE_CORRUPT, "Not a map: {0}", s);
}
if (i++ - startKey == 4 && s.regionMatches(startKey, "name", 0, 4)) {
if (i++ - startKey == keyLength && s.regionMatches(startKey, key, 0, keyLength)) {
StringBuilder buff = new StringBuilder();
i = parseMapValue(buff, s, i, size);
parseMapValue(buff, s, i, size);
return buff.toString();
} else {
while (i < size) {
......@@ -745,12 +753,9 @@ public final class DataUtils {
while (i < size) {
c = s.charAt(i++);
if (c == '\\') {
if (i == size) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_FILE_CORRUPT,
"Not a map: {0}", s);
if (i++ == size) {
throw newIllegalStateException(ERROR_FILE_CORRUPT, "Not a map: {0}", s);
}
c = s.charAt(i++);
} else if (c == '\"') {
break;
}
......@@ -1082,8 +1087,7 @@ public final class DataUtils {
@Override
public V setValue(V value) {
throw DataUtils.newUnsupportedOperationException(
"Updating the value is not supported");
throw newUnsupportedOperationException("Updating the value is not supported");
}
}
......
......@@ -8,7 +8,6 @@ package org.h2.mvstore;
import java.util.AbstractList;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
......@@ -96,12 +95,13 @@ public class MVMap<K, V> extends AbstractMap<K, V>
* Open this map with the given store and configuration.
*
* @param store the store
* @param config the configuration
* @param id map id
* @param createVersion version in which this map was created
*/
protected void init(MVStore store, HashMap<String, Object> config) {
protected void init(MVStore store, int id, long createVersion) {
this.store = store;
this.id = DataUtils.readHexInt(config, "id", 0);
this.createVersion = DataUtils.readHexLong(config, "createVersion", 0);
this.id = id;
this.createVersion = createVersion;
this.writeVersion = store.getCurrentVersion();
this.root = Page.createEmpty(this, -1);
}
......@@ -1112,10 +1112,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
MVMap<K, V> openReadOnly() {
MVMap<K, V> m = new MVMap<>(keyType, valueType);
m.readOnly = true;
HashMap<String, Object> config = new HashMap<>();
config.put("id", id);
config.put("createVersion", createVersion);
m.init(store, config);
m.init(store, id, createVersion);
m.root = root;
return m;
}
......
......@@ -336,10 +336,7 @@ public final class MVStore {
(UncaughtExceptionHandler)config.get("backgroundExceptionHandler");
meta = new MVMap<>(StringDataType.INSTANCE,
StringDataType.INSTANCE);
HashMap<String, Object> c = new HashMap<>();
c.put("id", 0);
c.put("createVersion", currentVersion);
meta.init(this, c);
meta.init(this, 0, currentVersion);
if (this.fileStore != null) {
retentionTime = this.fileStore.getDefaultRetentionTime();
int kb = DataUtils.getConfigParam(config, "autoCommitBufferSize", 1024);
......@@ -445,35 +442,29 @@ public final class MVStore {
* @param builder the map builder
* @return the map
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public synchronized <M extends MVMap<K, V>, K, V> M openMap(
String name, MVMap.MapBuilder<M, K, V> builder) {
checkOpen();
String x = meta.get("name." + name);
int id;
long root;
HashMap<String, Object> c;
M map;
if (x != null) {
id = DataUtils.parseHexInt(x);
@SuppressWarnings("unchecked")
M old = (M) maps.get(id);
if (old != null) {
return old;
}
map = builder.create();
String config = meta.get(MVMap.getMapKey(id));
// Cast from HashMap<String, String> to HashMap<String, Object> is safe
c = (HashMap) DataUtils.parseMap(config);
c.put("id", id);
map.init(this, c);
String v = DataUtils.getFromMap(config, "createVersion");
map.init(this, id, v != null ? DataUtils.parseHexLong(v): 0);
root = getRootPos(meta, id);
} else {
c = new HashMap<>();
id = ++lastMapId;
c.put("id", id);
c.put("createVersion", currentVersion);
map = builder.create();
map.init(this, c);
map.init(this, id, currentVersion);
markMetaChanged();
x = Integer.toHexString(id);
meta.put(MVMap.getMapKey(id), map.asString(name));
......
......@@ -6,11 +6,9 @@
package org.h2.test.store;
import java.util.AbstractSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVStore;
/**
* A custom map returning the keys and values values 1 .. 10.
......@@ -31,11 +29,6 @@ public class SequenceMap extends MVMap<Long, Long> {
super(null, null);
}
@Override
public void init(MVStore store, HashMap<String, Object> config) {
super.init(store, config);
}
@Override
public Set<Long> keySet() {
return new AbstractSet<Long>() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论