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