Search in sources :

Example 31 with FileStore

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

the class CreateScriptFile method openScriptWriter.

/**
 * Open a script writer.
 *
 * @param fileName the file name (the file will be overwritten)
 * @param compressionAlgorithm the compression algorithm (uppercase)
 * @param cipher the encryption algorithm or null
 * @param password the encryption password
 * @param charset the character set (for example UTF-8)
 * @return the print writer
 * @throws IOException on failure
 */
public static PrintWriter openScriptWriter(String fileName, String compressionAlgorithm, String cipher, String password, String charset) throws IOException {
    try {
        OutputStream out;
        if (cipher != null) {
            byte[] key = SHA256.getKeyPasswordHash("script", password.toCharArray());
            FileUtils.delete(fileName);
            FileStore store = FileStore.open(null, fileName, "rw", cipher, key);
            store.init();
            out = new FileStoreOutputStream(store, compressionAlgorithm);
            out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE_COMPRESS);
        } else {
            out = FileUtils.newOutputStream(fileName, false);
            out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE);
            out = CompressTool.wrapOutputStream(out, compressionAlgorithm, "script.sql");
        }
        return new PrintWriter(new OutputStreamWriter(out, charset));
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : FileStore(org.h2.store.FileStore) OutputStream(java.io.OutputStream) FileStoreOutputStream(org.h2.store.FileStoreOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) FileStoreOutputStream(org.h2.store.FileStoreOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 32 with FileStore

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

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 33 with FileStore

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

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 34 with FileStore

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

the class InformationSchemaTable method settings.

private void settings(SessionLocal session, ArrayList<Row> rows) {
    for (Setting s : database.getAllSettings()) {
        String value = s.getStringValue();
        if (value == null) {
            value = Integer.toString(s.getIntValue());
        }
        add(session, rows, identifier(s.getName()), value);
    }
    add(session, rows, "info.BUILD_ID", "" + Constants.BUILD_ID);
    add(session, rows, "info.VERSION_MAJOR", "" + Constants.VERSION_MAJOR);
    add(session, rows, "info.VERSION_MINOR", "" + Constants.VERSION_MINOR);
    add(session, rows, "info.VERSION", Constants.FULL_VERSION);
    if (session.getUser().isAdmin()) {
        String[] settings = { "java.runtime.version", "java.vm.name", "java.vendor", "os.name", "os.arch", "os.version", "sun.os.patch.level", "file.separator", "path.separator", "line.separator", "user.country", "user.language", "user.variant", "file.encoding" };
        for (String s : settings) {
            add(session, rows, "property." + s, Utils.getProperty(s, ""));
        }
    }
    add(session, rows, "DEFAULT_NULL_ORDERING", database.getDefaultNullOrdering().name());
    add(session, rows, "EXCLUSIVE", database.getExclusiveSession() == null ? "FALSE" : "TRUE");
    add(session, rows, "MODE", database.getMode().getName());
    add(session, rows, "QUERY_TIMEOUT", Integer.toString(session.getQueryTimeout()));
    add(session, rows, "TIME ZONE", session.currentTimeZone().getId());
    add(session, rows, "TRUNCATE_LARGE_LENGTH", session.isTruncateLargeLength() ? "TRUE" : "FALSE");
    add(session, rows, "VARIABLE_BINARY", session.isVariableBinary() ? "TRUE" : "FALSE");
    add(session, rows, "OLD_INFORMATION_SCHEMA", session.isOldInformationSchema() ? "TRUE" : "FALSE");
    BitSet nonKeywords = session.getNonKeywords();
    if (nonKeywords != null) {
        add(session, rows, "NON_KEYWORDS", Parser.formatNonKeywords(nonKeywords));
    }
    add(session, rows, "RETENTION_TIME", Integer.toString(database.getRetentionTime()));
    // database settings
    for (Map.Entry<String, String> entry : database.getSettings().getSortedSettings()) {
        add(session, rows, entry.getKey(), entry.getValue());
    }
    Store store = database.getStore();
    MVStore mvStore = store.getMvStore();
    FileStore fs = mvStore.getFileStore();
    if (fs != null) {
        add(session, rows, "info.FILE_WRITE", Long.toString(fs.getWriteCount()));
        add(session, rows, "info.FILE_WRITE_BYTES", Long.toString(fs.getWriteBytes()));
        add(session, rows, "info.FILE_READ", Long.toString(fs.getReadCount()));
        add(session, rows, "info.FILE_READ_BYTES", Long.toString(fs.getReadBytes()));
        add(session, rows, "info.UPDATE_FAILURE_PERCENT", String.format(Locale.ENGLISH, "%.2f%%", 100 * mvStore.getUpdateFailureRatio()));
        add(session, rows, "info.FILL_RATE", Integer.toString(mvStore.getFillRate()));
        add(session, rows, "info.CHUNKS_FILL_RATE", Integer.toString(mvStore.getChunksFillRate()));
        add(session, rows, "info.CHUNKS_FILL_RATE_RW", Integer.toString(mvStore.getRewritableChunksFillRate()));
        try {
            add(session, rows, "info.FILE_SIZE", Long.toString(fs.getFile().size()));
        } catch (IOException ignore) {
        /**/
        }
        add(session, rows, "info.CHUNK_COUNT", Long.toString(mvStore.getChunkCount()));
        add(session, rows, "info.PAGE_COUNT", Long.toString(mvStore.getPageCount()));
        add(session, rows, "info.PAGE_COUNT_LIVE", Long.toString(mvStore.getLivePageCount()));
        add(session, rows, "info.PAGE_SIZE", Integer.toString(mvStore.getPageSplitSize()));
        add(session, rows, "info.CACHE_MAX_SIZE", Integer.toString(mvStore.getCacheSize()));
        add(session, rows, "info.CACHE_SIZE", Integer.toString(mvStore.getCacheSizeUsed()));
        add(session, rows, "info.CACHE_HIT_RATIO", Integer.toString(mvStore.getCacheHitRatio()));
        add(session, rows, "info.TOC_CACHE_HIT_RATIO", Integer.toString(mvStore.getTocCacheHitRatio()));
        add(session, rows, "info.LEAF_RATIO", Integer.toString(mvStore.getLeafRatio()));
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) FileStore(org.h2.mvstore.FileStore) Setting(org.h2.engine.Setting) BitSet(java.util.BitSet) FileStore(org.h2.mvstore.FileStore) MVStore(org.h2.mvstore.MVStore) Store(org.h2.mvstore.db.Store) IOException(java.io.IOException) Map(java.util.Map)

Example 35 with FileStore

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

the class TestMVStore method testProvidedFileStoreNotOpenedAndClosed.

private void testProvidedFileStoreNotOpenedAndClosed() {
    final AtomicInteger openClose = new AtomicInteger();
    FileStore fileStore = new OffHeapStore() {

        @Override
        public void open(String fileName, boolean readOnly, char[] encryptionKey) {
            openClose.incrementAndGet();
            super.open(fileName, readOnly, encryptionKey);
        }

        @Override
        public void close() {
            openClose.incrementAndGet();
            super.close();
        }
    };
    MVStore store = new MVStore.Builder().fileStore(fileStore).open();
    store.close();
    assertEquals(0, openClose.get());
}
Also used : MVStore(org.h2.mvstore.MVStore) FileStore(org.h2.mvstore.FileStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OffHeapStore(org.h2.mvstore.OffHeapStore)

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