use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestOutOfMemory method testMVStoreUsingInMemoryFileSystem.
private void testMVStoreUsingInMemoryFileSystem() {
FilePath.register(new FilePathMem());
String fileName = "memFS:" + getTestName();
final AtomicReference<Throwable> exRef = new AtomicReference<>();
MVStore store = new MVStore.Builder().fileName(fileName).backgroundExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
exRef.compareAndSet(null, e);
}
}).open();
try {
Map<Integer, byte[]> map = store.openMap("test");
Random r = new Random(1);
try {
for (int i = 0; i < 100; i++) {
byte[] data = new byte[10 * 1024 * 1024];
r.nextBytes(data);
map.put(i, data);
}
Throwable throwable = exRef.get();
if (throwable instanceof OutOfMemoryError)
throw (OutOfMemoryError) throwable;
if (throwable instanceof IllegalStateException)
throw (IllegalStateException) throwable;
fail();
} catch (OutOfMemoryError | IllegalStateException e) {
// expected
}
try {
store.close();
} catch (IllegalStateException e) {
// expected
}
store.closeImmediately();
store = MVStore.open(fileName);
store.openMap("test");
store.close();
} finally {
// just in case, otherwise if this test suffers a spurious failure,
// succeeding tests will too, because they will OOM
store.closeImmediately();
FileUtils.delete(fileName);
}
}
use of org.h2.mvstore.MVStore in project georocket by georocket.
the class H2Store method close.
/**
* Release all resources and close this store
*/
public void close() {
MVStore s = mvstore.getAndSet(null);
if (s != null) {
s.close();
}
map = null;
}
use of org.h2.mvstore.MVStore in project georocket by georocket.
the class H2Store method getMVStore.
/**
* Get or create the H2 MVStore
* @return the MVStore
*/
protected MVStore getMVStore() {
MVStore result = mvstore.get();
if (result == null) {
synchronized (mvstore) {
MVStore.Builder builder = new MVStore.Builder().fileName(path);
if (compress) {
builder = builder.compress();
}
result = builder.open();
mvstore.set(result);
}
}
return result;
}
use of org.h2.mvstore.MVStore in project jackrabbit-oak by apache.
the class PersistentCache method createMapFactory.
private MapFactory createMapFactory(final int generation, final boolean readOnly) {
MapFactory f = new MapFactory() {
final String fileName = getFileName(generation);
MVStore store;
@Override
void openStore() {
if (store != null) {
return;
}
MVStore.Builder builder = new MVStore.Builder();
try {
if (compress) {
builder.compress();
}
if (manualCommit) {
builder.autoCommitDisabled();
}
if (fileName != null) {
builder.fileName(fileName);
}
if (memCache >= 0) {
builder.cacheSize(memCache);
}
if (readOnly) {
builder.readOnly();
}
if (maxSizeMB < 10) {
builder.cacheSize(maxSizeMB);
}
if (autoCompact >= 0) {
builder.autoCompactFillRate(autoCompact);
}
builder.backgroundExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
exceptionCount++;
LOG.debug("Error in the background thread of the persistent cache", e);
LOG.warn("Error in the background thread of the persistent cache: " + e);
}
});
store = builder.open();
if (appendOnly) {
store.setReuseSpace(false);
}
} catch (Exception e) {
exceptionCount++;
LOG.warn("Could not open the store " + fileName, e);
}
}
@Override
synchronized void closeStore() {
if (store == null) {
return;
}
boolean compact = compactOnClose;
try {
if (store.getFileStore().isReadOnly()) {
compact = false;
}
// clear the interrupted flag, if set
Thread.interrupted();
store.close();
} catch (Exception e) {
exceptionCount++;
LOG.debug("Could not close the store", e);
LOG.warn("Could not close the store: " + e);
store.closeImmediately();
}
if (compact) {
try {
MVStoreTool.compact(fileName, true);
} catch (Exception e) {
exceptionCount++;
LOG.debug("Could not compact the store", e);
LOG.warn("Could not compact the store: " + e);
}
}
store = null;
}
@Override
<K, V> Map<K, V> openMap(String name, Builder<K, V> builder) {
try {
if (builder == null) {
return store.openMap(name);
}
return store.openMap(name, builder);
} catch (Exception e) {
exceptionCount++;
LOG.warn("Could not open the map", e);
return null;
}
}
@Override
long getFileSize() {
try {
if (store == null) {
return 0;
}
FileStore fs = store.getFileStore();
if (fs == null) {
return 0;
}
return fs.size();
} catch (Exception e) {
exceptionCount++;
LOG.warn("Could not retrieve the map size", e);
return 0;
}
}
};
f.openStore();
return f;
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class Database method setWriteDelay.
public void setWriteDelay(int value) {
writeDelay = value;
if (writer != null) {
writer.setWriteDelay(value);
// TODO check if MIN_WRITE_DELAY is a good value
flushOnEachCommit = writeDelay < Constants.MIN_WRITE_DELAY;
}
if (mvStore != null) {
int millis = value < 0 ? 0 : value;
mvStore.getStore().setAutoCommitDelay(millis);
}
}
Aggregations