Search in sources :

Example 21 with FileStore

use of org.h2.mvstore.FileStore in project SpringStudy by myounghaklee.

the class Store method close.

/**
 * Close the store. Pending changes are persisted.
 * If time is allocated for housekeeping, chunks with a low
 * fill rate are compacted, and some chunks are put next to each other.
 * If time is unlimited then full compaction is performed, which uses
 * different algorithm - opens alternative temp store and writes all live
 * data there, then replaces this store with a new one.
 *
 * @param allowedCompactionTime time (in milliseconds) alloted for file
 *                              compaction activity, 0 means no compaction,
 *                              -1 means unlimited time (full compaction)
 */
public void close(int allowedCompactionTime) {
    try {
        FileStore fileStore = mvStore.getFileStore();
        if (!mvStore.isClosed() && fileStore != null) {
            boolean compactFully = allowedCompactionTime == -1;
            if (fileStore.isReadOnly()) {
                compactFully = false;
            } else {
                transactionStore.close();
            }
            if (compactFully) {
                allowedCompactionTime = 0;
            }
            mvStore.close(allowedCompactionTime);
            String fileName = fileStore.getFileName();
            if (compactFully && FileUtils.exists(fileName)) {
                // the file could have been deleted concurrently,
                // so only compact if the file still exists
                MVStoreTool.compact(fileName, true);
            }
        }
    } catch (MVStoreException e) {
        int errorCode = e.getErrorCode();
        if (errorCode == DataUtils.ERROR_WRITING_FAILED) {
        // disk full - ok
        } else if (errorCode == DataUtils.ERROR_FILE_CORRUPT) {
        // wrong encryption key - ok
        }
        mvStore.closeImmediately();
        throw DbException.get(ErrorCode.IO_EXCEPTION_1, e, "Closing");
    }
}
Also used : FileStore(org.h2.mvstore.FileStore) MVStoreException(org.h2.mvstore.MVStoreException)

Example 22 with FileStore

use of org.h2.mvstore.FileStore in project SpringStudy by myounghaklee.

the class Store method statisticsEnd.

/**
 * Stop collecting statistics.
 *
 * @return the statistics
 */
public Map<String, Integer> statisticsEnd() {
    HashMap<String, Integer> map = new HashMap<>();
    FileStore fs = mvStore.getFileStore();
    int reads = fs == null ? 0 : (int) (fs.getReadCount() - statisticsStart);
    map.put("reads", reads);
    return map;
}
Also used : FileStore(org.h2.mvstore.FileStore) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 23 with FileStore

use of org.h2.mvstore.FileStore in project SpringStudy by myounghaklee.

the class ValueBlob method createTemporary.

/**
 * Create a BLOB in a temporary file.
 */
private static ValueBlob createTemporary(DataHandler handler, byte[] buff, int len, InputStream in, long remaining) throws IOException {
    String fileName = ValueLob.createTempLobFileName(handler);
    FileStore tempFile = handler.openFile(fileName, "rw", false);
    tempFile.autoDelete();
    long tmpPrecision = 0;
    try (FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null)) {
        while (true) {
            tmpPrecision += len;
            out.write(buff, 0, len);
            remaining -= len;
            if (remaining <= 0) {
                break;
            }
            len = ValueLob.getBufferSize(handler, remaining);
            len = IOUtils.readFully(in, buff, len);
            if (len <= 0) {
                break;
            }
        }
    }
    return new ValueBlob(new LobDataFile(handler, fileName, tempFile), tmpPrecision);
}
Also used : FileStore(org.h2.store.FileStore) LobDataFile(org.h2.value.lob.LobDataFile) FileStoreOutputStream(org.h2.store.FileStoreOutputStream)

Example 24 with FileStore

use of org.h2.mvstore.FileStore 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 25 with FileStore

use of org.h2.mvstore.FileStore in project h2database by h2database.

the class ChangeFileEncryption method process.

private void process(String fileName, boolean quiet) {
    if (fileName.endsWith(Constants.SUFFIX_MV_FILE)) {
        try {
            copy(fileName, quiet);
        } catch (IOException e) {
            throw DbException.convertIOException(e, "Error encrypting / decrypting file " + fileName);
        }
        return;
    }
    FileStore in;
    if (decrypt == null) {
        in = FileStore.open(null, fileName, "r");
    } else {
        in = FileStore.open(null, fileName, "r", cipherType, decrypt);
    }
    try {
        in.init();
        copy(fileName, in, encrypt, quiet);
    } finally {
        in.closeSilently();
    }
}
Also used : FileStore(org.h2.store.FileStore) IOException(java.io.IOException)

Aggregations

FileStore (org.h2.store.FileStore)20 FileStore (org.h2.mvstore.FileStore)16 IOException (java.io.IOException)14 MVStore (org.h2.mvstore.MVStore)11 BufferedInputStream (java.io.BufferedInputStream)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 FileStoreInputStream (org.h2.store.FileStoreInputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 BitSet (java.util.BitSet)6 HashSet (java.util.HashSet)6 DbException (org.h2.message.DbException)6 FileStoreOutputStream (org.h2.store.FileStoreOutputStream)6 InputStream (java.io.InputStream)5 InputStreamReader (java.io.InputStreamReader)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Setting (org.h2.engine.Setting)5 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 OutputStream (java.io.OutputStream)3