提交 58dec4b5 authored 作者: Thomas Mueller's avatar Thomas Mueller

MVStore: compress is now disabled by default, and can be enabled on request.

上级 e15005f7
...@@ -23,8 +23,8 @@ import org.h2.mvstore.cache.CacheLongKeyLIRS; ...@@ -23,8 +23,8 @@ import org.h2.mvstore.cache.CacheLongKeyLIRS;
import org.h2.mvstore.cache.FilePathCache; import org.h2.mvstore.cache.FilePathCache;
import org.h2.mvstore.type.DataType; import org.h2.mvstore.type.DataType;
import org.h2.mvstore.type.DataTypeFactory; import org.h2.mvstore.type.DataTypeFactory;
import org.h2.mvstore.type.ObjectTypeFactory; import org.h2.mvstore.type.ObjectDataTypeFactory;
import org.h2.mvstore.type.StringType; import org.h2.mvstore.type.StringDataType;
import org.h2.store.fs.FilePath; import org.h2.store.fs.FilePath;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.util.New; import org.h2.util.New;
...@@ -91,6 +91,8 @@ TODO: ...@@ -91,6 +91,8 @@ TODO:
- dump values - dump values
- tool to import / manipulate CSV files (maybe concurrently) - tool to import / manipulate CSV files (maybe concurrently)
- map split / merge (fast if no overlap) - map split / merge (fast if no overlap)
- auto-save if there are too many changes (required for StreamStore)
- StreamStore optimization: avoid copying bytes
*/ */
...@@ -149,6 +151,8 @@ public class MVStore { ...@@ -149,6 +151,8 @@ public class MVStore {
private HashMap<String, String> fileHeader = New.hashMap(); private HashMap<String, String> fileHeader = New.hashMap();
private ByteBuffer writeBuffer;
private final boolean readOnly; private final boolean readOnly;
private int lastMapId; private int lastMapId;
...@@ -157,7 +161,9 @@ public class MVStore { ...@@ -157,7 +161,9 @@ public class MVStore {
private long retainVersion = -1; private long retainVersion = -1;
private int retainChunk = -1; private int retainChunk = -1;
private Compressor compressor; private final Compressor compressor = new CompressLZF();
private final boolean compress;
private long currentVersion; private long currentVersion;
private int fileReadCount; private int fileReadCount;
...@@ -167,7 +173,7 @@ public class MVStore { ...@@ -167,7 +173,7 @@ public class MVStore {
MVStore(HashMap<String, Object> config) { MVStore(HashMap<String, Object> config) {
this.config = config; this.config = config;
this.fileName = (String) config.get("fileName"); this.fileName = (String) config.get("fileName");
DataTypeFactory parent = new ObjectTypeFactory(); DataTypeFactory parent = new ObjectDataTypeFactory();
DataTypeFactory f = (DataTypeFactory) config.get("dataTypeFactory"); DataTypeFactory f = (DataTypeFactory) config.get("dataTypeFactory");
if (f == null) { if (f == null) {
f = parent; f = parent;
...@@ -176,7 +182,7 @@ public class MVStore { ...@@ -176,7 +182,7 @@ public class MVStore {
} }
this.dataTypeFactory = f; this.dataTypeFactory = f;
this.readOnly = "r".equals(config.get("openMode")); this.readOnly = "r".equals(config.get("openMode"));
this.compressor = "0".equals(config.get("compression")) ? null : new CompressLZF(); this.compress = "1".equals(config.get("compress"));
if (fileName != null) { if (fileName != null) {
Object s = config.get("cacheSize"); Object s = config.get("cacheSize");
int mb = s == null ? 16 : Integer.parseInt(s.toString()); int mb = s == null ? 16 : Integer.parseInt(s.toString());
...@@ -343,7 +349,7 @@ public class MVStore { ...@@ -343,7 +349,7 @@ public class MVStore {
private DataType getDataType(Class<?> clazz) { private DataType getDataType(Class<?> clazz) {
if (clazz == String.class) { if (clazz == String.class) {
return StringType.INSTANCE; return StringDataType.INSTANCE;
} }
if (dataTypeFactory == null) { if (dataTypeFactory == null) {
throw new RuntimeException("No data type factory set " + throw new RuntimeException("No data type factory set " +
...@@ -366,7 +372,7 @@ public class MVStore { ...@@ -366,7 +372,7 @@ public class MVStore {
* Open the store. * Open the store.
*/ */
void open() { void open() {
meta = new MVMap<String, String>(StringType.INSTANCE, StringType.INSTANCE); meta = new MVMap<String, String>(StringDataType.INSTANCE, StringDataType.INSTANCE);
HashMap<String, String> c = New.hashMap(); HashMap<String, String> c = New.hashMap();
c.put("id", "0"); c.put("id", "0");
c.put("name", "meta"); c.put("name", "meta");
...@@ -508,7 +514,6 @@ public class MVStore { ...@@ -508,7 +514,6 @@ public class MVStore {
m.close(); m.close();
} }
meta = null; meta = null;
compressor = null;
chunks.clear(); chunks.clear();
cache.clear(); cache.clear();
maps.clear(); maps.clear();
...@@ -599,8 +604,17 @@ public class MVStore { ...@@ -599,8 +604,17 @@ public class MVStore {
} }
} while (freedChunks.size() > 0); } while (freedChunks.size() > 0);
maxLength += meta.getRoot().getMaxLengthTempRecursive(); maxLength += meta.getRoot().getMaxLengthTempRecursive();
ByteBuffer buff;
ByteBuffer buff = ByteBuffer.allocate(maxLength); if (maxLength > 16 * 1024 * 1024) {
buff = ByteBuffer.allocate(maxLength);
} else {
if (writeBuffer != null && writeBuffer.capacity() >= maxLength) {
buff = writeBuffer;
buff.clear();
} else {
writeBuffer = buff = ByteBuffer.allocate(maxLength + 1024 * 1024);
}
}
// need to patch the header later // need to patch the header later
c.writeHeader(buff); c.writeHeader(buff);
c.maxLength = 0; c.maxLength = 0;
...@@ -1008,6 +1022,10 @@ public class MVStore { ...@@ -1008,6 +1022,10 @@ public class MVStore {
return compressor; return compressor;
} }
boolean getCompress() {
return compress;
}
public boolean getReuseSpace() { public boolean getReuseSpace() {
return reuseSpace; return reuseSpace;
} }
......
...@@ -75,6 +75,15 @@ public class MVStoreBuilder { ...@@ -75,6 +75,15 @@ public class MVStoreBuilder {
return set("cacheSize", Integer.toString(mb)); return set("cacheSize", Integer.toString(mb));
} }
/**
* Enable data compression using the LZF algorithm.
*
* @return this
*/
public MVStoreBuilder compressData() {
return set("compress", "1");
}
/** /**
* Use the given data type factory. * Use the given data type factory.
* *
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论