SCViewer/plugins/com.minres.scviewer.databas.../src/com/minres/scviewer/database/leveldb/TxDBWrapper.java

90 lines
2.4 KiB
Java
Raw Normal View History

2018-11-03 17:48:36 +01:00
package com.minres.scviewer.database.leveldb;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.File;
import java.io.IOException;
import org.iq80.leveldb.Options;
import org.iq80.leveldb.Range;
import org.iq80.leveldb.ReadOptions;
import org.iq80.leveldb.Snapshot;
import org.iq80.leveldb.impl.DbImpl;
import org.iq80.leveldb.impl.SeekingIterator;
class TxDBWrapper {
2018-11-03 17:48:36 +01:00
private final Options options;
private final ReadOptions ro = new ReadOptions();
2018-11-03 17:48:36 +01:00
private final File databaseDir;
private DbImpl db;
private long timeResolution=1L;;
2018-11-03 17:48:36 +01:00
TxDBWrapper(Options options, File databaseDir) throws IOException {
this.options = options.verifyChecksums(true).createIfMissing(false).errorIfExists(false).cacheSize(64*1024*1024);
2018-11-03 17:48:36 +01:00
this.databaseDir = databaseDir;
this.db = new DbImpl(options, databaseDir);
ro.snapshot(db.getSnapshot());
2018-11-03 17:48:36 +01:00
}
public String get(String key) {
byte[] slice = db.get(LevelDBLoader.toByteArray(key));
if (slice == null) {
return null;
}
return new String(slice, UTF_8);
}
public String get(String key, Snapshot snapshot) {
byte[] slice = db.get(LevelDBLoader.toByteArray(key), ro);
return slice == null? null : new String(slice, UTF_8);
2018-11-03 17:48:36 +01:00
}
public void put(String key, String value) {
db.put(LevelDBLoader.toByteArray(key), LevelDBLoader.toByteArray(value));
}
public void delete(String key) {
db.delete(LevelDBLoader.toByteArray(key));
}
public SeekingIterator<String, String> iterator() {
return new StringDbIterator(db.iterator());
}
public Snapshot getSnapshot() {
return db.getSnapshot();
}
public void close() {
try {
ro.snapshot().close();
db.close();
} catch (IOException e) {} // ignore any error
2018-11-03 17:48:36 +01:00
}
public long size(String start, String limit) {
return db.getApproximateSizes(new Range(LevelDBLoader.toByteArray(start), LevelDBLoader.toByteArray(limit)));
}
public long getMaxNextLevelOverlappingBytes() {
return db.getMaxNextLevelOverlappingBytes();
}
public void reopen() throws IOException {
reopen(options);
}
public void reopen(Options options) throws IOException {
this.close();
2018-11-03 17:48:36 +01:00
db = new DbImpl(options.verifyChecksums(true).createIfMissing(false).errorIfExists(false), databaseDir);
ro.snapshot(db.getSnapshot());
}
public long getTimeResolution() {
return timeResolution;
}
public void setTimeResolution(long resolution) {
this.timeResolution = resolution;
2018-11-03 17:48:36 +01:00
}
}