Search in sources :

Example 66 with MVStore

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);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) MVStore(org.h2.mvstore.MVStore) Random(java.util.Random) FilePathMem(org.h2.store.fs.FilePathMem)

Example 67 with MVStore

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;
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 68 with MVStore

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;
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 69 with MVStore

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;
}
Also used : Builder(org.h2.mvstore.MVMap.Builder) MVStore(org.h2.mvstore.MVStore) FileStore(org.h2.mvstore.FileStore)

Example 70 with MVStore

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);
    }
}
Also used : Constraint(org.h2.constraint.Constraint)

Aggregations

MVStore (org.h2.mvstore.MVStore)123 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)69 Random (java.util.Random)23 Task (org.h2.util.Task)20 TransactionStore (org.h2.mvstore.db.TransactionStore)19 Transaction (org.h2.mvstore.db.TransactionStore.Transaction)17 MVRTreeMap (org.h2.mvstore.rtree.MVRTreeMap)8 SpatialKey (org.h2.mvstore.rtree.SpatialKey)8 IOException (java.io.IOException)6 Connection (java.sql.Connection)5 Statement (java.sql.Statement)5 FileStore (org.h2.mvstore.FileStore)5 StreamStore (org.h2.mvstore.StreamStore)5 ArrayList (java.util.ArrayList)4 TreeMap (java.util.TreeMap)4 Store (org.h2.mvstore.db.MVTableEngine.Store)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 PreparedStatement (java.sql.PreparedStatement)3